Search This Blog

Schedule future Linux tasks

Crontab

The crontab (cron derives from chronos, Greek for time; tab stands for table) command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. To see what crontabs are currently running on your system, you can open a terminal and run:
$ sudo crontab -l
To edit the list of cronjobs you can run:
$ sudo crontab -e
This wil open a the default editor (could be vi or pico, if you want you canchange the default editor) to let us manipulate the crontab. If you save and exit the editor, all your cronjobs are saved into crontab. Cronjobs are written in the following format:
* * * * * /bin/execute/this/script.sh

Scheduling explained

As you can see there are 5 stars. The stars represent different date parts in the following order:
·         minute (from 0 to 59)
·         hour (from 0 to 23)
·         day of month (from 1 to 31)
·         month (from 1 to 12)
·         day of week (from 0 to 6) (0=Sunday)

Execute every minute

If you leave the star, or asterisk, it means every. Maybe that's a bit unclear. Let's use the the previous example again:
* * * * * /bin/execute/this/script.sh
They are all still asterisks! So this means execute /bin/execute/this/script.sh:
·         every minute
·         of every hour
·         of every day of the month
·         of every month
·         and every day in the week.
In short: This script is being executed every minute. Without exception.

Execute every Friday 1AM

So if we want to schedule the script to run at 1AM every Friday, we would need the following cronjob:
0 1 * * 5 /bin/execute/this/script.sh
Get it? The script is now being executed when the system clock hits:
·         minute: 0
·         of hour: 1
·         of day of month: * (every day of month)
·         of month: * (every month)
·         and weekday: 5 (=Friday)

Execute on workdays 1AM

So if we want to schedule the script to Monday till Friday at 1 AM, we would need the following cronjob:
0 1 * * 1-5 /bin/execute/this/script.sh
Get it? The script is now being executed when the system clock hits:
·         minute: 0
·         of hour: 1
·         of day of month: * (every day of month)
·         of month: * (every month)
·         and weekday: 1-5 (=Monday til Friday)

Execute 10 past after every hour on the 1st of every month

Here's another one, just for practicing
10 * 1 * * /bin/execute/this/script.sh
Fair enough, it takes some getting used to, but it offers great flexibility.

Neat scheduling tricks

What if you'd want to run something every 10 minutes? Well you could do this:
0,10,20,30,40,50 * * * * /bin/execute/this/script.sh
But crontab allows you to do this as well:
*/10 * * * * /bin/execute/this/script.sh
Which will do exactly the same. Can you do the the math? ; )

Special words

For the first (minute) field, you can also put in a keyword instead of a number:
@reboot     Run once, at startup
@yearly     Run once  a year     "0 0 1 1 *"
@annually   (same as  @yearly)
@monthly    Run once  a month    "0 0 1 * *"
@weekly     Run once  a week     "0 0 * * 0"
@daily      Run once  a day      "0 0 * * *"
@midnight   (same as  @daily)
@hourly     Run once  an hour    "0 * * * *"
Leaving the rest of the fields empty, this would be valid:
@daily /bin/execute/this/script.sh

Storing the crontab output

By default cron saves the output of /bin/execute/this/script.sh in the user's mailbox (root in this case). But it's prettier if the output is saved in a separate logfile. Here's how:
*/10 * * * * /bin/execute/this/script.sh >> /var/log/script_output.log 2>&1

Explained

Although CronTabs are useful, sometimes you just want to run a command at a given time once and not have it reoccurring. The commands `at` and `batch` can do just that.
The `at` command can be used to run a command based on a specific time. Lets say you wanted to run a command 1 day in advance or even 1 hour from your current time, `at` would be the tool to use.
`batch` on the other hand runs commands based on the systems load average. By default it will run when the load average is less then 0.8.

Installation
Before using the command we need to make sure the `at` package is installed. We can do this as follows:
rpm –qa | grep ^atPlain Text
This will display a list of packages that start with the word at. If you do not have the at package installed you can install it by using the rpm command or yum.
yum install atPlain Text
Installing the at package also installed the batch binary.
`at` Usage
Lets say you wanted to schedule at to run a specific command at a future time. You could do this by specifying the at command followed by a future time: at
at 12:30Plain Text
Then press enter, and you will be prompted to enter a command. For this example, I’m going to output the current date and time to a file in the root directory.
date >> /root/datelog.txtPlain Text
Then you could press CTRL-D to issue the one time cron, or you could press enter to issue another command for the same scheduled task.
You could also specify a number of days to wait until running the given command by doing the following:
at now + 1 dayPlain Text
Then you will be prompted for your command. After you type the command you want to issue, press CTRL-D to set your schedule.
To view a list of items in the at queue you can type the following:
This would list all of the tasks you have scheduled via at. You will also see a list of numbers in front of each task. Each number specifies the job number for that task. You can use that job number in order to remove the scheduled task from running.
atrm 5Plain Text
For my example, the job number we scheduled was set to 5. In order to remove the scheduled task you type the command atrm followed by the job number.
`batch` Usage
The batch utility does not allow any command line options. In order to use batch, you simply just type the word batch.
Press enter and then type the command you wish to issue.
uptime >> /root/uptime.txtPlain Text
After issuing your command press CTRL-D to apply it. It will then be run the next time your server load is less then 0.8.