I want to schedule a task to run on a regular basis and have heard that Cron is the way to do this.
How do I add Cron jobs in Ubuntu?
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 e.g. twice a month or every 5 minutes. 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! In every line you can define one command to run and its schedule, and the format is quite simple when you get the hang of it. The structure is:
minute hour day-of-month month day-of-week command
For all the numbers you can use lists, e.g. 5,34,55
in the minutes field will mean run at 5 past, 34 past, and 55 past whatever hour is defined.
You can also use intervals. They are defined like this: */20
. This example means every 20th, so in the minutes column it is equivalent to 0,20,40
.
So to run a command every Monday at 5:30 in the 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.
*/15 * * * * /path/to/command
, you can do @reboot /path/to/command
in order to execute something on startup.
Commented
Aug 12, 2016 at 7:56
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 use 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
.
To add a cron job that runs as root, you can edit root's crontab by running sudo crontab -e
.
The most flexible way is to use the system crontab /etc/crontab
which you can edit only with root privileges. In this file, the user each command is to be run as is specified, so you can run your commands as root (in case you need that level of privilege) or any other user on the system.
For example, if you want to run something every 10 minutes as root, you'd add a line like this
*/10 * * * * root /usr/bin/somedirectory/somecommand
(notice the addition of the user to the line)
You can see the contents of the system crontab file with cat /etc/crontab
.
More details at: https://help.ubuntu.com/community/CronHowto
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.
I recommend KDE's Task Scheduler (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.
I wanted to set a Cron job to run through a bash script, so executing the script would add a cron job.
I realised that when you make use of:
crontab -e
Then it creates the file:
/var/spool/cron/crontabs/root
Where root is the name of the user running running the crontab command. So based on this and in 14.04 at least, we can execute the following bash commands to create a new Cron job:
echo "30 17 * * 1 /path/to/command" > /var/spool/cron/crontabs/root
We also need to set the correct ownership for the file:
chown root:root /var/spool/cron/crontabs/root
And set the correct permissions:
chmod 600 /var/spool/cron/crontabs/root
If when you run crontab -e
there are already Cron jobs in the list, then you are able append to the list using the following command:
echo "30 17 * * 1 /path/to/command" >> /var/spool/cron/crontabs/root
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.
I can't install gnome schedule on Ubuntu 16.04
@ askubuntu.com/questions/785657/…. There is a package to download @ bugs.launchpad.net/ubuntu/+source/gnome-schedule/+bug/1576060/…. Just perform $ sudo dpkg -i gnome-schedule_2.3.0-0ubuntu16.04_amd64.deb
Example of running script test_cron.sh
by cron every minute on Ubuntu 18.04 using symbolic link:
test_cron.sh
file:
#!/bin/bash
echo "System backuped" >> /media/myname/data/backup/backup_tmp.log
If you want to use environment variables in your script like $USER
in paths it is better to type precise path, bash will not know your variables at execution time.
myname
is user name (part of root group, I am not sure that root privileges are necessary).
Allow users to set cron jobs, file will be created if necessary:
sudo nano /etc/cron.allow
root
myname
The path to script is /home/myname/shell/test_cron.sh
I changed the owner and made it executable:
sudo chown myname /home/myname/shell/test_cron.sh
chmod +x /home/myname/shell/test_cron.sh
I added symbolic link:
sudo ln -s /home/myname/shell/test_cron.sh /usr/bin/test_cron
Logged as myname
I added new task to execute test_cron
every minute.
crontab -e
*/1 * * * * test_cron
To check if the command in the list:
crontab -l
*/1 * * * * test_cron
To check execution
grep -i cron /var/log/syslog
Nov 17 12:28:01 myname-ubuntu CRON[13947]: (myname) CMD (system-backup)
Considering you have multiple cron jobs with particular user and they don't share same schedule. You can just simple create file under /etc/cron.d/
Lets say file name is myjobs
then just write all your schedulers in that file and then run following command.
crontab -u <username> /etc/cron.d/myjobs