
- Role-based, attribute-based, & just-in-time access to infrastructure
- Connect any person or service to any infrastructure, anywhere
- Logging like you've never seen

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.
Understanding Cron Jobs in Linux
What Is Cron?
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.
Cron Syntax Basics
The general syntax of a cron job usually follows the format below:
MIN HOUR DOM MON DOW CMD
Where:
- "MIN" represents the number of minutes the job runs. It ranges from zero to 59.
- "HOUR" represents the number of hours the job runs and ranges from zero to 23.
- "DOM" represents the day of the month to run the job and ranges from one to 31.
- "MON" represents the month to run the job and ranges from one to 12.
- "DOW" represents the day of the week to run the job and ranges from zero to seven. Some Linux distributions treat both zero and seven as Sunday, while others treat only zero as Sunday.
- "CMD" represents the actual command or job that will run at the stipulated time.
The cron syntax may also contain special characters, including:
- *: The asterisk represents all of the possible values for that field.
- ,: The comma specifies multiple values.
- -: The hyphen represents a range.
- /: The slash defines how often something should happen within a range.
- ?: This is used as a "no specific value" placeholder and is only supported by some cron implementations, such as Quartz Scheduler.
How to Create a Cron Job in Linux: Set Up In 3 Steps
1. Open the Crontab File
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.
2. Define the Cron Job
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
3. Save and Exit
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:
- For vim, press “Esc,” type “:wq,” and hit “Enter.”
- For nano, you’ll press "Esc," then type ":wq," and finally hit "Enter."
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
Linux Cron Job Examples
Example 1
30 14 15 6 3 /path/to/script.sh
The above cron job will run on Wednesday, June 15, at 2:30 p.m.
Example 2
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.
Managing and Viewing Cron Jobs
List Existing Cron Jobs
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.
Edit an Existing Cron Job
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.
Remove a Cron Job
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.
Check If the Cron Daemon Is Running
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
Advanced Cron Job Configurations
Redirecting the Output to a Log File
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 }
Running Cron Jobs as a Specific User
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.
Using Environment Variables
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
Chaining Multiple Commands
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
Debugging Cron Jobs
At some point, you'll encounter errors when running cron jobs. When this happens:
- Check your logs for errors using "grep CRON /var/log/syslog" for Debian-based systems. For Red Hat-based systems, use “grep CRON /var/log/cron.”
- Reduce your chances of running into errors by using the full paths for commands. For instance, instead of "python3 script.py," you can use "/usr/bin/python3 /path/to/script.py."
- You can also test your script by running it manually.
Alternative to Cron: Systemd Timers
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.
How to Create a Basic Systemd Timer
Here’s how to go about creating a systemd timer.
1. Create a Service File
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
2. Create a Timer File
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:
- "OnCalendar" defines the schedule.
- "Persistent" ensures the job runs after reboot if the system is off at the scheduled time.
- "WantedBy" starts the timer after initializing the timer service.
3. Enable and Start the Timer
You must activate the timer so that it starts automatically after a system boot:
systemctl enable mytask.timer systemctl start mytask.timer
StrongDM's Approach to Secure and Streamline Cron Jobs
Automating tasks with cron jobs is essential for system efficiency, but without proper access controls and security, these scheduled tasks can become vulnerabilities. StrongDM’s Zero Trust Privileged Access Management (PAM) solution ensures that cron jobs and other automation processes remain secure and controlled by:
- Granular Access Management: Restrict cron job execution to authorized users and predefined roles.
- Audit Logging & Visibility: Maintain complete oversight of every automated job to detect anomalies and unauthorized modifications.
- Secure Secrets & Credentials: Prevent plaintext credentials in scripts by integrating with secure vaults for authentication.
- Real-Time Monitoring: Get alerts on failed or suspicious cron job activity to prevent unauthorized task execution.
- Seamless Integration: Automate tasks without exposing critical system configurations by enforcing access policies dynamically.
With StrongDM, 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 StrongDM can help secure your Linux automation workflows.
Frequently Asked Questions
How to make a cron job?
- Open the crontab file:
crontab -e
- Add a cron job in the format:
MIN HOUR DOM MON DOW CMD
- Example: Run a script daily at 3 AM:
0 3 * * * /path/to/script.sh
- Save and exit the editor.
How do I run a cron job command?
- Manually execute a cron job by running the command in your terminal:
/path/to/script.sh
- Trigger all scheduled cron jobs immediately (not recommended in production):
sudo run-parts /etc/cron.hourly
- Check logs to confirm cron jobs ran:
grep CRON /var/log/syslog # For Debian-based systems
grep CRON /var/log/cron # For Red Hat-based systems
What is the basic cron job?
A basic cron job follows this structure:
* * * * * /path/to/script.sh
Example: Run a script every day at midnight:
0 0 * * * /path/to/script.sh
How to apply crontab in Linux?
- List all existing cron jobs:
crontab -l
- Edit the crontab file:
crontab -e
- Delete all cron jobs for the user:
crontab -r
- Ensure cron service is running:
systemctl status cron
If not running, start it:systemctl start cron
About the Author
StrongDM Team, Zero Trust Privileged Access Management (PAM), the StrongDM team is building and delivering a Zero Trust Privileged Access Management (PAM), which delivers unparalleled precision in dynamic privileged action control for any type of infrastructure. The frustration-free access stops unsanctioned actions while ensuring continuous compliance.
You May Also Like




