Use Crontab to schedule tasks on Linux

You can use Crontab to schedule the execution of tasks. The command crontab -l list all the scripts already scheduled on your machine and the option -e runs the editing mode.
The basic format string looks like this:

A B C D E /bin/do_something.sh

Where
A = minutes (0-59)
B = hours (0-23)
C = day (1-31)
D = month (1-12)
E = week day (0-6 where 0 is Sunday)

The following are examples for:
– Execute /bin/do_something.sh at 9:40 PM from monday to wednesday
– Execute the script every 15 minute and every day
– Execute the script every 21st and 44th minute on every hour and every day, but only in December

40 21 * * 1-3 /bin/do_something.sh
*\15 * * * * /bin/do_something.sh  
21,44 * * 12 * /bin/do_something.sh

Other interesting options:
@reboot = Run once, at startup
@yearly = Run once a year. Like “0 0 1 1 *”
@annually (same as @yearly)
@monthly = Run once a month. Like “0 0 1 * *”
@weekly = Run once a week. Like “0 0 * * 0”
@daily = Run once a day. Like “0 0 * * *”
@midnight (same as @daily)
@hourly = Run once every hour. Like “0 * * * *”

This execute /bin/do_something.sh once, at startup:

@reboot /bin/do_something.sh

How can you manipulate the output?

By default the output is sending to the user (root) mailbox, but it can be redirected.
This Add a row to the file do_something.log inserting output and errors:

@weekly /bin/do_something.sh >> /var/log/do_something.log 2>&1

Send a mail to me@mydomain.com:

* 1,2,3 * * * /bin/script 2>&1 | mail -s "Cronjob ouput" me@mydomain.com:

Trash all output:

@daily /bin/script > /dev/null 2>&1

Leave a Reply

Your email address will not be published.