Linux Crontab

There are two kinds of crontab jobs a linux server can use: user and system. To determine which your system has type crontab -e from your command prompt. When you look at the file, the jobs will either have a username or not. With a username, it’s a system crontab.

* * * * * /path-to/and-command-to-be-executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
* * * * * username /path-to/and-command-to-be-executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

To schedule a job in your chrontab, like a database backup, for example, just append a line to the file with the time, (username if needed), command, and any optional arguments, and save the file. Assuming you have a database backup script called backup-db.sh that you want to run every day at 1:00 am, and your server root path is /www/public_html, then you would do something like:

0 1 * * * /www/public_html/backup-db.sh

It’s a lot, lot better if your scripts are in a non-publicly accessible directory, obviously. They have your passwords in plain text stored in them.

If you don’t want to see the output from the job, use >/dev/null 2>&1 after your script arguments like

0 1 * * * /www/public_html/backup-db.sh >/dev/null 2>&1

10 Responses to Linux Crontab

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>