Creating and Managing cron jobs in Centos
Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time.cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.
open crontab with text editor
To open crontab with text editor enter following command
Each cron command has 5 time and date fields, followed by user name (optional), and if this is the system crontab file, it will be followed by command. Commands are executed when the time specified by the time/date fields matches the current time.
field values ----- -------------- Minutes Range: 0 – 59 Hours Range : 0 – 23 Days Range: 0 – 31 Months Range: 0 – 12 Days of the week:0 – 7 (Starting from Monday, 0 or 7 represents Sunday) USERNAME: replace this with your username path to command – The name of the script or command you want to schedule
To view crontab entries of current user use the following command.
To view crontab entries of the specific user
crontab -u username -l
schedule cron that executes twice a day
1.Below example command will run at 5 AM and 2 PM daily
0 5,14 * * * /home/scripts/script.sh
2.To run cronjob to run every 15 minutes
*/15 * * * * /home/scripts/diskspace
3. To run cronjob only on every sunday at 1AM
0 1 * * sun /home/scripts/backup
4. To run cron for every minute
* * * * * /home/scripts/test.php
5. Run script monthly on the 1st of month at 3:30AM
30 3 1 * * /home/scripts/backup