Skip to content

Scheduling jobs on CentOS

Andrew Post edited this page Jan 11, 2017 · 1 revision

On CentOS, server jobs that must run as root at an exact time every day/week/month should be run using cron and configured by adding a file to the /etc/cron.d directory that invokes a script. Cron is usually installed by default.

For one or more jobs, create a file in /etc/cron.d that is owned by root, is in the root group, and has 0600 permissions. The name of the file does not matter. The file should contain one line per job, following the format below:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
30 3 * * mon root /usr/local/sbin/my-job.sh

The contents of /etc/cron.d are parsed by cron automatically, once a minute. Nothing further needs to be done for cron to pick up the new job.

Then, create a shell script in /usr/local/sbin that runs the job. The script should be owned by root, be in the root group, and have 0700 permissions.

Cron will execute the script at the specified time.

According to various sites online, the /etc/crontab file should not be used for adding custom jobs. The file is empty in CentOS by default, and according to some websites it may be overwritten in the future by an OS update.

Similarly, the cron.daily, cron.weekly and cron.monthly directories in /etc are not appropriate for adding custom jobs that must be run at an exact time. They are managed by anacron, which is a separate job scheduling system that runs jobs at approximate times depending on system load and other factors.

Clone this wiki locally