CRON JOB

Introduction

Cron is a time-based job scheduler in Unix-like operating systems.

Users can schedule scripts or commands to run at specific intervals using the cron daemon. It is commonly used for system maintenance, automation, and recurring tasks.

Cron Syntax

A cron job follows a specific syntax consisting of five time fields followed by the command to be executed:

* * * * * /path/to/command

- - - - -

| | | | |

| | | | +---- Day of the week (0 - 7) [0 and 7 represent Sunday]

| | | +------ Month (1 - 12)

| | +-------- Day of the month (1 - 31)

| +---------- Hour (0 - 23)

+------------ Minute (0 - 59)

Special Characters

  1. * → Any value (wildcard, runs every possible value)
  2. , → List of values (e.g., 1,5,10 runs on days 1, 5, and 10)
  3. - → Range of values (e.g., 1-5 runs from day 1 to day 5)
  4. / → Step values (e.g., */5 means every 5 minutes)

Common Cron Jobs Examples

  1. Run a script every day at midnight:
  2. Run a command every Monday at 8 AM:
  3. Run a backup job every 6 hours:
  4. Restart a service every Sunday at 3 AM:
  5. Run a script on the first day of every month at 5 AM:

0 0 * * * /path/to/script.sh

0 8 * * 1 /path/to/command

0 */6 * * * /path/to/backup.sh

0 3 * * 0 systemctl restart apache2

0 5 1 * * /path/to/monthly_task.sh

Managing Cron Jobs

Adding and Editing Cron Jobs

  1. Edit cron jobs:

crontab -e

This opens the user’s crontab file in the default editor.

Viewing Scheduled Jobs

  1. List cron jobs for the current user:
  2. List cron jobs for a specific user (requires sudo access):

crontab -l

sudo crontab -u username -l

Removing Cron Jobs

  1. Remove all cron jobs for the current user:
  2. Remove a specific cron job by editing the crontab (crontab -e) and deleting the relevant line.

crontab -r

Logging and Debugging

  1. Check cron logs to verify if jobs are executing:
  2. Redirect output to a log file for debugging:
  3. Check user-specific cron logs:

cat /var/log/syslog | grep CRON

0 2 * * * /path/to/script.sh >> /var/log/script.log 2>&1

journalctl -u cron --no-pager

Environment Considerations

Cron jobs run with a minimal environment. Unlike interactive shell sessions, they do not inherit user-specific configurations. This can lead to issues where commands work in the terminal but fail in cron.

Using Environment Variables

To ensure a consistent execution environment:

PATH=/usr/local/bin:/usr/bin:/bin

0 1 * * * /path/to/script.sh

Alternatively, define environment variables inside the script:

#!/bin/bash

export PATH=/usr/local/bin:/usr/bin:/bin

python3 /path/to/script.py

Using Absolute Paths

Always use absolute paths when specifying files or scripts:

/home/user/scripts/myscript.sh

Troubleshooting

1. Cron Job Not Running?

  1. Ensure the script has execution permissions:
  2. Use absolute paths in the script.
  3. Check if the cron service is running:
  4. Ensure the script works manually:
  5. If the job uses sudo, try adding it explicitly:

chmod +x /path/to/script.sh

systemctl status cron

/path/to/script.sh

0 3 * * * sudo /path/to/script.sh

2. Checking User Privileges

Cron jobs may fail due to permission issues. Ensure the correct user is running the job:

whoami

If root privileges are needed, add the job under the root user’s crontab:

sudo crontab -e

3. Using Debugging Techniques

  1. Run cron jobs with verbose logging:
  2. Check email notifications for cron job failures:

bash -x /path/to/script.sh > /tmp/debug.log 2>&1

mail

(Cron sends output to the user’s mail by default.)

Conclusion

Cron is a powerful tool for task automation. By understanding its syntax, logging outputs, and troubleshooting issues, users can ensure reliable execution of scheduled tasks. 

Follow Us On

Registered Office

CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

Parent Office

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

  +81 03-3496-1571
AboutUs

CHG IT Consultancy Pvt. Ltd. is a subsidiary of CIC Holdings Co. Ltd. Japan. Our company is focused on IT related solutions to reap the benefits of global popularity of Software Industry.

Registered Office
CHG IT CONSULTANCY PVT LTD

STPI Technology Incubation Centre,
2nd Floor, No.5, Rajiv Gandhi Salai,
Taramani, Chennai – 600113,
Tamil Nadu, INDIA

CIC Corporation

2-16-4 Dogenzaka, Shibuya-ku,
Nomura Real Estate,
Shibuya Dogenzaka Building,
Tokyo 150-0043, JAPAN

+81 03-3496-1571