Operations 9 min read

Understanding and Using crontab for Task Scheduling on Linux

This guide explains how to install, start, and stop the crontab service, configure global and user-specific cron directories, write crontab entries with proper syntax, and apply advanced techniques such as output redirection, nohup, background execution, and date-based log naming for reliable Linux task automation.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Understanding and Using crontab for Task Scheduling on Linux

The crontab command is used to schedule periodic commands by storing them in a crontab file, which the system reads and executes at defined times, making it ideal for offline tasks like daily data updates.

Most Linux distributions include the crontab service; if missing, install it with:

# vixie-cron is the main cron program
yum -y install vixie-cron
yum -y install crontabs

Control the service with the following commands:

service crond start      # start service
service crond stop       # stop service
service crond restart    # restart service
service crond reload     # reload configuration
service crond status     # check status
# enable at boot
chkconfig --level 345 crond on

Global cron configuration files are located in directories such as cron.d/ , cron.daily/ , cron.hourly/ , cron.weekly/ , cron.monthly/ , and the user’s crontab file.

Each user can edit their personal crontab with crontab -e ; the file is saved under /var/spool/cron/ named after the user. The cron daemon checks /var/spool/cron , /etc/crontab , and /etc/cron.d for jobs.

A crontab line consists of six fields: minute, hour, day, month, week, and command. Special symbols can be used:

* – wildcard (all values)

, – list separator (e.g., 1,2,5)

- – range (e.g., 2-6)

/ – step interval (e.g., */10 for every ten minutes)

Common scheduling examples:

00 05 * * * – every day at 05:00

20 12 1,10,20 * * – on the 1st, 10th, and 20th of each month at 12:20

0 */2 * * * – every two hours

To run a script at 23:58 daily and log its output, add to the crontab:

58 23 * * * sh /home/work/update.sh >> /home/work/log/update.log 2>&1

Redirecting both stdout and stderr ensures errors are captured in the same log file. Standard file descriptors are:

Name

Type

FD

Operator

stdin

standard input

0

<, <<

stdout

standard output

1

>, >>

stderr

standard error

2

2>, 2>>

Using nohup before the command ignores input, and appending & runs the job in the background:

58 23 * * * nohup sh /home/work/update.sh >> /home/work/log/update.log 2>&1 &

For daily log files, embed the date in the filename using the date command, remembering to escape the percent sign inside crontab:

58 23 * * * nohup sh /home/work/update.sh >> /home/work/log/`date +\%Y\%m\%d`.log 2>&1 &

Running the same command directly in a shell works without escaping, but the escaped version will fail inside crontab.

For further reading, see the “Linux Knowledge Encyclopedia”.

Task SchedulingLinuxSystem Administrationcrontabcron jobs
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.