How to Schedule Linux Commands with cron and systemd
In this article
Yes, you can schedule Linux commands to run automatically at specific times or intervals using cron or systemd timers. Cron is simpler and more common; systemd timers offer more flexibility and better logging. You should schedule a command when you need it to run repeatedly without manual intervention—for example, nightly backups, daily log rotation, or weekly system updates.
What You'll Need
- A Linux system with
croninstalled (most distributions include it by default; if not, install thecronpackage via your package manager). - Basic familiarity with the command line and editing files with a text editor like
nanoorvim. - For systemd timers, a system running systemd (most modern distributions).
Step-by-Step
Using Cron
- Open your user's crontab file by running
crontab -ein the terminal. If it's your first time, it will ask you to choose an editor (nano is easiest for beginners). - Understand the cron syntax: each line has five time fields followed by the command. The fields are: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7, where 0 and 7 are Sunday). Use
*to mean "every". For example,0 2 * * *means at 2:00 AM every day. - Add a schedule line at the bottom of the file. For instance, to run a backup script at 3:30 AM every Monday, add:
30 3 * * 1 /home/username/backup.sh. Replace the path with your actual script or command. - Save and exit the editor. Cron will automatically load the new schedule. You can verify it with
crontab -l. - Test your command manually before scheduling it, to ensure it works and has the correct permissions. If the command needs root privileges, you may need to edit the system crontab with
sudo crontab -e.
Using systemd Timers (Alternative)
- Create a service unit file that defines what command to run. For example, create
/etc/systemd/system/backup.servicewith the following content:[Unit] Description=My backup job [Service] ExecStart=/home/username/backup.sh
- Create a timer unit file that schedules the service. Create
/etc/systemd/system/backup.timerwith:[Unit] Description=Run backup daily at 2am [Timer] 02:00:00 Persistent=true [Install] WantedBy=timers.target
- Enable and start the timer with
sudo systemctl enable --now backup.timer. - Check the timer status with
systemctl list-timersto see when it will next run.
Note: Menu and file names may vary slightly by distribution and version, but the general structure is consistent.
Common Mistakes
- Forgetting to use absolute paths: Cron runs with a minimal environment, so commands like
backup.shmay not be found. Always use full paths like/usr/bin/rsyncor/home/user/backup.sh. - Not making scripts executable: If your script isn't executable, cron can't run it. Use
chmod +x script.sh. - Ignoring output redirection: If you don't redirect output, cron will email it to you (if mail is set up) or silently discard it. Add
>> /var/log/myscript.log 2>&1to log output. - Using relative paths inside scripts: A script that refers to
./configmay not find it because cron's working directory is often the user's home. Use absolute paths inside the script. - Misunderstanding day-of-week and day-of-month: When both are specified, cron runs the job if either matches. This can cause unexpected runs. If you need both, use a separate condition or systemd timers.
Tips
- Test with a simple command first, like
* * * * * echo "test" >> /tmp/cron-test.log, then check the file after a minute. - Use systemd timers for complex schedules like "every 15 minutes on weekdays" because they handle calendar logic more reliably.
- Set the
MAILTOvariable in your crontab to receive output by email:MAILTO=you@example.com. - Keep a log of cron runs by redirecting output to a file. This helps in troubleshooting.
- Use
systemctl list-timersto see all active timers and their next run times.
Troubleshooting
- Check if cron service is running: Run
systemctl status cron(orcrondon some systems). If it's not active, start it withsudo systemctl start cronand enable it withsudo systemctl enable cron. - Verify your crontab syntax: Run
crontab -lto see your entries. A syntax error will prevent all jobs from running. Look for extra spaces or missing fields. - Check system logs: Look at
/var/log/syslogor/var/log/cronfor entries likeCRON: (user) CMD (command). If you see errors, they'll be logged there. - Test the command manually as the same user: Run the exact command from the crontab in a terminal. If it fails, fix the command first.
- If using systemd timers, check the service status: Run
systemctl status backup.serviceto see if the service ran successfully. Look for exit codes or errors in the journal withjournalctl -u backup.service. - Ensure the command has execute permission: For scripts,
ls -l script.shshould show-rwxr-xr-x. If not, runchmod +x script.sh.
Frequently Asked Questions
How do I run a command every 5 minutes?
In your crontab, add */5 * * * * command. The */5 means every 5th minute.
How do I schedule a command to run only on weekdays?
Use 0 8 * * 1-5 command to run at 8:00 AM Monday through Friday. The 1-5 represents Monday to Friday.
Can I schedule a command to run once at a specific future time?
Yes, use the at command instead of cron. For example, at 14:30 then type your command, then press Ctrl+D.
Why didn't my cron job run?
Check the cron service status, verify your crontab syntax, and look at system logs. Also ensure the command path is correct and the script is executable.
What's the difference between cron and systemd timers?
Cron is simpler and works on all Unix-like systems. Systemd timers are integrated with systemd, offer better logging, can handle missed runs with Persistent=true, and support more complex calendar expressions.
How do I stop a scheduled job?
Edit your crontab with crontab -e and delete the line, or comment it out with #. For systemd timers, run sudo systemctl disable --now timer-name.timer.