Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I add Cron jobs in Ubuntu?

share|improve this question

5 Answers 5

up vote 137 down vote accepted

Put a shell script in one of these folders: /etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly or /etc/cron.weekly.

If these are not enough for you you can add more specific tasks eg. twice a month or every 5 minutes or... go to the terminal and type:

crontab -e

this will open your personal crontab (cron configuration file), the first line in that file explains it all (don't you think)! In every line you can define one command to run, and the format is quite simple when you get the hang of it. So the structure is:

minute hour day-of-month month day-of-week command

For all the numbers you can use lists eg, 5,34,55 in the first field will mean run at 5 past 34 past and 55 past what ever hour is defined.

You can also use intervals, they are defined like this: */20 this example means every 20th and if in the minutes column this will be equivalent to 0,20,40
So to run a command every monday at 5:30 in afternoon:

30 17 * * 1 /path/to/command

or every 15 minutes

*/15 * * * * /path/to/command

Note that the day-of-week goes from 0-6 where 0 is sunday.

You can read more here.

share|improve this answer
    
These are system-wide and run with high privileges. I wouldn't put anything there unless there is a pressing need for access or permission. As a rule of thumb, try to do stuff without capabilities. Therefore, I like this answer better: ubuntu.stackexchange.com/questions/2368/how-do-i-setup-cron-job/… –  Marcelo Morales Aug 16 '10 at 21:29
2  
@Marcelo Morales, Which also will run the given commands as root! if you on the other hand doesn't use sudo then you will create a user crontab and this will be run as the user who created it! –  Source Lab Aug 16 '10 at 21:44
1  
*/15 * * * * /path/to/command was very handy for me! thanks! –  f00644 Feb 21 '13 at 11:18
    
It's worth noting these changes are applied automatically, you don't need to restart/reload anything. –  Molomby Dec 12 '13 at 1:26
    
Great answer! This is really thorough –  Willem Ellis Mar 19 at 2:11

If the job you want to run can be run with the same privileges as your user I recommend using a user crontab which you can edit by running EDITOR="gedit" crontab -e(which will used gedit to edit the crontab file) or simply crontab -e(which will use the default editor) in a terminal.

If you want to run something every 10 minutes, for example, you add a line like this

*/10 * * * * /usr/bin/somedirectory/somecommand

and save the file.

You can see the contents of the user crontab with crontab -l.

The most flexible way is to use the system crontab which you can edit by running sudo crontab -e in a terminal. This will allow you to run your commands as root (in case you need that level of privilege) or any other user on the system.

Then if you want to run something every 10 minutes as root, for example, you add a line like this

*/10 * * * * root /usr/bin/somedirectory/somecommand

and save the file.(notice the addition of the user to the line)

You can see the contents of the system crontab file with sudo crontab -l.

More details at: https://help.ubuntu.com/community/CronHowto

share|improve this answer

If you prefer to do it using a GUI, you can go to the Software Center and install Scheduled tasks (or run sudo apt-get install gnome-schedule). It will provide a powerful GUI to add cron tasks.

Note that if you use this method, tasks by default will be executed as your own user, not as root. This is usually a good thing.

share|improve this answer
2  
+1 for GUI option! –  Marco Ceppi Aug 16 '10 at 15:18

I recommend KDE's Task Scheduler (kde-config-cron) Install kde-config-cron. Access it from the System Settings in the Task Scheduler module there.

It manages both personal and system Crontabs, and the ease of creating the time boundaries greatly surprised me (see the screenshot below). I think this part is really underrated.

enter image description here

share|improve this answer

KDE Task Scheduler will not work in regular Ubuntu. It works only in KDE Systems like KUbuntu. For non KDE system you will prefer to use gnome-schedule

$ sudo apt-get install gnome-schedule

The app is Scheduled tasks in the Dash.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.