Mastering Job Scheduling with Crontab on Linux

Crontab enables users to schedule commands or scripts at predefined itnervals—minutes, hours, days, months, or weekdays. It is widely used for tasks like log rotation, backups, and system maintenance.

Before using crontab, ensure the cron daemon is running:

systemctl start cron        # Start the service
systemctl enable cron       # Enable at boot
systemctl status cron       # Check status

Command Syntax

crontab [-u user] file
crontab [-u user] { -e | -l | -r }

Options

  • -u user: Operate on the specified user’s crontab (requires privileges).
  • file: Use the given file as the crontab content and install it. Without a file, crontab reads from standard input.
  • -e: Edit the current user’s crontab (or the target user’s with -u).
  • -l: List the crontab entries.
  • -r: Remove the crontab. Combine with -i for an interactive prompt before deletion.

Time Field Specification

Field Description Allowed Values
minute minute of the hour 0-59
hour hour of the day 0-23
day day of month 1-31
month month of year 1-12
weekday day of week 0-6 (0=Sunday)
command command to execute any shell command

Each field can be a single value, a comma‑separated list, a range (e.g., 8-11), a step value (*/2 means every two units), or * for "every".

Creating and Editing Crontabs

To avoid editor confusion, set the EDITOR variable before your first crontab -e:

export EDITOR=vim   # or nano, emacs, etc.

Now create a new crontab. For demonstration, write a file called mycron with the following content:

# Log the current time to the console every 15 minutes from 6 PM to 6 AM
0,15,30,45 18-23,0-6 * * * /bin/echo "$(date)" >> /dev/console

Insttall it:

crontab mycron

Cron copies the file into /var/spool/cron/crontabs/ (the actual location may vary). You can verify with:

crontab -l

To back up the crontab before editing:

crontab -l > ~/cron_backup.txt

Modify the schedule interactively:

crontab -e

When you save and exit the editor, cron performs basic validation. For instance, adding the line below deletes core files at 03:30 on the 1st, 7th, 14th, 21st, and 26th of every month:

30 3 1,7,14,21,26 * * /usr/bin/find /tmp -name 'core' -exec /bin/rm {} \;

Remove a crontab entirely:

crontab -r          # no confirmation
crontab -ri         # asks before removal

Practical Scheduling Examples

  • Every minute
    * * * * * /path/to/script.sh

  • At minutes 3 and 15 of every hour
    3,15 * * * * /usr/bin/some-command

  • At 3:30 AM on the 1st, 7th, 14th, 21st, 26th
    30 3 1,7,14,21,26 * * /usr/local/bin/cleanup.sh

  • Every two days between 8 AM and 11 AM at minutes 3 and 15
    3,15 8-11 */2 * * /usr/local/bin/report.sh

  • Every Monday at 21:30
    30 21 * * 1 /etc/init.d/smb restart

  • Every Saturday and Sunday at 01:10
    10 1 * * 6,0 /etc/init.d/smb restart

  • Every 30 minutes from 18:00 to 23:00
    0,30 18-23 * * * /etc/init.d/smb restart

  • Every hour
    0 */1 * * * /etc/init.d/smb restart

  • Every hour between 23:00 and 07:00
    0 23-7 * * * /etc/init.d/smb restart

Important Considerations

Environment Variables

Cron runs with a minimal environment. If a script works manually but fails in cron, missing environment variables are often the cause. Always:

  • Use absolute paths for executables and files.
  • Source necessary profiles or set variables explicitly at the top of your script or within the cron line.

Example script header:

#!/bin/bash
source /etc/profile
export APP_HOME=/opt/myapp
/opt/myapp/bin/start.sh

Or source directly in the crontab entry:

0 * * * * . /etc/profile; /var/www/java/bin/restart_audit.sh

Handling Output and Logs

By default, cron emails the output of each job to the user. Over time this can fill the mailbox. To discard all output:

0 */3 * * * /usr/local/bin/apachectl restart >/dev/null 2>&1

The >/dev/null 2>&1 redirects both stdout and stderr to the null device, suppressing messages.

User vs. System‑wide Crontabs

  • User crontabs are managed with crontab -e and stored per‑user.
  • System‑wide jobs are placed in /etc/crontab or in /etc/cron.d/. The format of /etc/crontab includes an extra field—the user who runs the command—after the time fields.

For tasks that require root privileges, you can either use sudo crontab -e or add an entry to /etc/crontab. Note that commands like reboot must often be defined in a system crontab to work correctly.

Tags: crontab Linux scheduling cron automation

Posted on Fri, 15 May 2026 10:36:38 +0000 by Tensing