Creating a cron Job

Schedule an automated task as a cron job by editing a user's crontab file.

Any user can create a cron job, but the location of the job definition depends on the user's privileges.

An administrator who signs in as root creates jobs that are stored in /etc/crontab. Jobs in /etc/crontab are run as root, unless the job definition specifies a different user.

Any user with administrator privileges can create system-wide cron jobs. The jobs are stored in /etc/crontab.d/ and the job file names include the administrator's username.

A regular user can also create jobs that are stored in /etc/crontab.d/. The job file name also includes that user's name.

To create or edit a crontab file as a signed in user, such as jsmith, follow these steps:

  1. Sign in to the system with that user account, for example jsmith.
  2. Edit crontab with a text editor by running the following command:
    crontab -e

    The crontab command uses the EDITOR environment variable to decide which text editor to use for creating or editing cron jobs in the user's crontab file. To select a specific text editor, such as vim or nano, you can set this environment variable in the shell configuration. To temporarily use a different text editor, run the following command:

    env EDITOR=text-editor crontab -e
  3. When the editor opens, create a cron job, following the format described in cron Table Fields Reference.

    For example, to define a backup job that runs every 15 minutes and starts a script called mybackup.sh in that user's home directory, add the following line:

    15 * * * * /home/jsmith/mybackup.sh
  4. Save the file and exit. For the user jsmith, the file is saved as /var/spool/cron/jsmith.

The cron job is now active. To view and verify the contents of the new cron job, run the following command:

15 * * * * /home/jsmith/mybackup.sh

To delete the signed in user's crontab file, run the following command:

crontab -r 

For more information, see the crontab(5) manual page.