Follow

How to Add Custom Cron Jobs

Applies to platform: UTM all, 4i Edge all
Last updated: 30th August 2013

Warning

While adding custom cron jobs to any Linux box is a quite straightforward task, doing so on any Endian Appliance is an activity that should be achieved with the collaboration of the support team, as these kind of changes may impact on the Appliance's warranty and terms of support. In particular, adding cron jobs via the system crontab is strongly discouraged and it will not be discussed here, since even a small error in the crontab may prevent the system to work correctly.

In the Endian UTM Appliances, like in many other Linux distributions, there are special directories in which you can put your custom scripts that should routinely run on the system. Your custom cron job scripts will not be altered by other Endian UTM Appliance scripts or during the upgrade process, so you can safely save them in those directories.

Throughout this lesson we will use myscript as the name of a custom script that you want to install on your system.

Whenever you add a custom script to the Endian UTM Appliance, you should make sure that the scripts:

  • is executable. You can make sure the command is executable by issuing the command:
    root@endian:~# chmod 700 myscript
  • exits with errorcode 0. You can verify this by launching the script manually, then issuing the command
    root@endian:~# echo $?
    The output of this command is either 0 (the script has successfully exited) or an integer, which denotes an error.
  • must not contain a dot within the filename, so myscript.sh will not work.
  • must end within a short time. If this is not possible, consider putting the process in background, otherwise all other cronjobs will be enqueued and delayed.

The directories in which you can put your custom scripts are under /etc/ and their name begins with cron. The scripts stored in those directories will be run, one after the other, at a precisely precooked time, governed by the cron daemon, as follows:

  • /etc/cron.cyclic/ every 5 minutes (e.g., 3:05, 3:10, 3:15)
  • /etc/cron.minutely/ every minute (e.g., 2:01, 2:02, 2:03)
  • /etc/cron.hourly/ every hour, one minute after full hour (e.g., 2:01, 3:01, 4:01).
  • /etc/cron.daily/ every day at 1:25 AM.
  • /etc/cron.weekly/ on Sundays at 2:47 AM.
  • /etc/cron.monthly/ every first day of the month at 3:52 AM.

There is however a drawback of the cron daemon: When the Endian UTM Appliance is turned off, the scripts are not run. So for example, if you turn the appliance off every evening and boot it on the next morning, no nightly cron script will ever run!

Hence, if your Endian UTM Appliance is not intended to run continuously, but you need the guarantee that your scripts are run, you should use the anacron daemon instead. While doing the same tasks as cron, i.e., running periodic jobs, anacron performs one additional check on the scripts. Whenever a script is run, a timestamp for it is saved in a file. When the difference between the current system time and this timestamp is larger than the desired running frequency for the script, anacron immediately starts the script. For example, suppose the system is turned off for one night, from 8PM to 8AM. This implies that no cyclyc, minutely, and hourly cron script (and perhaps even daily, weekly and monthly) jobs are carried out. When the system is booted, the anacron daemon notices that none of these jobs has run and schedules their execution, taking also care that the next executions of the scripts take place regurarly.

The following directories are used to store periodic jobs whose execution is governed by anacron:

  • /etc/anacron.cyclic/ (every 5 minutes)
  • /etc/anacron.hourly/ (ever hour)
  • /etc/anacron.daily/ (every day)
  • /etc/anacron.weekly/ (every week)
  • /etc/anacron.monthly (every month)
Was this article helpful?
2 out of 2 found this helpful
Have more questions? Submit a request

Comments