How to set up a cron job on Linux

Open it with crontab -e, add a line, save – that is all a cron job on a Linux server needs. This page shows the three steps, how to list existing cron jobs, where to see whether they ran, and the mistakes almost everyone trips over once.

In three steps

1. Open the crontab

crontab -e

This opens the crontab of the logged-in user. The first time, Linux asks for an editor – nano is the easiest choice for beginners. If the job should run as a different user, for example the web server, use sudo crontab -e -u www-data.

2. Add a line

30 2 * * * /usr/bin/php /var/www/shop/cron.php >> /var/log/shop-cron.log 2>&1

The five time fields come first (minute, hour, day, month, weekday), then the command. This line starts the script every night at 2:30 am and writes its output, including error messages, into a log file. How the fields work in detail is explained in the crontab syntax; the crontab generator builds the right expression.

3. Save

In nano with Ctrl+O, Enter and Ctrl+X, in vim with Esc and :wq. Linux confirms with crontab: installing new crontab – the new schedule applies from the next minute, no restart needed.

List cron jobs

Your own entries are shown by

crontab -l

Another user's entries are shown with sudo crontab -l -u name. All users at once:

for u in $(cut -d: -f1 /etc/passwd); do echo "## $u"; sudo crontab -l -u "$u" 2>/dev/null; done

Not every cron job sits in a user crontab. These places count too:

LocationWhat it contains
/etc/crontabsystem-wide crontab with an extra column for the user
/etc/cron.d/crontabs installed by packages, also with a user column
/etc/cron.daily/scripts that run once a day – plus cron.hourly, cron.weekly and cron.monthly
systemctl list-timerssystemd timers, which take over some tasks on newer systems

To delete one, remove the line with crontab -e. Careful with crontab -r: it deletes the whole crontab without asking.

Did the cron job run?

On Debian and Ubuntu every start is in the system log:

grep CRON /var/log/syslog
journalctl -u cron --since today

On Red Hat, AlmaLinux and Rocky Linux the service is called crond and the log is in /var/log/cron. Whether the service is running at all is shown by systemctl status cron or crond.

The log only says that the command was started. Whether it worked is in its output – hence the redirect >> file.log 2>&1 in step 2.

The four most common mistakes

Relative paths

Cron starts with a very short PATH and without your shell settings. Always write programs and files with their full path, i.e. /usr/bin/php instead of php. which php tells you where a program lives.

The percent sign

In the crontab, % ends the command. For a date in a file name write date +\%F instead of date +%F.

The last line

Some cron versions ignore the last line if no line break follows it. Simply leave an empty line at the end of the crontab.

Permissions and user

A script needs execute permission (chmod +x) and runs as the user who owns the crontab – with that user's permissions on files and folders.

Still not running? The step-by-step troubleshooting goes through every cause.

Crontab examples

LineMeaning
*/5 * * * * /usr/bin/php /var/www/app/cron.phpa PHP script every 5 minutes
0 3 * * * /usr/local/bin/backup.sha backup every night at 3:00 am
0 6 * * 1-5 curl -fsS https://example.com/cron.php > /dev/nullcall an address on weekdays at 6:00 am
0 0 1 * * /usr/local/bin/monatsbericht.shon the 1st of every month at midnight
@reboot /usr/local/bin/start.shonce after every server restart

More ready-made expressions to copy are in the crontab syntax.

Know that it runs – and without your own server

Cron tells nobody when a job fails or the server is down. With a heartbeat the job checks in with Cronjob.de after every run – if the check-in does not arrive, you get a message by email, Telegram, Slack or Teams.

No access to the crontab, for example on shared hosting? Then Cronjob.de calls your script via its address, down to every minute: Set up a cron job on shared hosting.

Common questions

How do I list all cron jobs on a Linux server?
Your own with crontab -l, another user's with sudo crontab -l -u name. On top of that come the system-wide entries in /etc/crontab and /etc/cron.d/, the scripts in /etc/cron.daily/ and its siblings and, on newer systems, the systemd timers (systemctl list-timers).
Where can I see whether my cron job ran?
In the system log: on Debian and Ubuntu with grep CRON /var/log/syslog or journalctl -u cron, on Red Hat, AlmaLinux and Rocky in /var/log/cron. It only says that the command was started – whether it worked is shown by the output you redirect into a file.
Do I need root privileges for a cron job?
No. Every user has their own crontab and edits it with crontab -e. You only need root for other users' crontabs and for the system-wide files under /etc.
What if I do not have my own Linux server?
On shared web hosting there is often no access to the crontab. Then the script can be called via its address: Cronjob.de calls it on your schedule, down to every minute, and lets you know when the call fails.
How do I notice when a cron job on my server fails?
Cron itself tells nobody. Append a heartbeat to the line: after every run the job checks in with Cronjob.de, and if the check-in does not arrive or reports an error, you get a message.

Cron jobs without your own server

Five cron jobs are free forever, no credit card. For the first 14 days you can try everything, including failure alerts.

Start for free See pricing