Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Create and Set Up a Cron Job in Ubuntu

Open the crontab editor, add a job, save it and check that it is listed.

How-To Guides2 min read7 screenshots

Create a cron job

Step 1: Open the crontab editor

crontab -e
Opening the crontab editor

The first time, Ubuntu asks which editor to use. Choose nano (the easiest).

Choosing the editor
The crontab editor

Step 2: Add a job

Use this format:

* * * * * command_to_execute

The five stars are minute, hour, day of month, month and day of week.

Example, run a backup script every day at 2:00:

0 2 * * * /home/user/scripts/backup.sh
Adding a job to the crontab

Step 3: Save and exit

In nano press Ctrl+O, Enter, then Ctrl+X. Cron reads the new file by itself.

Example: write a message every minute

* * * * * echo "This is a message written every minute" >> /home/user/message.txt
Adding the example job

Wait a few minutes, then read the file:

cat /home/user/message.txt
The messages written by cron

Remove the job when the test is done, or it will keep running.

Check your cron jobs

crontab -l
Listing the cron jobs

More tips

  • Use full paths for commands and scripts.
  • Make scripts executable: chmod +x script.sh.
  • Send the output to a log to find errors: ... >> /var/log/myjob.log 2>&1.
  • Check that the service runs: systemctl status cron.
  • See what ran: grep CRON /var/log/syslog.
  • Use sudo crontab -e for jobs that need root.

Quick recap

  • crontab -e to add jobs, crontab -l to list them.