Skip to content

cronie - Timed Tasks

Prerequisites

  • A computer running Rocky Linux
  • Know how to use your favorite editor to change the configuration file in the command line environment (this article will use vi)
  • You have understood the basic knowledge of bash, python, or other scripting or programming tools and expect to run the script automatically
  • You have connected to your computer by SSH (it can be root user or ordinary user with UID greater than 1000)

cron introduction

GNU/Linux provides the cron system, a time-based cron jobs program for automated processes. It is not hard but quite powerful. Do you want a script or program to run at 5 PM daily? cron can do it. There are different branches (or variants) of cron, which have the same functions. This document uses cronie, and the version is 1.5.2. Click here to find the latest version and update the log.

cronie description

  • cronie -package name, Rocky Linux includes cronie by default
  • crontab -command to maintain crontab (task schedule) for each user
  • crond.service -cronie's daemon, you can manage the daemon by systemctl start | restart | stop | status
  • /etc/crontab -Assign cron jobs to different users, usually we are more accustomed to using crontab -e . For example, if you are currently logged in as the root user, type crontab -e and you will see specific cron jobs in the file /var/spool/cron/root after saving.
  • /var/log/cron * - cronie's log, by default, does log rotation and ends with a date suffix. Here, it means wildcard.
  • anacron - part of cronie. For more information about anacron, see anacron-automation commands.

crontab Command

crontab is a command obtained after installation of the cronie package. Compared with anacron, it is more suitable for servers that work 24 by 7. Common options of crontab are:

-e # edit crontab scheduled tasks
the -l # View crontab task
-r # delete all the current user's crontab tasks

Use of cronie

Writing commands or scripts into this file allows them to run at different times. However, usually we are more accustomed to using crontab -e.

shell > cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# 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
ParameterMeaningValue Range
The 1st*The first minute of the hour0-59
The 2nd*Hour of the day0-23
The 3rd*Day of the month1-31
The 4th*The month of the year1-12
The 5th*Day of the week0-7 (0 and 7 both indicate Sunday)

In the example that follows, assuming you are performing this operation as the root user, type the following:

  • crontab -e will show the root user's timed tasks. If you use vi as the default system editor, press I key to enter the insert mode.
  • "#" means this is a line of comment.
# Nightly 10:00 backup system
00 22 *  *  * /usr/local/sbin/backup
  • After entering this line (again assuming vi is the system editor), press Esc to exit insert mode
  • Save and exit your file with Shift+:+w+q+! (displayed at the bottom of the editor)

The script will run every night at 22:00. This is a simplistic example. If you need something more elaborate, things can become more complicated.

Attention

The script needs to have execute permission (chmod +x) before cronie can run it.

Complex options

The content discussed so far are simplistic options, but what about more complex timed tasks?

# Suppose you want to run every 10 minutes backup script (may be impractical, however, it is only an example!) Throughout the day. To this end, the following will be written:
* /10 *  *  *  * /usr/local/sbin/backup
#What if you only want to run a backup every 10 minutes on Monday, Wednesday, and Friday? :
* /10 *  *  * 1,3,5 /usr/local/sbin/backup
# In addition to Saturdays and Sundays, once every 10 minutes, every day, how to back up?
* /10 *  *  * 1-5 /usr/local/sbin/backup
Special SymbolsMeaning
*represents any time. For example, the first * means any minute, and the second * means any hour
,stands for discontinuous time, such as 0 8,12,16 * * *, which means that the command will run once every day at 8:00, 12:00, and 16:00
-represents a continuous time range, such as 0 5 * * 1-6, which means that a command will run at five o'clock in the morning every day from Monday to Saturday
*/nRepresents how often the interval will run, such as */10 * * * * means that it every 10 minutes

Attention

The smallest time unit that cronie can recognize is 1 minute; when using, for example, 30 4 1,15 * 5 command , it will cause the command to run on the 1st and 15th of each month and 4:30 in the morning of every Friday ; The output information of some scripts or commands will prevent the execution of timed tasks, and output redirection is required, such as this- */10 * * * * /usr/local/sbin/backup &> /dev/null

Q and A

  1. /etc/crontab and crontab -e , is there any difference between the two methods? crontab -e does not need to specify a user (the currently logged-in user is used by default), while /etc/crontab needs to specify a user.
  2. What should you do if the specified command or script is not run correctly? Check the /var/log/cron* file, use journalctl -u crond.service to check the information about the daemon process, whether the script has x permission, etc., for troubleshooting.
  3. In addition to cronie, what cron variants are there? fcron, the latest version is 3.3.0 (dev, 2016-08-14). cronsun, the latest version 0.3.5 (2018-11-20).

Summary

For Rocky Linux desktop users or system administrators, cronie is a powerful tool. It allows the automation of tasks and scripts, so you do not have to remember to run them manually. Although the basic knowledge is not hard, the task can be complex. For more information about crontab, visit crontab man page. You can also search for "crontab" on the Internet, which provides many search results and helps you fine-tune the crontab expression.

Author: tianci li

Contributors: Steven Spencer, Ganna Zhyrnova