CRON & CRONTAB: automatically run your commands when you want

From Wikipedia:
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals.

Normally, CRONIE is installed by default in Linux-Based OS. However, CRONIE is not enable by default in new ARCH LINUX INSTALLATION. This can be checked by digiting in your terminal:

$ systemctl is-enabled cronie

Cronie systemd service must be started and enabled via systemctl by root

$ sudo systemctl start cronie
$ sudo systemctl enable cronie

Now, you have to edit your crontab via:

$ crontab -e

# * * * * *  command to execute
# | | | | |
# | | | | |
# | | | | |
# | | | | 0--- day of week (0 - 6) (0 is Sunday, 6 is Saturday)
# | | | 0------- month (1 - 12)
# | | 0----------- day of month (1 - 31)
# | 0--------------- hour (0 - 23)
# 0------------------- min (0 - 59)

For example to run a command every Monday at 8:00 AM:

$ crontab -e

0 8 * * 1  command to execute

In this way you can decide the exact time of the command execution. If you want to run a command at every defined number of minutes ( or hours, day, etc. ) you have to write */ before the chosen number (in the example below the command is run every 5 minutes).

$ crontab -e

*/5 * * * *  command to execute


If you want to run a command at 09:00 in the first day of the month, you have to set the third option to 1, as follow

$ crontab -e

0 9 1 * *  command to execute

But if you have to run a command or a script at 09:00 in the last day of the month, what can you do? The last day of the month could be 30, 31, 28 or sometimes even 29. So, you can write as follow:

$ crontab -e

0 9 28-31 * *  command to execute

the command will be executed at the 28-th, 29-th, 30-th and 31-th day of the month. How is possible to choose which is the last day between the four days before cited?
Only one thing is always true about the last day of the month: the following day is the first! Thus, once you have verified that tomorrow is the first day of the month, you are sure that today is the last.

The bash command

date +%d

will give you the current date as a two character number. To get the number of tomorrow:

date +%d -d tomorrow

If that mastches “01”, tomorrow is the first so today is the last day of the month

[ "$(date +%d -d tomorrow)" = "01" ] """" execute_command

The square brackets will result in either true or false. If the command between square brackets comes back false, the second command won’t be run. So, only when the command between square brackets come back true, the second command will be run.

The complete crontab will result:

$ crontab -e

0 9 28-31 * *  [ "$(date +%d -d tomorrow)" = "01" ] """" execute_command

About Francesco Serafin

I am a PhD student at the Department of Civil, Environmental and Mechanical Engineering at the University of Trento. My two greatest loves: Computer Science and Water (take three with my Lenovo!:D). As a result, the aim of my life is to make both passions coexist. My gpg public key available at

Leave a Reply

Your email address will not be published.