How to Setup Cron Job on Linux
Cron is Linux most useful tools which allow running automated commands at specific date & time. This is developer favourite because it helps to set up the task-specific script in the background which will run on defined time. So In this post, we will learn how to a setup cron job using your shell user.
- Linux System where you setup cron
- A command line or terminal window for add/edit/delete/list cron
- A user account with root or sudo privileges
The command for view the existing cron
The command for creating and editing cron jobs
The standard form for a crontab line is as follows:
* * * * * /directory/command output
Explanation of above cron syntax:
- 1 *: Minutes range: 0 - 59
- 2 *: Hours range: 0 - 23
- 3 *: Days range: 0 - 31
- 4 *: Months range: 0 - 12
- 5 *: Days of the week range: 0 - 7. Starting from Monday, 0 or 7 represents Sunday
- /directory/command - specifies the location and script you want to run
- output - it is optional and it defines how the system notifies the user of the job completion
Below are a few basic examples to get an idea of how it works.
Run once an hour (every hour at minute zero)
0 * * * * /var/www/html/cron-job mail.php >/dev/null 2>&1
Run at every 2nd minute
*/2 * * * * /var/www/html/cron-job mail.php >/dev/null 2>&1
Run at minute 30 means run at every hour at 30 minutes
30 * * * * /var/www/html/cron-job mail.php >/dev/null 2>&1
Run at every 12th hour
0 */12 * * * /var/www/html/cron-job mail.php >/dev/null 2>&1
Run once a day (every day at midnight and minute zero)
0 0 * * * /var/www/html/cron-job mail.php
Run once a month (on the first day of every month at midnight and minute zero)
0 0 1 * * /var/www/html/cron-job mail.php
Run once a year (on the first day of the first month every year at midnight and minute zero)
0 0 1 1 * /var/www/html/cron-job mail.php
Run at every Sunday
0 0 * * SUN /var/www/html/cron-job mail.php
Set up a crontab
Open you SSH into the server and edit the crontab
the crontab default editor is vi editors. So in vi editors below are the step to set up the crontab
- press ESC.
- press I (for "insert") to begin editing the file.
- paste the cron command in the file.
- Press ESC again to exit editing mode.
- type:wq to save (w - write) and exit (q - quit) the file.
You can add multiple cron jobs. just add each corn command in a new line.
This is a simple post about how to set up cron jobs. I hope you like this article In next article we learn how to setup the node cron.
You May Like: How to increase MySQL GROUP_CONCAT() maximum length