CRON AND ANACRON

Task scheduling is an important and useful option built into both Windows and now Linux. For a while there, if you wanted to schedule tasks in Linux, it wasn’t as easy as Windows, technically, it still isn’t, however, it has grown leaps and bounds over what it used to be. With Linux you have a multitude of options, Cron and Anacron are just two such options. Today, we’ll focus mainly on these two, the similarities and differences. What makes each of them great, what might make one better than the other depending upon the individual circumstance. Task Scheduling in Windows was a difficult thing for me to learn as well, but it was pretty straight forward once I figured it out. In Linux, you have to either use the built-in Systemd timers or Cron/Anacron. Systemd is a really good thing to use if you are trying to be more precise with regards to timing, however, that is for a later article.

Both Cron and Ananacron use the system time, but what makes these two different is that Anacron runs 5 minutes after the computer is booted, then waits until the hour that the job was scheduled each day/week/month. Anacron is also picky about the shell environment used to perform system functions on such a basis. This could be a downside for new users who want to use third party scripts to handle maintenance and other things without them having to work at the system manually all the time. For those users, there is Cron, but it too takes a small amount of knowledge to set up. Crontab uses the system mail protocol which is the smtp server. Most things in Linux no longer require use of this tool as it is deprecated. This is not a requirement as many jobs can be forwarded to a log file as well from Cron.

Cron is a bit more simplistic to use, in Ubuntu, the Crontab file even has a basic outline of how to use the program. Crontab also offers a convenient Home Folder backup script in the Ubuntu version. Arch users are left with a clean file, they have to know the syntax to create their own jobs. One thing that Cron does have going for it though, it can use the Bash(Born Again Shell) or the regular shell. Cron has two modes, Root and User. The two separate modes will inact functions based on respective account privileges. If you are a standard user, you might find it difficult to run maintenance or backup tasks from Cron, whereas if you are a root, you will have an infinite amount of system resources to utilize at your disposal. Anacron uses the root account only to make changes to the system, such as; updates, apt-xapien, updatedb, mandb, logrotation.

Anacron has hours, days, weeks, months as its primary time settings, each of these is a separate folder that interacts on the system based on the time that anacron set it up to use when you installed your system. Both Crontab and Anacron are installed on most desktop systems and servers as of today, but this is not always the case, in Manjaro, Cronie has to be added in order to use the Crontab. In early versions of Cron, such as early Unix systems, Cron was thought of as a system service or Daemon, which was started via /etc/rc. At first, Cron didn’t have multi-user mode available on its own, it relied heavily on Unix systems for that, but in later versions, multi-user support was added.

Cron uses a pretty straightforward syntax. The syntax of Cron Table(Crontab), is as follows: ex. MM HH DOM M DOW Command to execute. In other words, you’d supply the minute(MM), followed by the hour(HH), followed by the day of month(DOM), followed by the month(M), followed by the day of the week(DOW). The minutes could be any number between 0 and 59, the hours could be anything between 0 and 23, the day of the month could be anything between 1 to 31, the month could be anything between 1 and 12, the day of the week could be any number between 0 and 6 with 7 being acceptable on some systems. The Command to execute could be anything the user writes, anything tailored for the user, and anything the system normally runs. For instance, if I wrote a script to update my hosts and named it hostsupdate, say I wanted to run it once, daily at around 5:30. I could simply run the following: 30 17 * * * /bin/bash /home/$USER/hostsupdate. Of course, at this time, that will run whatever is in the script, but if I wanted to check its work, I’d have to add the line cat /etc/hosts > hosts.log in the file. If I simply wanted to be alerted that it ran, I could type the cron job with > log1 and log1 would be created in my home directory. Another way that I haven’t tried, but might be possible, running two commands together in a crontab string. 30 17 * * * /bin/bash /home/$USER/hostsupdate && cat /etc /hosts > hosts.log. Note that for a script to update the hosts file, I need to run it using the BASH shell.

Depending upon your needs, Cron may be better than Anacron, however, if you just want to run a simple update procedure at the same time every day or week, Anacron is a great tool to use that is built into Linux systems already. Next week, I will probably be getting into /etc/rc and /etc/init.d along with system timers for Systemd. If you’d like to know more about these options for task scheduling in Linux, open a terminal and type man cron or man anacron. Also, there is a text based website that gives you similar information, link will be below.

Link for more: http://crontab.org

Leave a Reply