Hamsa K
Editor
5 min read | 1 month ago

How to Schedule Cron Jobs with Crontab in Centos 7

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

crontab -e

Also Readv-> How to Install and Configure Redis Server on Centos 7

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.

crontab -l

To view crontab entries of the specific user

crontab -u username -l

To remove or erase all crontab jobs use below command

crontab -r

cron jobs backup

crontab -l > cronsbackup.txt
crontab -u user -l > usercronbackup.txt

 Replace your username instead of user.

Restore single user backups from backup

crontab cronsbackup.txt
crontab -u user usercronbackup.txt

Backup all user cronjobs in centos

zip -r Allcronjobs.zip /var/spool/cron

Backup all user cronjobs in ubuntu

zip -r Allcronjobs.zip /var/spool/cron/crontabs

you can use cat or tail commands to view cron logs.

cat /var/log/cron
tail -f /var/log/cron

Also Read -> How to Install Webmin on Centos 7

 Basic examples:

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

Also Read -> How to Install and Configure Redmine on Centos 7



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy