The Linux Page

Writing a CRON job in /etc/cron.*/job

Setup

When writing a CRON job script that you want to install under /etc/cron.*/job-name you must remember to apply the following steps:

1. Write the script and test it as root

2. Make sure to give it execution permission, usually 755

3. The ownership is expected to be root:root

4. The filename cannot include a period or the file it completely ignored

5. The script MUST start with #!/bin/sh or an equivalent (i.e. #!/bin/bash works too.)

6. Use full paths for most everything1

Behavior

If the job doesn't happen, it's probably because the file does not have execution permissions. In most modern Linux the script filename will appear in a different color if the execution flags are set or not set.

A filename that includes a period (i.e. my.job) is totally ignored, that means you do not get any errors when it fails running.

Other errors will generally appear in your root user email.

Common error

run-parts: failed to exec /etc/cron.daily/my-job: Exec format error

This error occurs when the script doesn't start with an interpreter (i.e. #!/bin/sh)

 

  • 1. Remember that the cron environment is minimal, you generally will have PATH defined and not much more.