|
An asterisk (*) is used to indicate that every instance (i.e. every hour, every weekday, etc.) of the particular time period will be used. If you wish to use more than one instance of a particular time periods, then seperate the times by a comma. If you wish for continuous execution, the start and stop items are separated by a dash. For example, if you wanted to run your command at :05 and :35 past the hour, every hour, Monday through Friday, then your time stamp would look like this: 5,35 * * * 1-5 The sixth position indicates which task will be run at the given time(s). For example, if you wanted to remove all of the files in you "temp" directory every morning at 4:45 AM, your command would look: 45 4 * * * rm /home/{username}/temp/* (Where you insert your username where appropriate.) Special Note: While system commands are normally located in the same directories with standard settings for almost all machines, it is best to put the full path of the directory to any commands, system or not. For example, it would be better to use /usr/bin/rm instead of just rm (on weather.ou.edu). This also applies to any scripts. If there are not full paths to system commands (or other local commands), then it is possible that the cronjob will error. It is always best to include full path names on all commands. Only command lines, blank lines and comments may be place in a crontab file. Any other type of text will cause the crontab to not function properly. Now what? Once the file is saved, the crontab is running. You do not need to initialize, or run any start-up program. Crontab executes when there is/are a command(s) (not comments or blank space) in the setup file (the one created above). If at any time you wish for command in the crontab file to not run anymore, just delete (or comment out) the line containing the command. If you wish to have no crontab jobs running at all, just remove (or comment out) all of the lines containing commands. (To add comments place # before any text.). What are some examples? This is an example of a crontab file that runs GEMPAK scripts for upper air maps at 9AM and 8PM everyday, and surface maps every 20 minutes past the hour. (The slight delay allows for enough time for the data to be ingested before the scripts commence to running). There are also a couple of examples (as well as online help) of crontab files if you type: man crontab at the Unix prompt. Are there any toggle options to Crontab? Yes, one has already been introduced. The -e option allows you to edit your cron file (or create a cron file does not exist). There are three toggle options to crontab:
Why do I keep getting an email each time my Cron job runs? The email is crontab's way of notifing you that it has completed (or not) the job you requested it to run. After everything is running smoothly, you may want to disable this feature, or redirect the output to a log file instaed of an email. If you wish to diasble the email (and not output to a log file) then, at the end of each of the cron job lines you wish to not be notified on, place the command: >/dev/null 2>&1 This essential writes the email out to nowhere (a trash bin of sorts), and thus solves the problem. Your final cron job line will look like this: 45 4 * * * rm /home/{username}/temp/* >/dev/null 2>&1 If you wish to diasble the email (and output to a log file) then, at the end of each of the cron job lines you wish to not be notified on, place the command: > {logfile path and name} or >> {logfile path and name} Special Note: One > means replace the current log with a new one, while (two) >> means append the current output to the end of the current log. This essential writes the email out to nowhere (a trash bin of sorts), and thus solves the problem. Your final cron job line will look like this: 45 4 * * * rm /home/{username}/temp/* > /home/{username}/cronlogs/clear_temp_dir.txt >/dev/null 2>&1 That's it? As has been show, there is not really too much to learn in order to use crontab. It is a quite useful tool in automating your recurring tasks. Happy crontabbing!!! |