Try out this handy cron job crib sheet which you can paste directly into your crontab as a reference each time you make a scheduled job change.
Cron is a unix program that is used to automatically run commands or scripts at predetermined times. Common uses include running nightly backups, log searching or other system resource intensive tasks when everyone is at home sleeping. Each unix user has their own crontab list. If you are new to scheduling tasks in Sun Solaris, Red Hat Enterprise Linux, Fedora, Ubuntu, Debian OS X, FreeBSD or any other flavor of unix, the following quick cron guide should help.
To check what is currently scheduled for execution for one user, open a terminal and type:
crontab -l -u <username>
Any task that requires root privileges will need to be put in the root uer's crontab. Let's add a simple backup scheduled task as an example. To open the cron file as the root user with your default text editor, type:
sudo crontab -e
At the top of the crontab file (or if it is empty) paste in the following:
# +- - - - - - minute [0-59] | Special Entries: @reboot, @yearly, @monthly,
# | +- - - - - hour [0-23] | @weekly, @midnight, @hourly
# | | +- - - - monthday [1-31] ---------------------------------------------
# | | | +- - - month [1-12, jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec]
# | | | | +- - weekday [0-6, sun,mon,tue,wed,thu,fri,sat,sun]
# * * * * * /full/path/to/command args >> ~/command.log
This Crib Sheet will serve as a crontab syntax reference every time you list or edit the crontab but won't be interpreted by cron since each line starts with a poundsign (#) character indicating a comment line. The first five columns allow you to define the minutes, hours, days of the month, months, and weekdays that you want your job to run. The sixth column is the command or script to execute at the appointed time or times.
Here's an example crontab line that runs the backup.sh script every twenty minutes on April 13th, 14th and 15th:
0,20,40 * 13-15 4 * /home/tom/scripts/backup.sh >> /var/log/backups.log
The last part may look new if you are unfamiliar with pipes. The double greaterthan signs will append the output of backup.sh into a file called backups.log located in the /var/log/ directory. While you can chain together many unix commands directly in the crontab file, I recommend using a separate file (ie: in your scripts directory) to perform more complicated tasks.
Here's another cron job example that searches for files in anyones home directory modified in the last 24 hours and puts a report in your home directory:
0 1 * * * /usr/bin/find /home -mtime -1 -type f -exec ls -lh {} \; > \
/home/tom/new_files_report_`date +%Y-%m-%d`.txt
Hope that helps! Comments welcome.
No comments:
Post a Comment