{"id":1354,"date":"2021-12-07T19:13:25","date_gmt":"2021-12-07T13:43:25","guid":{"rendered":"https:\/\/smarttech101.com\/?p=1354"},"modified":"2023-08-17T15:11:35","modified_gmt":"2023-08-17T09:41:35","slug":"cron-the-job-scheduler-in-linux-unix","status":"publish","type":"post","link":"https:\/\/smarttech101.com\/cron-the-job-scheduler-in-linux-unix\/","title":{"rendered":"Cron: the Job Scheduler in Linux\/Unix"},"content":{"rendered":"\n
Cron<\/strong> is a job scheduler utility in Linux\/Unix. It is used to execute commands (called cronjobs)<\/strong> at a given schedule without any human intervention. I can use it, for example, to backup and update my computer every Sunday or reboot my servers at the start of every month.<\/p>\n\n\n\n Many distributions install it by default. But for barebone distributions like Archlinux, you might need to install it.<\/p>\n\n\n\n Archlinux<\/strong> based distributions:<\/p>\n\n\n\n Fedora<\/strong> based distributions:<\/p>\n\n\n\n Debian<\/strong> based distributions:<\/p>\n\n\n\n Now, activate the cron daemon using systemd.<\/p>\n\n\n\n Archlinux<\/strong> and Fedora <\/strong>based distributions:<\/p>\n\n\n\n Debian<\/strong> based distributions:<\/p>\n\n\n\n Now that we have set up cron, we will be learning about creating cronjobs.<\/p>\n\n\n\n Cronjobs are created using the following crontab command:<\/p>\n\n\n\n The above command opens a text file using the editor specified by the VISUAL<\/strong> or EDITOR<\/strong> environment variables. If you have not specified the variable, it uses the Vi<\/strong> editor. And if your system does not have Vi<\/strong> editor installed, it might throw another error saying it. In that case, you will need to supply EDITOR<\/strong> as well:<\/p>\n\n\n\n Here, in the text file, we write our cronjobs. Your output might look like this:<\/p>\n\n\n\n In the above example, each line corresponds to a single cronjob. And empty lines and comment lines (lines starting with a Note<\/strong>: The As stated above, you might need to supply the EDITOR<\/strong> variable also. These root<\/strong> user’s cronjobs will have administration-level permissions. Hence, they will be able to execute commands like system update<\/strong>.<\/mark><\/p>\n\n\n\n Each cronjob has 6 fields<\/strong> separated by space. The first five fields are time and date fields:<\/p>\n\n\n\nTable of Contents<\/h2>\n\n\n\n
\n
\n
\n
1. Installation and Early Preparation<\/h2>\n\n\n\n
1.1. Installation of Cron<\/h3>\n\n\n\n
~$ sudo pacman -S cronie<\/code><\/pre>\n\n\n\n
~$ sudo dnf install cronie<\/code><\/pre>\n\n\n\n
~$ sudo apt install cron <\/code><\/pre>\n\n\n\n
1.2. Activation of Cron<\/h3>\n\n\n\n
~$ sudo systemctl enable cronie.service<\/code><\/pre>\n\n\n\n
~$ sudo systemctl start cronie.service<\/code><\/pre>\n\n\n\n
~$ sudo systemctl enable cron.service<\/code><\/pre>\n\n\n\n
~$ sudo systemctl start cron.service<\/code><\/pre>\n\n\n\n
2. Creation of Cronjobs<\/h2>\n\n\n\n
~$ crontab -e<\/code><\/pre>\n\n\n\n
~$ EDITOR=nano crontab -e<\/code><\/pre>\n\n\n\n
# Chronological table of program loadings \n# User: johndoe\n# source: https:\/\/wiki.archlinux.org\/title\/cron\n\n# mm hh DD MM W \/path\/progam\n 21 01 * * * \/usr\/bin\/systemctl hibernate\n 30 06 * * 1 \/usr\/bin\/ubdatedb<\/code><\/pre>\n\n\n\n
#<\/code><\/strong>) are ignored. These cronjobs are written in special syntax which is described below.<\/p>\n\n\n\n
crontab -e<\/code><\/strong> command is used to create cronjobs for the user who executed it. To create cronjobs for the root user, use the sudo<\/strong>. <\/p>\n\n\n\n
~$ sudo crontab -e<\/code><\/pre>\n\n\n\n
2.1. Cronjob Syntax<\/h3>\n\n\n\n