- Unix Commands Reference
- Unix Commands - Home
crond Command in Linux
crond is a system daemon (background service) that continuously runs on your Linux system. It's typically named cron or crond. crond's primary function is to execute scheduled tasks defined in crontabs (cron tables). It periodically checks these crontabs for entries that match the current time and date based on the cron expressions they contain. crond itself doesn't have any user-facing options or commands for you to interact with directly. It silently executes tasks in the background based on the crontab entries.
The crond command is an essential part of the Linux operating system, used for scheduling tasks to be executed at predetermined times. This functionality is incredibly useful for system administrators and users who need to automate repetitive tasks or ensure that certain commands are run at specific intervals.
Table of Contents
Here is a comprehensive guide to the options available with the crond command −
- Understanding crond Command in Linux
- Install crond Command in Linux
- How to Use crond Command in Linux?
- Examples of crond Command in Linux
- Alternatives of crond Command in Linux
Understanding crond Command in Linux
crond works behind the scenes, reading crontab entries and executing the defined tasks at the scheduled times based on the cron expressions. crond as a diligent assistant who constantly checks a task list (crontab) with specific instructions (cron expressions) on what to do and when. When a task's scheduled time arrives based on the cron expression, crond automatically executes the command or script associated with that task.
crond is the daemon that executes cron jobs at specified times. These jobs are defined in `crontab` files, which list the commands and the times at which they should run. The `crond` command and the cron scheduling system are powerful tools for automating tasks on a Linux system. By understanding how to set up and manage cron jobs, users can save time and ensure important tasks are not forgotten.
Prerequisite: Install crond Command in Linux
In most Linux distributions, the crond daemon (cron daemon) is pre-installed. There's typically no separate installation required for crond. crond is a daemon, a background service that runs continuously on your system. It's not a traditional command you execute directly.
This command checks for crontab (the tool to manage cron jobs) and displays its version information if found. If you see output with a version number, crond (the daemon) is installed.
In the rare case where crond is not pre-installed, you can install it using the package manager for your specific distribution −
Debian/Ubuntu −
sudo apt-get update sudo apt-get install cron
Red Hat/CentOS/Fedora −
sudo yum update sudo yum install cronie
OpenSUSE −
sudo zypper update sudo zypper install cron
Once you've verified or installed crond, you can start using crontab to manage your scheduled tasks as described previously.
How to Use crond Command in Linux?
The crond command is a staple in the Linux world, a powerful tool that allows users to run commands or scripts automatically at specified times and intervals. It's a daemon that runs in the background and checks for scheduled tasks, executing them when their set times are reached. This utility is incredibly useful for system administrators and users who want to automate repetitive tasks, such as backups, system updates, or custom scripts for maintenance and monitoring.
The cron command itself has limited options, as it primarily functions as a launcher for the crontab utility, which manages cron jobs.
Syntax of crontab Entries
A crontab file contains lines of cron jobs, and each job is represented by a single line. The syntax for a cron job is as follows −
* * * * * command_to_execute
This consists of six fields separated by spaces. The first five are time fields that specify when the command should run, and the last field is the command itself.
- Minute − Values range from 0 to 59.
- Hour − Values range from 0 to 23.
- Day of the Month − Values range from 1 to 31.
- Month − Values range from 1 to 12, or you can use the first three letters of the month's name.
- Day of the Week − Values range from 0 to 6 (Sunday to Saturday), or you can use the first three letters of the day's name.
Special Characters in Time Fields
- Asterisk (*) − Represents all possible values for a field.
- Comma (,) − Specifies a list of values.
- Hyphen (-) − Defines a range of values.
- Slash (/) − Specifies a step value.
While not technically cron options, these commands are helpful for managing cron jobs. Here's a breakdown of the available cron command options −
Options | Description |
---|---|
crontab -l | Lists the cron jobs (entries) in your crontab file. |
crontab -e | Opens the crontab file for editing in your default text editor. If you don't have a crontab file yet, it will be created for you. |
crontab -i | Starts an interactive shell where you can directly create or edit cron job entries. |
crontab -r | Removes all your cron job entries from the crontab file. |
crontab -f file | Specifies an alternative crontab file to edit or list entries from (usually not needed for most users). |
Additional Cron Commands: | |
crontab -v | Check the installed crontab version. |
service cron status or systemctl status cron | Check the status of the cron daemon (running, stopped, etc.). |
service cron start or systemctl start cron | Starts the cron daemon if it's stopped. |
service cron stop (or systemctl stop cron) | Stops the cron daemon (use it with caution as it will prevent scheduled jobs from running). |
Examples of crond Command in Linux
Now, let’s go through some examples to better understand the working of the crond command in Linux −
- Run a script every minute
- Run a script hourly
- Run a script at 3 AM every day
- Backup files daily at midnight
- Update software packages weekly on Sundays at 2 AM
- Run a script at 4:30 PM on the first day of each month
- Run a script every 15 minutes
- Run a script at 2 AM on weekdays (Monday to Friday)
- Run a script at 8 PM on the last day of February
- Run a script at 10 AM on the second Sunday of April
- View your crontab file
- Edit crontab File
- Remove crontab file
Run a script every minute
The line * * * * * script.sh represents a cron expression that defines when and what command to run using cron. Let’s run a command every minute −
* * * * * script.sh
This is the actual command or script that will be executed according to the cron expression. Replace this with the actual path to your script on the system.
Run a script hourly
This cron expression runs the script /path/to/your/script.sh every minute, because * in each field represents all possible values. In other words, the script runs continuously −
0 * * * * script.sh
Run a script at 3 AM every day
This cron expression runs the script at /path/to/script.sh at 3:00 AM every day. Here, 0: Minute (0 for the 0th minute, meaning on the hour). 3 * * * * Runs the script at 3 AM (* for every hour, day of month, month, and weekday) −
0 3 * * * script.sh
Backup files daily at midnight
This cron expression runs a script at midnight (0 0) every day (* * *) to create a compressed backup archive (.tar.gz) named with the current date using tar.
0 0 * * * /bin/tar -zcvf /home/user/backup_$(date +%Y-%m-%d).tar.gz /home/user/important_files
This example uses tar to create a compressed backup archive named with the current date and stores it in /home/user/backup.
Update software packages weekly on Sundays at 2 AM
This cron expression instructs your system to run the command apt update && apt upgrade (update package lists and then upgrade packages) at exactly 2:00 AM every Sunday −
0 2 * * 0 apt update && apt upgrade
This assumes you use apt for package management.
Run a script at 4:30 PM on the first day of each month
This cron expression runs script.sh at 4:30 PM (16:30) on the 1st day of every month.
30 16 1 * * script.sh
Run a script every 15 minutes
This cron expression (*/15 * * * * /path/to/script.sh) runs the script at /path/to/script.sh every 15 minutes because */15 in the minute field specifies running every 15th minute (0, 15, 30, 45). The remaining *s indicate all other time units (every hour, day, month, weekday).
*/15 * * * * script.sh
Run a script at 2 AM on weekdays (Monday to Friday)
This cron expression runs /path/to/script.sh at exactly 2:00 AM every weekday (Monday-Friday) −
0 2 * * 1-5 script.sh
Note − Remember to replace command or script.sh with the actual paths to your commands or scripts.
Run a script at 8 PM on the last day of February
This cron expression runs the script script.sh only once at 8:20 PM on February 28th of every year −
0 20 28 2 * script.sh
Note − This assumes February has 28 days.
Run a script at 10 AM on the second Sunday of April
This cron expression runs /path/to/script.sh only once a month, at 12:00 AM (midnight) on the first Friday of the month. It checks the current week number (using date +%U) and executes the script only if it's equal to 1 (first week) −
0 10 * 4 0 [ $(date +\%U) -eq 1 ] && script.sh
This uses a combination of cron and a shell test to determine the second Sunday.
By effectively utilizing cron, you can automate various tasks in Linux, enhancing system efficiency and reducing manual workload.
View your crontab file
Managing cron Jobs To view your crontab file, use −
crontab -l is a command in Linux that displays a list of all the automated tasks (cron jobs) you have currently scheduled in your crontab file, in just two lines. It essentially shows you what commands will run and when.
crontab -l
Edit crontab File
To edit your crontab file, use crontab -e opens the crontab file, which stores your scheduled tasks (cron jobs), for editing in your default text editor. This allows you to define when and what commands to run automatically on your Linux system.
crontab -e
Remove crontab file
To remove your crontab file, use the command crontab -r removes all your scheduled tasks (cron jobs) stored in the crontab file, essentially clearing your automated task list in two steps -r: Specifies the "remove" option. crontab − Launches the crontab utility for managing cron jobs.
crontab -r
Alternatives of crond Command in Linux
In the Linux environment, the crond command is a time-based job scheduler that enables users to automate system tasks. However, there are several alternatives to crond that offer different features and functionalities.
Anacron
Anacron is a popular choice for systems that do not run 24/7, as it can execute tasks when the system is up, regardless of the scheduled time. Cronie includes the anacron package and provides both synchronous and asynchronous job scheduling.
Fcron
Fcron is another alternative that combines the best features of cron and anacron, suitable for systems that may not be running all the time. Systemd timers are part of the systemd suite and can trigger events at specified intervals, offering more flexibility than traditional cron jobs.
at command
For those looking for advanced workload automation capabilities, tools like ActiveBatch Workload Automation and Redwood RunMyJobs may be considered. Each of these alternatives has its own set of advantages and can be chosen based on the specific requirements of the system and the tasks to be automated. It's important to evaluate the features of each to determine the best fit for your automation needs.
Conclusion
crond can greatly enhance your ability to manage tasks on a Linux system. Each field in a cron job represents a different unit of time, and by combining them, you can tailor job execution to fit nearly any schedule.
While crond is powerful, it's essential to ensure that the tasks you schedule do not overlap or cause resource contention on your system. Proper planning and testing are key to using crond effectively and efficiently.
For more detailed explanations and examples, you can refer to resources. These sites offer in-depth guides and additional examples to help you master the crond command and make the most of its capabilities for task automation in Linux.