Cron, a Linux utility, schedules and automates tasks on a server. This widely used feature lets your operating system run specific commands or scripts at a specified date or time. You can set cron jobs on a Linux machine by following a few simple steps.
Why is cron used?
Cron jobs simplify workflow and reduce human interaction for day-to-day tasks. By integrating shell scripts and cron, we can automate many tasks like server backups, server patching, log backups and purging, log rotations, etc.
How to use cron?

Steps to set cron:
- Create a script or command that you want to schedule.
- Use the
crontab -e
command to open the cron editor. - Use the absolute path of the script or command instead of using it directly.
- Set cron timing like the above image.
- Add script after scheduled time and exit editor using (Esc then :wq).
Tip: Add redirections to a log file to monitor executions of scripts or commands via cron.
Examples of cron.
00 */1 * * * logrotate -f /etc/logrotate.d/iptraf-ng >/dev/null 2>&1
In the above example, the cron will run every 1 hour to rotate iptraf-ng logs using the logrotate service.
00
: At minute 0*/1
: Every 1 hour*
: Every day of the month*
: Every month*
: Every day of the week
Most used Crontab options to use:
-u
: Specify user-based cron.-e
: Edit user cron.-l
: List all existing cron.-r
: Remove all crons for a specific user.-i
: Prompt before deleting cron.
Monitor and debug outputs:
By default, cron sends output to the user’s email address, but by using redirections like >
and >>
we can maintain a log file to monitor and debug outputs of cron jobs.
Examples:
Example 1:
00 09 * * * /path/of/command > /var/log/command_debug.log
In this example, the cron is redirecting standard output inside /var/log/command_debug.log instead of sending it to the user’s email ID.
Example 2:
00 09 * * * /path/of/command 2> /var/log/command_debug.log
In this example, the cron is only redirecting error output inside /var/log/command_debug.log.
Example 3:
00 09 * * * /path/of/command 1> /var/log/command_debug.log
In this example, the cron is only redirecting non-error or standard output inside /var/log/command_debug.log using file descriptor.
Example 4:
00 09 * * * /path/of/command &> /var/log/command_debug.log
In this example, the cron is redirecting all error and standard output inside /var/log/command_debug.log.
Suggested tools for cron jobs:
Conclusion:
We learn how to set cronjobs in Linux is essential for automating routine tasks such as log rotation, backups, and system maintenance. By mastering the cron format, using the crontab -e command to edit your schedule, and providing the correct script paths and timing, you can ensure your commands run automatically at the right times. Remember to use output redirection (>
, 2>
, &>
) to monitor and debug cron jobs through log files. With these basics, cron becomes a valuable tool to efficiently manage and automate tasks on any Linux server.