Almost every Linux distribution has some form of cron installed by default. However, if you’re using a CentOS machine on which cron isn’t installed, you can install it using dnf.
sudo dnf install crontabs
This will install cron on your system, but you’ll need to start the daemon manually. You’ll also need to make sure it’s set to run whenever the server boots. You can perform both of these actions with the systemctl command.
sudo systemctl start crond.service
To set cron to run whenever the server starts up, type:
sudo systemctl enable crond.service
Here are some more examples of how to use cron’s scheduling component:
* * * * * - Run the command every minute.
12 * * * * - Run the command 12 minutes after every hour.
0,15,30,45 * * * * - Run the command every 15 minutes.
*/15 * * * * - Run the command every 15 minutes.
0 4 * * * - Run the command every day at 4:00 AM.
0 4 * * 2-4 - Run the command every Tuesday, Wednesday, and Thursday at 4:00 AM.
20,40 */8 * 7-12 * - Run the command on the 20th and 40th minute of every 8th hour every day of the last 6 months of the year.
crontab -e
Note: On new CentOS 8 servers, the crontab -e command will open up your user’s crontab with vi by default. vi is an extremely powerful and flexible text editor, but it can feel somewhat obtuse for users who lack experience with it.
If you’d like to use a more approachable text editor as your default crontab editor, you could install and configure nano as such.
To do so, install nano with dnf:
sudo dnf install nano
To set nano as your user profile’s default visual editor, open up the .bash_profile file for editing. Now that you’ve installed it, you can do so with nano:
nano ~/.bash_profile
At the bottom of the file, add the following line:
export VISUAL="nano"