Cron: scheduling tasks
Cron is the traditional service to schedule tasks (jobs) on Linux systems. Learn how to use cron using these simple steps (you can also use Systemd timers).
Table of Contents
- Install cron if it’s not already installed
- Starting and enabling the service
- Scheduling cron jobs as root user
- Schedule cron jobs as a non-root user
Install cron if it’s not already installed
cronie is the package that contains the cron tool. You can install it using your package manager.
Starting and enabling the service
You need to start the cron service in order cron to work. Using
systemd you can check if the service is active and running with this command:
systemctl status cron
- Some systems use
crondas the service name.
Start and enable the service with
systemctl enable --now and the service name.
Scheduling cron jobs as root user
The root user uses the file
/etc/crontab or, preferably, a cron table file inside
/etc/cron.d/. You can also create hourly, daily, weekly and monthly jobs inside
An example cron table file inside /etc/cron.d/
# is used to create comments.
SHELL=/bin/bash PATH=/sbin:/bin:/user/sbin:/usr/bin MAILTO=root # minute hour day_of_month month day_of_week username command # Run a job every minute of every hour of every day # * * * * * root some_command # Run a job at 17:00 every Monday # 0 17 * * mon root some_command # Run a job every ten minutes # 0/10 * * * * root some_command
Cron jobs inside cron.hourly, cron.daily, etc.
Files inside these folders are simple scripts:
Schedule cron jobs as a non-root user
Non-root users can use the crontab utility to create cron jobs.
This command will prompt for a default editor. Then, it will show a file you can edit to add your cron jobs at the end.
# minute hour day_of_month month day_of_week command # Run the script every day at 17:00 0 17 * * * /home/ricardo/script.sh
anacron works like
cron, but it does not assume that the machine is always running (some jobs in
cron may never be started if the machine is off when job is scheduled).
anacron may be already installed in some distros with
cron (as part of
cronie package) or you need to install it manually.
Anacron list jobs from the
/etc/anacrontab file. This is a template of the content of this file:
SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command
If you have any suggestion, feel free to contact me via social media or email.