That sounds a little confusing, but with a few examples, you will see that it is not as complicated as it sounds.
Code Listing 3.8: Examples |
# Run /bin/false every minute year round * * * * * /bin/false # Run /bin/false at 1:35 on the mon,tue,wed and the 4th of every month 35 1 4 * mon-wed /bin/false # Run /bin/true at 22:25 on the 2nd of March 25 22 2 3 * /bin/true # Run /bin/false at 2:00 every Monday, Wednesday and Friday 0 2 * * 1-5/2 /bin/false |
Note: Notice how you have to specify specific days of the week and days of the month before they are combined. If you have * for only one of them, the other takes precedence, while * for both just means every day. |
To test what we have just learned, let's go through the steps of actually inputting a few cron-jobs. First, create a file called crons.cron and make it look like the this:
Code Listing 3.9: Editing crons.cron |
$ nano crons.cron #Mins Hours Days Months Day of the week 10 3 1 1 * /bin/echo "I don't really like cron" 30 16 * 1,2 * /bin/echo "I like cron a little" * * * 1-12/2 * /bin/echo "I really like cron" |
Now we can add that crontab to the system with the "new command" from the table above.
4. Using cronbase
As mentioned earlier, all of the available cron packages depend on sys-process/cronbase. The cronbase package creates /etc/cron.{hourly,daily, weekly,monthly}, and a script called run-crons. You might have noticed that the default/etc/crontab contains something like this:
Code Listing 4.1: Default system crontab |
*/15 * * * * test -x /usr/sbin/run-crons && /usr/sbin/run-crons 0 * * * * rm -f /var/spool/cron/lastrun/cron. |
To avoid going into much detail, we can just assume that these commands will effectively run your hourly, daily, weekly and monthly scripts. This method of scheduling cron-jobs has some important advantages:
- They will run even if your computer was off when they were scheduled to run
- It is easy for package maintainers to place scripts in those well defined places
- You know exactly where your cron-jobs and your crontab are stored, making it easy for you to backup and restore this part of your system
If you're having problems getting cron to work properly, you might want to go through this quick checklist.
- Is cron running? Run ps ax | grep cron and make sure it shows up!
- Is cron working? Try: * * * * * /bin/echo "foobar" >> /file_you_own and make sure it works
- Is your command working? Try: * * * * * /bin/foobar > /file_you_own 2>&1 and look for errors in /file_you_own
- Can cron run your job? Check the cron log, usually /var/log/cron.log or /var/log/messages for errors
- Are there any dead.letters? cron usually sends mail when there's a problem; check your mail and also look for ~/dead.letter.
Remember, each cron package is different and the range of features varies greatly. Be sure to consult the man pages for crontab, fcrontab or anacrontab, depending on what you use.
No comments:
Post a Comment