How to Run Cron Jobs Every Minute, Hour, Day on Ubuntu Linux
Cron jobs run automated tasks on a schedule in Ubuntu Linux, so you do not have to start backups, clear temporary files, or handle routine system maintenance by hand. A crontab file holds these scheduled commands. Each one starts with a line of five time fields, such as */1 * * * *, which runs a script every minute.
Cron saves you from retyping the same commands in the terminal. You manage the schedule through plain text, and the cron service runs the jobs in the background. On Ubuntu this service is installed and running by default, so you normally do not need to install or start anything.
Edit your crontab file by running `crontab -e`. Use asterisks for minute, hour, or day intervals, like `* * * * *` for every minute.
How to schedule cron tasks in Ubuntu Linux
Setting up a cron job on Ubuntu means editing a crontab file, which works as a schedule for background commands. Each user has a personal crontab, and there is also a system-wide crontab. You open the file from the terminal and add a line that says when to run which command.
Cron reads the file again after you save it. You do not need to restart the service or reboot for a new or changed job to take effect.
The sections below show how to run cron jobs at specific intervals. First, you need to know how a crontab line is built.
Crontab (cron table) is a text file that holds scheduled tasks. Each line in a user crontab has six parts. The first five are the time fields, and the sixth is the command to run. In order, the time fields are:
- Minute0 to 59
- Hour0 to 23, using a 24-hour clock
- Day of month1 to 31
- Month1 to 12
- Day of week0 to 7, where both 0 and 7 mean Sunday
Lines that start with a hash (#) are comments, and cron ignores them. Use them to note what each job does.
An example cron file looks similar to the block below. Read each line from left to right: five time fields, then the command.
The asterisk (*) is a wildcard in cron. It tells cron to run the task in every possible value of that field. An asterisk in the Minute field means every minute. The same applies to the Hour, Day, and Month fields.
A line with five asterisks therefore runs every minute of every day. Check the other fields carefully before you save, so you do not schedule something more often than you intended.
The hyphen (-) sets a range. For instance, writing 1-5 in the Day of Week field targets Monday through Friday. A range works in any field, as long as the numbers fall inside that field’s allowed values.
Commas separate individual values within a field. Writing 1,2,3 in the Hour field runs the job at 1 AM, 2 AM, and 3 AM. If you leave the Day fields as asterisks, the job runs daily at those hours.
You can also combine operators in one field. For example, 1,2,5-8 runs the task at 1 AM, 2 AM, and every hour from 5 AM through 8 AM.
The system-wide crontab uses the same syntax with one extra field. That field sits between the time fields and the command, and it tells cron which user should run the job. A user crontab does not have this field, because the job always runs as the owner of that crontab. If you copy a line between the two kinds of file, add or remove the user field or the job will fail.
Cron gives you fine control over system tasks. Once you understand the five time fields and the three operators, you can read any crontab line and write your own for routine jobs on an Ubuntu Linux system.
How to schedule a cron job to run every 5 minutes
To run a cron job every 5 minutes, follow these steps:
- Open your crontab for editing from the terminal. If Ubuntu asks which editor to use the first time, pick the one you are comfortable with. You should see the crontab file with any existing jobs.
- Scroll to the end of the file and add the line below on its own line. Replace the command at the end with the script or command you want to run.
- Save the file and exit the editor. The terminal should confirm that the new crontab is installed.
The */5 in the Minute field means “every 5 minutes”. The job runs at minutes 0, 5, 10, and so on, which is 12 times an hour. It follows the clock, not the time you saved the file.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * ~/script.sh
How to schedule a cron job to run every 10 minutes
To run a job every 10 minutes, follow the same steps. Open your crontab, add the line below at the end, then save and exit.
If you are unsure whether the job runs, wait for the next 10-minute mark and check whether the command did what you expected, for example by looking at a log file or an output file it writes.
*/10 * * * * ~/scripts.sh
How to run a cron job every 10 minutes
To run a cron job every 10 minutes, you put a step value, written with a forward slash, in the Minute field of your crontab. This tells the system to run your script automatically six times every hour, at minutes 0, 10, 20, 30, 40, and 50. You do not have to trigger updates or checks by hand.
Cron starts jobs with a very small environment, so give the full path to any script or program in the command part of the line. A job that works in your terminal can fail in cron if it depends on your usual PATH.
*/15 * * * * ~/script.sh
How to run a cron job every minute
Every minute is the shortest interval cron supports. It is useful for quick tests and for tasks that need frequent checks, but it can add load if the command is slow or heavy. Make sure each run finishes before the next one starts.
To run a cron job every minute, follow these steps:
- Open your crontab for editing from the terminal.
- Add the line below at the end of the file, with your own command in place of the example.
- Save the file and exit the editor.
Within a minute, the job should run for the first time. Writing */1 in the Minute field has the same effect as a plain asterisk.
* * * * * ~/script.sh
Cron is also a common way to renew TLS certificates. An example cron entry for Let's Encrypt certbot to renew certificates automatically looks like the one below. Certbot often sets up its own automatic renewal when you install it, so check for an existing renewal job before you add this one. Two renewal jobs are not needed.
# the cronjob.timer function takes precedence over this cronjob. For # more details, see the systemd.timer manpage, or use systemctl show # certbot.timer. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 0 */12 * * * root test -x /usr/bin/certbot -a ! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew
That completes the setup process. To confirm your jobs are saved, open your crontab again and check that the lines are there. To undo a change, open the crontab, delete the line or put a hash (#) at the start of it, and save. Cron stops running that job on its next check, and the hash keeps the line around if you want to turn it back on later.
Conclusion:
- Cron jobs in Ubuntu Linux are useful for automating operational and mundane tasks.
- The key to scheduling tasks with cron jobs lies in understanding the syntax and utilizing the crontab file effectively.
- By leveraging the asterisk, hyphen, and comma operators, users can create precise schedules for their cron jobs.
- Examples of scheduling cron jobs at various intervals, along with a Let's Encrypt certbot renewal cron entry, demonstrate the versatility of this feature.
- With this understanding, users can efficiently manage and automate tasks using cron jobs in Ubuntu Linux.
Have a question about this guide?
Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
[…] How to run cron jobs on Ubuntu […]