Scheduling jobs with the cron utility
Often we have tasks we perform on a regular basis. It might be every hour, every day, or the first day of every month. If you can script the task then you can also have the computer run the task automatically for you. On Linux systems you would use the cron utility to run those jobs. To schedule the job you will edit the crontab file with when to run the job and the name of the script to run. Each line starts with five fields or spots for when to run the job. They are, in order, the minute from zero to 59, the hour from zero to 23, the day of the month from 1 to 31, the month of the year from 1 to 12, and the day of the week from zero to 6. Some systems will take both zero and 7 as values to run on a Sunday. You can use an asterisk to indicate all of a particular timeframe. So like every minute or every hour or every day. Then you simply specify the name of the script or program to run. You will typically always put the full path to the script here.
|
# ┌───────────── minute (0 - 59) |
so you might be asking how to edit the crontab file. Well this is really easy. You simply run crontab -e and you will open up your text editor to create a new file or edit the existing one. Of course different people prefer different editors. Some like vi some nano some emacs etc. You can set which one is used for you by simply editing your .bash_profile file and add the line export VISUAL="nano" (enter editor of choice where I put nano) and save it out. Of course this will not be active till the next time you log in. so you can run that command right on the command line to make the change immediate (don't forget to put it in your .bash_profile file also).
Let's consider a few examples to make this more clear. Let's say you need to run a daily report that checks several things on server like available disk space and mail it to your team. You make a script file called mydailyreport.sh that has runs all the different commands and outputs the result to a file. Then the script will use the mail command to mail the latest file out to the team. You run this every morning first thing going from one server to the next spending the first hour of your day every day doing this task. Instead we will set up a cron job that will run that shell script at 8 AM every morning. You would put the following line in your crontab file.
0 8 * * * /home/asg/mydailyreport.sh |
|
Often we want a job to only run say on the weekdays. If we wanted this daily report to only run Monday through Friday we can adjust the command to do that. And just for grins let's run it at 8:17 AM. We can set a range or make a list of values. So for weekdays we can use a 1-5. If you wanted say just Monday, Wednesday, and Friday you could put 1,3,5 in that spot instead of the 1-5. So the command below will run at 8:17 AM Monday through Friday.
17 8 * * 1-5 /home/asg/mydailyreport.sh
Let's consider another example. This is one that I have recently created for work. I needed to run a process to gather some statistics once a minute 24 by 365 that then gets sent to an InfluxDB instance. Here is the line in crontab to run this particular job. Notice that I simply put asterisks in for all 5 spots. I have also used this same type of entry for things like scripts to check netstat to find connections on a particular port to track down servers connecting into a server of mine for some service.
* * * * * /home/asg/ldapstats.sh |
|
You can also specify the times using what is called step values. This means it will run every X period. For example if you want to run something every 2 hours you could put in 0-23/2 in the second position. You can also use the asterisk to specify the all. So we could do every 2 hours with */2 or say every 5 minutes with */5 for the step indication. So let's look at a couple lines that use these. The first will run every 5 minutes and the second every 2 hours.
*/5 * * * * /apps/scripts/chkusers.sh
0 0-23/2 * * * /apps/scripts/alertsrpt.sh
Another popular one would be a weekly backup every Sunday night. This one would run every Sunday night at 1 AM. Notice the use of the name of the day instead of the number here. For months and day of the week you can use the full name or the abbreviation.
0 1 * * Sun /apps/scripts/weekly-backup.sh
Additional crontab options
There are some additional options for the crontab command. First, you don't have to use the editor from within crontab. You can make a simple text file with all the information you want for your various crontab jobs. Then you can simply load it in by the following command.
crontab mycronfile.txt
Here are some additional options for the command line.
|
|
Home |
About |
Services |
Copyright © 2016