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.
Setting the Cron Editor
Cron jobs are configured by crontab files that are stored where the cron service is located. Any Linux console text editor can be used 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 crontab editor - one time session
Run the export command to set the default editor
Set Nano as the crontab editor - as the default
Change to user's home directory and edit environment PATH settings (CentOS)
Append the following:
# User specific aliases and functions export VISUAL=nano export EDITOR=$VISUAL
Activate the change in settings
Each line in a crontab file represents a scheduled job. Each job begins with time parameters in the following sequence:
* * * * * do something minute | hour | day | month | dayofweek | command
minute: between 0 and 59.
hour: between 0 and 23.
day: day of month, depends on month.
month: between 1 and 12.
day of week: between 0 and 7, where Sunday can be both 0 or 7.
command: the command to execute.
***** any value (placeholder or unused date parameter)
/ step values.
- range of values.
, value list separator.
- Make sure the crontab file ends with an empty line!
- SELinux problems?
sudo setenforce 0
Run a shell script at 11:00am every day:
0 11 * * * sh /home/user/script.sh
Run a shell script every two hours, on the hour
0 /2 * * * sh /home/user/script.sh
Run a shell at 11:00am every Tuesday
0 11 * * 2 sh /home/user/script.sh
sudo systemctl status crond
Check for cron error messages
Check cron log for history
Clear Cron 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