DEV3 Technologies home

DEV3 Developer Forum

Why is my cron job not running or behaving differently than when I run the script manually?

DevOps helpSolved0 views
dev3 ·

My script works in the terminal, but the crontab entry */5 * * * * backup.sh does nothing. There is no output and no error. What should I check?

#cron#linux#troubleshooting

0

1 reply

dev3 · Accepted answer

Cron runs commands with a minimal environment: a short PATH, no login profile, a different working directory, and it discards output unless you capture it.

Checklist

  1. Use absolute paths for the script and every binary it calls:
*/5 * * * * /usr/bin/env bash /home/deploy/bin/backup.sh >> /var/log/backup.log 2>&1
  1. Make it executable (chmod +x) and add a shebang (#!/usr/bin/env bash), or call the interpreter explicitly as above.
  2. Capture output. Without >> file 2>&1 errors go to local mail, which usually nobody reads.
  3. Set PATH or env at the top of the crontab if needed:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
  1. Right crontab: crontab -l for your user; system crons in /etc/cron.d/ need a user field (* * * * * root command). Files in /etc/cron.d must not have a dot in the name on Debian.
  2. Percent signs are special in crontab lines (they mean newline); escape them: date +\%F.
  3. Is cron running? systemctl status cron (Debian/Ubuntu) or crond (RHEL).
  4. Check the log: grep CRON /var/log/syslog or journalctl -u cron.
  5. Time zone: cron uses the system zone. Confirm with timedatectl.
  6. Permissions: the user must be allowed (not listed in /etc/cron.deny) and have access to the files.

Overlap: if a job can take longer than its interval, prevent stacking:

*/5 * * * * flock -n /tmp/backup.lock /home/deploy/bin/backup.sh

Build and verify schedule expressions with the cron expression builder.

0

Your reply

Every post is reviewed by DEV3 before it appears. Usually reviewed within 24 hours.

Markdown supported. Use ``` for code blocks. Images are not allowed.