Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Schedule Linux Commands with cron and systemd

· 5 min read · Home Automation

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 cron installed (most distributions include it by default; if not, install the cron package via your package manager).
  • Basic familiarity with the command line and editing files with a text editor like nano or vim.
  • For systemd timers, a system running systemd (most modern distributions).

Step-by-Step

Using Cron

  1. Open your user's crontab file by running crontab -e in the terminal. If it's your first time, it will ask you to choose an editor (nano is easiest for beginners).
  2. 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.
  3. 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.
  4. Save and exit the editor. Cron will automatically load the new schedule. You can verify it with crontab -l.
  5. 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)

  1. Create a service unit file that defines what command to run. For example, create /etc/systemd/system/backup.service with the following content:
    [Unit]
    Description=My backup job
    
    [Service]
    ExecStart=/home/username/backup.sh
  2. Create a timer unit file that schedules the service. Create /etc/systemd/system/backup.timer with:
    [Unit]
    Description=Run backup daily at 2am
    
    [Timer] 02:00:00
    Persistent=true
    
    [Install]
    WantedBy=timers.target
  3. Enable and start the timer with sudo systemctl enable --now backup.timer.
  4. Check the timer status with systemctl list-timers to 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.sh may not be found. Always use full paths like /usr/bin/rsync or /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>&1 to log output.
  • Using relative paths inside scripts: A script that refers to ./config may 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 MAILTO variable 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-timers to see all active timers and their next run times.

Troubleshooting

  1. Check if cron service is running: Run systemctl status cron (or crond on some systems). If it's not active, start it with sudo systemctl start cron and enable it with sudo systemctl enable cron.
  2. Verify your crontab syntax: Run crontab -l to see your entries. A syntax error will prevent all jobs from running. Look for extra spaces or missing fields.
  3. Check system logs: Look at /var/log/syslog or /var/log/cron for entries like CRON: (user) CMD (command). If you see errors, they'll be logged there.
  4. 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.
  5. If using systemd timers, check the service status: Run systemctl status backup.service to see if the service ran successfully. Look for exit codes or errors in the journal with journalctl -u backup.service.
  6. Ensure the command has execute permission: For scripts, ls -l script.sh should show -rwxr-xr-x. If not, run chmod +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.