Schedule tasks on Linux using crontab
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:
|
To edit the list of cronjobs you can run:
|
This wil open a the default editor (could be vi or pico), press "i" to insert text and once done hit "esc" and type ":wq" to save the file. If you save and exit the editor, all your cronjobs are saved into crontab. Cronjobs are written in the following format:
|
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:
|
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:
|
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:
|
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
|
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:
|
But crontab allows you to do this as well:
|
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:
|
Leaving the rest of the fields empty, this would be valid:
|
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:
|
Explained
Linux can report on different levels. There’s standard output (STDOUT) and standard errors (STDERR). STDOUT is marked 1, STDERR is marked 2. So the following statement tells Linux to store STDERR in STDOUT as well, creating one datastream for messages & errors:
|
Now that we have 1 output stream, we can pour it into a file. Where >
will
overwrite the file, >>
will append to the file. In this case we’d like to
to append:
|
Mailing the crontab output
By default cron saves the output in the user’s mailbox (root in this case) on the local system. But you can also configure crontab to forward all output to a real email address by starting your crontab with the following line:
|
Mailing the crontab output of just one cronjob
If you’d rather receive only one cronjob’s output in your mail, make sure this package is installed:
|
And change the cronjob like this:
|
Trashing the crontab output
Now that’s easy:
|
Just pipe all the output to the null device, also known as the black hole. On
Unix-like operating systems, /dev/null
is a special file that discards all
data written to it.
Many scripts are tested in a BASH environment with the PATH
variable
set. This way it’s possible your scripts work in your shell, but when
run from cron (where the PATH
variable is different), the script
cannot find referenced executables, and fails.
Now i want to restart this .jar file every 3 hours and for doing the same my script is:
#!/bin/bash
kill `ps -ef | grep Server.jar | grep -v grep | awk ‘{ print $2 }’`
nohup nice java -jar ‘/root/Desktop/Server/Server.jar’ & gt; ./logs.out 2& gt;& amp;1 & amp;
And for this cronjob would be:
0 */3 * * * /root/desktop/Server/restart.sh