On Linux servers, cron can be used to schedule jobs that run commands and shell scripts at fixed times, dates or intervals. It’s great for repetitive tasks like backups, downloads and deleting log files. See https://en.wikipedia.org/wiki/Cron.
Cron jobs are configured by crontab files that are stored where the cron service is located. Use just about any Linux console text editor to manage these crontab files. Here’s how to use the Nano text editor in a one-time session or as the default crontab editor.
Set Nano as the default crontab editor (session only)
Run export command to set the default editor
Set Nano as the default crontab editor (permanently)
Change to user’s home directory and edit environment settings (CentOS)
# User specific aliases and functions export VISUAL=nano export EDITOR=$VISUAL
Activate environmental PATH variables and confirm they show up in the path
Each line in the crontab file represents a scheduled job, with the following parameters. An asterisk is used as a placeholder for an unused date parameter.
- m: minute the command executes on, between 0 and 59.
- h: hour the command executes on, between 0 and 23.
- dom: day of month the command executes on.
- mon: the month the command executes on, between 1 and 12.
- dow: the day of the week the command executes on, between 0 and 7. Sunday may be specified by using 0 or 7, both values are valid.
- command: the command to execute.
# m h dom mon dow command 0 11 * * * /home/backups/ontology.sh
sudo systemctl status crond
Check for cron error messages
Check cron log for history
- Make sure file ends with an empty line!
- SELinux – sudo setenforce 0
Clear log files
This example has three jobs. Each job runs a different shell script, silences cron error messages and then appends any remaining output to a specific log file.
> is for redirect
/dev/null is a black hole for data
2 is the file descriptor for Standard Error (STDERR)
>> append to file
01 01 * * * /home/saf/logs/ontology.sh > /dev/null 2 >> /home/saf/logs/cron.log 01 01 * * * /home/saf/logs/webapps.sh > /dev/null 2 >> /home/saf/logs/cron.log 01 01 * * * /home/saf/logs/logs.sh > /dev/null 2 >> /home/saf/logs/cron.log