Managing routine Linux tasks like backups and service restarts can be overwhelming. Cron jobs automate these processes, keeping your system running smoothly with minimal effort. This guide covers how to set up, use, and secure cron jobs for seamless automation.
The Cron Daemon (crond) is a built-in Linux utility that reads the cron table (crontab) and executes commands and scripts at prescheduled times and intervals. You can set up cron jobs in the crontab to automate routine activities.
The general syntax of a cron job usually follows the format below:
MIN HOUR DOM MON DOW CMD
Where:
The cron syntax may also contain special characters, including:
The crontab file contains the jobs that cron reads and executes. To open it, use:
crontab -e
This will open the crontab file in your default text editor. If this is your first time opening the crontab file, the system will create a new file.
Note: This applies only to user-specific crontabs. For system-wide cron jobs, such as those in "/etc/crontab" or "/etc/cron.d/," you must manually create and edit them with superuser privileges.
If you’re using Ubuntu 22.04, you’ll receive a prompt to choose your preferred text editor.
This step is when you’ll actually create the cron job following the basic format we covered in the previous section.
For example, if you want to run a script every day at 3 a.m., your cron job line will look like this:
0 3 * * * /path/to/your/script.sh
Remember to replace "/path/to/your/script.sh" with the actual path for the command you want to run. Also, you can create as many cron jobs in your crontab file as you want. Just make sure you create each job on a new line.
Note: You must ensure that your script has execute permissions so it can run. To do so, you can use:
chmod +x /path/to/script.sh
After creating your cron job, you can now save it and exit the text editor. The next steps will depend on the text editor you’re using:
Cron will then automatically apply the changes in the crontab file. However, scheduled jobs will not persist after a system reboot unless explicitly configured.
To ensure a job runs at system startup, use:
@reboot /path/to/script.sh
30 14 15 6 3 /path/to/script.sh
The above cron job will run on Wednesday, June 15, at 2:30 p.m.
0 9-17/2 * * 1-5 /path/to/script.sh
This cron job will run at the start of every second hour between 9 a.m. and 5 p.m., from Monday to Friday.
When you create a cron job, it’s a best practice to verify that you’ve successfully saved it in the crontab file:
crontab -l
This will list all of the cron jobs in your crontab file.
You may need to edit the details of an existing cron job, such as the time or day. To do so, open the crontab file using the command "crontab -e," find the line you want to edit, and make your preferred changes.
If you no longer need a cron job, find its line in the crontab file and edit it out. You can also delete all of the cron jobs in your crontab file at once using the command "crontab -r."
Note: This deletes all cron jobs for the user. It’s best to first list the existing jobs (crontab -l) before running this command.
Cron jobs will run only if the Cron Daemon can read the crontab file. Therefore, you must ensure it runs as expected. To do so, you can use the command below for systemd-based Linux distributions (Ubuntu, Debian, CentOS 7+):
systemctl status cron
If the output shows that "crond" isn’t running, you can restart it using:
systemctl start cron
For older systems use:
service cron status
Sometimes, you may want to record and review the output of your cron job. In such a case, you can redirect the output — standard (stdout) and error (stderr) — to a log file by adding ">> /var/log/script.log 2>&1" to the applicable line. This sends the output to "script.log."
However, logs can grow indefinitely and consume too much disk space over time. To prevent this, configure logrotate to automatically manage the log file:
sudo nano /etc/ logrotate.d/mycronlog /var/log/ script.log { weekly rotate 4 compress missingok notifempty }
By default, you can only run or edit cron jobs in your crontab file. However, if you have root privileges, you can perform the same actions for another user by using the command below:
sudo crontab -u username -e
If you need to schedule a cron job that applies to all users or the system, edit "/etc/crontab" or add a job to "/etc/cron.d/." Unlike user-specific crontabs, these files require an additional field specifying the user who will execute the job.
You must set the right paths and variables for your scripts to access. To do so, use:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin
Note: Some scripts also require explicit environment variables, so it’s best to define these at the top of the crontab file using:
SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin
It’s possible to run several commands in a particular order. You can leverage operators such as "&&" and ";" to do so.
For instance, the "&&" in this cron job means that command No. 2 will run after command No. 1 succeeds:
job 0 5 * * * command1 && command2
At some point, you'll encounter errors when running cron jobs. When this happens:
If you want an alternative that offers more flexibility than cron, you should consider systemd timers. These unit files are available for scheduling tasks in Linux.
Unlike cron, systemd timers execute missed jobs on boot if the system was off during the scheduled execution time, running as soon as the system starts up again. Additionally, systemd timers make it easier to monitor and troubleshoot your jobs because they log time units in the system journal.
Here’s how to go about creating a systemd timer.
Start by creating a service unit file with the name "mytask.service" in the "/etc/systemd/system/" directory using the following command:
sudo nano /etc/systemd/system/mytask.service
Once it's created, add the following:
[Unit] Description=Run my script [Service] ExecStart=/path/to/script.sh
In the same directory, create another file — the timer unit file — and name it "mytask.timer." This dictates when the service should run:
sudo nano /etc/systemd/system/mytask.timer
Then, add:
[Unit] Description=Run my task every day at 2 AM [Timer] OnCalendar=*-*-* 02:00:00 Persistent=true [Install] WantedBy=timers.target
Where:
You must activate the timer so that it starts automatically after a system boot:
systemctl enable mytask.timer systemctl start mytask.timer
Automating tasks with cron jobs is essential for system efficiency, but without proper access controls and security, these scheduled tasks can become vulnerabilities. Delinea’s Zero Trust Privileged Access Management (PAM) solution ensures that cron jobs and other automation processes remain secure and controlled by:
With Delinea, you control who can create, modify, and execute cron jobs—ensuring your automation remains efficient without exposing your systems to unnecessary risks. Book a demo today to see how Delinea can help secure your Linux automation workflows.
crontab -e MIN HOUR DOM MON DOW CMD 0 3 * * * /path/to/script.sh /path/to/script.sh sudo run-parts /etc/cron.hourly grep CRON /var/log/syslog # For Debian-based systems
grep CRON /var/log/cron # For Red Hat-based systemsA basic cron job follows this structure:
* * * * * /path/to/script.sh
Example: Run a script every day at midnight:
0 0 * * * /path/to/script.sh
crontab -l crontab -e crontab -r systemctl status cron systemctl start cron