My cron job does not run on the VPS: the usual causes

When a cron job does not run on a VPS, the cause is almost never cron itself: it is the environment it runs in. cron starts with a minimal environment, without your PATH, without your terminal and without your working directory. The command that works at the prompt fails there. The cure is to use full paths, redirect the output to a file, and read that file.

Step by step

1 See the job. crontab -l lists your user’s jobs (sudo crontab -l -u root root’s). System jobs live in /etc/crontab and /etc/cron.d/ and carry an extra column with the user name.
2 Check the service is active. systemctl status cron on Debian and Ubuntu; systemctl status crond on AlmaLinux and Rocky.
3 See whether cron even tried. On Debian and Ubuntu, grep CRON /var/log/syslog | tail; on any systemd system, sudo journalctl -u cron (or -u crond). If the line shows up, cron ran the command and the problem is inside it.
4 Capture the output. Add to the end of the line >> /var/log/my-cron.log 2>&1 (create the file or use a folder the user can write to). Wait for one run and read the file: the error will be there.
5 Use full paths. /usr/bin/php /var/www/yourdomain.tld/task.php instead of php task.php. Find the path with which php. If the script opens files by relative path, start the line with cd /folder &&.
6 Try the same command with cron’s environment: env -i /bin/sh -c "your_command". If it fails like that, it is the environment.

The causes, in order of frequency

Symptom Cause and remedy
Works in the terminal, fails in cron Minimal PATH and environment. Full paths for the program and the files; variables defined in the crontab itself or in the script.
Nothing in cron’s log The service is stopped, or the crontab has a syntax error. The five time fields are: minute, hour, day of month, month, day of week.
The script “has no permission” The execute bit is missing (chmod +x script.sh), or the cron user cannot read the folder: file permissions.
A line with % breaks In a crontab, % means newline. Escape it with \%, for example in date +\%F.
Runs at the wrong hour cron uses the server’s time zone. See it with timedatectl. A job “at 2” is at 2 in that zone, not yours. To set it: the VPS time zone and clock.
Runs, but does nothing The command ends with an error nobody sees. Without output redirection, cron tries to mail it to a local mailbox nobody reads.
Runs twice at the same time The previous run has not finished. Use flock to prevent overlaps.
Do not edit another user’s crontab by mistake. A job that writes files as root into a site folder leaves them owned by root, and the site can no longer change them. Put the job in the crontab of the user who owns the files (sudo crontab -e -u username).
Be careful with jobs that delete. Rehearse by hand first, with a test folder. A line with a mistyped path and an rm is hard to undo, and on a VPS the backups are yours: backing up a VPS.
For PHP jobs and automatic backups we have ready-made articles: running a PHP script from a cron job and automatic backups of your own application. On cron in general and in the hosting panel: cron jobs.

Questions about what is on our side, such as the machine being up or the network? Open a ticket and we will check.

Open a support ticket

SEE ALSO

Where the logs are on a Linux VPS

File permissions on a VPS

Running a PHP script from a cron job

Automatic backups of your own application

RECOMMENDED PRODUCT

VPS server with root access

Resources of your own, the OS you choose, reinstall whenever you like. from $8.40/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?