特殊:Badtitle/NS100:CronHowto:修订间差异
小 新页面: {{From|https://help.ubuntu.com/community/CronHowto}} {{Languages|UbuntuHelp:CronHowto}} == What is Cron? == Cron is a daemon used for scheduling tasks to be executed at a certain time. ... |
→Cron Jobs Alternatives: adding new cron jobs alternative |
||
(未显示1个用户的10个中间版本) | |||
第2行: | 第2行: | ||
{{Languages|UbuntuHelp:CronHowto}} | {{Languages|UbuntuHelp:CronHowto}} | ||
== What is Cron? == | == What is Cron? == | ||
Cron is a daemon used for scheduling tasks to be executed at a certain time. Each user has a crontab file, allowing them to specify actions and times that they should be executed. There is also a system crontab, allowing tasks such as log rotation and locate database updating to be done regularly. | Cron is a daemon used for scheduling tasks to be executed at a certain time. Each user has a crontab file, allowing them to specify actions and times that they should be executed. There is also a system crontab, allowing tasks such as log rotation and locate database updating to be done regularly. | ||
The following directions tell you how to set up scheduled tasks the traditional way using the command line, but it is much easier to use the Gnome ''Scheduled tasks'' tool (from the ''gnome-schedule'' package) in ''Applications'' --> ''System Tools''. | |||
== What is Crontab? == | |||
A crontab is a simple text file that holds a list of commands that are to be run at specified times. These commands, and their related run times, are controlled by the cron daemon and are executed in the system's background. More information can be found by viewing the crontab's man page. | |||
=== Using Cron === | === Using Cron === | ||
To use cron, simply add entries to your crontab file. To do this, open a terminal window and enter <code><nowiki>crontab -e</nowiki></code>. An editor will open displaying your current crontab file. Edit the crontab using the format described below. Then save your changes and your new crontab will be installed. Exiting without saving will leave your crontab untouched. | |||
To use cron, simply add entries to your crontab file. | === Crontab Sections === | ||
Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out: | |||
minute (0-59), hour (0-23, 0 == midnight), day (1-31), month (1-12), weekday (0-6, 0 == Sunday), command | |||
<pre><nowiki> | <pre><nowiki> | ||
01 04 1 1 1 /usr/bin/somedirectory/somecommand | |||
</nowiki></pre> | </nowiki></pre> | ||
The | The above example will run /usr/bin/somedirectory/somecommand at 4:01am on January 1st plus every Monday in January. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code: | ||
<pre><nowiki> | <pre><nowiki> | ||
* | 01 04 * * * /usr/bin/somedirectory/somecommand | ||
</nowiki></pre> | </nowiki></pre> | ||
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month. | |||
Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-seperated values can be used to run a command continuously. Code: | |||
Comma- | |||
<pre><nowiki> | <pre><nowiki> | ||
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand | 01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand | ||
</nowiki></pre> | </nowiki></pre> | ||
The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June. | The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June. | ||
The "/usr/bin/somedirectory/somecommand" text in the above examples indicates the task which will be run at the specified times. It is recommended that you use the full path to the desired commands as shown in the above examples. Enter ''which somecommand'' in the terminal to find the full path to ''somecommand''. The crontab will begin running as soon as it is properly edited and saved. | |||
You may want to run a script some number of times per time unit. For example if you want to run it every 10 minutes use the following crontab entry (runs on minutes divisible by 10: 0, 10, 20, 30, etc.) | |||
<pre><nowiki> | <pre><nowiki> | ||
*/10 * * * * /usr/bin/somedirectory/somecommand | |||
</nowiki></pre> | </nowiki></pre> | ||
which is also equivalent to the more cumbersome | |||
<pre><nowiki> | <pre><nowiki> | ||
0,10,20,30,40,50 * * * * /usr/bin/somedirectory/somecommand | |||
</nowiki></pre> | </nowiki></pre> | ||
=== Crontab Options === | |||
* The -l option causes the current crontab to be displayed on standard output. | |||
* The -r option causes the current crontab to be removed. | |||
* The -e option is used to edit the current crontab using the editor specified by the EDITOR environment variable. | |||
After you exit from the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically. | |||
The file is stored in ''/var/spool/cron/crontabs'' but should only be edited via the crontab command. | |||
=== Enable User Level Cron === | |||
If the '''/etc/cron.allow''' file exists, then users must be listed in it in order to be allowed to run the '''crontab''' command. If the '''/etc/cron.allow''' file does not exist but the '''/etc/cron.deny file''' does, then users must not be listed in the '''/etc/cron.deny''' file in order to run '''crontab'''. | |||
In the case where neither file exists, the default on current Ubuntu (and Debian, but not some other Linux and UNIX systems) is to allow all users to run jobs with '''crontab'''. | |||
No cron.allow or cron.deny files exist in a standard Ubuntu install, so all users should have cron available by default, until one of those files is created. If a blank cron.deny file has been created, that will change to the standard behavior users of other operating systems might expect: cron only available to root or users in cron.allow. | |||
The | |||
=== Further Considerations === | === Further Considerations === | ||
The above commands are stored in a crontab file belonging to your user account and executed with your level of permissions. If you want to regularly run a command requiring a greater level of permissions, set up a root crontab file using: | The above commands are stored in a crontab file belonging to your user account and executed with your level of permissions. If you want to regularly run a command requiring a greater level of permissions, set up a root crontab file using: | ||
<pre><nowiki> | <pre><nowiki> | ||
第66行: | 第56行: | ||
echo "Nightly Backup Successful: $(date)" >> /tmp/mybackup.log | echo "Nightly Backup Successful: $(date)" >> /tmp/mybackup.log | ||
</nowiki></pre> | </nowiki></pre> | ||
For more information, see the '''man''' pages for '''cron''' and '''crontab''' (man is detailed on the BasicCommands page). If your machine is regularly switched off, you may also be | For more information, see the '''man''' pages for '''cron''' and '''crontab''' (man is detailed on the [[UbuntuHelp:BasicCommands|BasicCommands]] page). If your machine is regularly switched off, you may also be interested in '''at''' and '''anacron''', which provide other approaches to scheduled tasks. For example, '''anacron''' offers simple system-wide directories for running commands hourly, daily, weekly, and monthly. Scripts to be executed in said times can be placed in '''/etc/cron.hourly/''', '''/etc/cron.daily/''', '''/etc/cron.weekly/''', and '''/etc/cron.monthly/'''. All scripts in each directory are run as root, and a specific order to running the scripts can be specified by prefixing the scripts' filenames with numbers (see the '''man''' page for '''run-parts''' for more details). Although the directories contain periods in their names, run-parts will not accept a file name containing one and will fail silently when encountering them ([https://bugs.launchpad.net/ubuntu/+source/debianutils/+bug/38022 bug #38022]). Either rename the file or use a symlink (without a period) to it instead. | ||
=== Troubleshooting and Common Problems === | |||
== | Edits to a user's crontab and jobs that are run on their behalf are all logged by default to '''/var/log/syslog''' and that's the first place to check if things are not running as you expect. | ||
When adding a new entry to a blank crontab, forgetting to add a newline at the end is a common source for the job not running. If the last line in the crontab does not end with a newline, no errors will be reported at edit or runtime, but that line will never run. See '''man crontab''' for more information. This has already been suggested as [https://bugs.launchpad.net/ubuntu/+source/cron/+bug/118168 a bug]. | |||
'''crontab | If a user was not allowed to execute jobs when their crontab was last edited, just adding them to the allow list won't do anything. The user needs to re-edit their crontab after being added to cron.allow before their jobs will run. | ||
When creating a crontab for the root user, the user name must be specified as a parameter after the date/time parameters. Accidentally including the user name that way in a user-specific crontab will result in trying to run the user's name as a command, rather than what was expected. | |||
Entries in cron may not run with the same environment, in particular the PATH, as you expect them to. Try using full paths to files and programs if they're not being located as you expect. | |||
The "%" character is used as newline delimiter in cron commands. If you need to pass that character into a script, you need to escape it as "\%". | |||
If you're having trouble running a GUI application using cron, see the GUI Applications section below. | |||
=== Advanced Crontab === | |||
The Crontabs discussed above are '''user''' crontabs. Each of the above crontabs is associated with a user, even the '''system''' crontab which is associated with the '''root''' user. There are two other types of crontab. | |||
Firstly, as mentioned above '''anacron''' uses the '''run-parts''' command and '''/etc/cron.hourly''', '''/etc/cron.weekly''', and '''/etc/cron.monthly''' directories. However '''anacron''' itself is invoked from the '''/etc/crontab''' file. This file could be used for other cron commands, but probably shouldn't be. Here's an example line from a ficticious '''/etc/crontab''': | |||
<pre><nowiki> | <pre><nowiki> | ||
00 01 * * * rusty /home/rusty/rusty-list-files.sh | |||
</nowiki></pre> | </nowiki></pre> | ||
This would run Rusty's command script as user '''rusty''' from his home directory. However, it is not usual to add commands to this file. While an experienced user should know about it, it is not recommended that you add anything to '''/etc/crontab'''. Apart from anything else, this could cause problem if the '''/etc/crontab''' file is affected by updates! Rusty could lose his command. | |||
The second type of crontab is to be found in '''/etc/cron.d'''. Within the directory are small named crontabs. The directory is often used by packages, and the small crontabs allows a user to be associated with the commands in them. | |||
Instead of adding a line to '''/etc/crontab''' which Rusty knows is not a good idea, Rusty might well add a file to '''/etc/cron.d''' with the name '''rusty''', containing his cron line above. This would not be affected by updates but is a '''well known''' location. | |||
When would you use these alternate crontab locations? Well, on a single user machine or a shared machine such as a school or college server, a '''user''' crontab would be the way to go. But in a large IT department, where several people might look after a server, then '''/etc/cron.d''' is probably the best place to install crontabs - it's a central point and saves searching for them! | |||
You may not need to look at '''/etc/crontab''' or '''/etc/cron.d''', let alone edit them by hand. But an experienced user should perhaps know about them and that the packages that he/she installs may use these locations for their crontabs. | |||
==== Special strings ==== | |||
Cron also offers some special strings: | |||
{|border="1" cellspacing="0" | |||
| '''string''' || '''meaning''' | |||
|- | |||
| @reboot || Run once, at startup. | |||
|- | |||
| @yearly || Run once a year, "0 0 1 1 *". | |||
|- | |||
| @annually || (same as @yearly) | |||
|- | |||
| @monthly || Run once a month, "0 0 1 * *". | |||
|- | |||
| @weekly || Run once a week, "0 0 * * 0". | |||
|- | |||
| @daily || Run once a day, "0 0 * * *". | |||
|- | |||
| @midnight || (same as @daily) | |||
|- | |||
| @hourly || Run once an hour, "0 * * * *". | |||
|} | |||
Usage: "@reboot /path/to/execuable1" will execute /path/to/executable1 when the system starts. | |||
See "man 5 crontab" for more info. | |||
=== GUI Applications === | |||
It is possible to run gui applications via cronjobs. This can be done by telling cron which display to use. | |||
<pre><nowiki> | <pre><nowiki> | ||
00 06 * * * env DISPLAY=:0 gui_appname | |||
</nowiki></pre> | </nowiki></pre> | ||
The ''env DISPLAY=:0'' portion will tell cron to use the current display (desktop) for the program "gui_appname". | |||
And if you have multiple monitors, don't forget to specify on which one the program is to be run. For example, to run it on the first screen (default screen) use : | |||
<pre><nowiki> | <pre><nowiki> | ||
00 06 * * * env DISPLAY=:0.0 gui_appname | |||
</nowiki></pre> | </nowiki></pre> | ||
The ''env DISPLAY=:0.0'' portion will tell cron to use the first screen of the current display for the program "gui_appname". | |||
= | '''Note:''' In Karmic(9.10), you have to enable X ACL for localhost to connect to for GUI applications to work. | ||
<pre><nowiki> | <pre><nowiki> | ||
~$ xhost +local: | |||
non-network local connections being added to access control list | |||
~$ xhost | |||
access control enabled, only authorized clients can connect | |||
LOCAL: | |||
... | |||
</nowiki></pre> | </nowiki></pre> | ||
== Tips == | |||
'''crontab -e''' uses the EDITOR environment variable. to change the editor to your own choice just set that. You may want to set EDITOR in you .bashrc because many commands use this variable. Let's set the EDITOR to nano a very easy editor to use: | |||
<pre><nowiki> | <pre><nowiki> | ||
export EDITOR=nano | |||
</nowiki></pre> | </nowiki></pre> | ||
There are also files you can edit for system-wide cron jobs. The most common file is located at '''/etc/crontab''', and this file follows a slightly different syntax than a normal crontab file. Since it is the base crontab that applies system-wide, you need to specify what user to run the job as; thus, the syntax is now: | |||
The | |||
<pre><nowiki> | <pre><nowiki> | ||
minute(s) hour(s) day(s)_of_month month(s) day(s)_of_week user command | |||
</nowiki></pre> | </nowiki></pre> | ||
It is recommended, however, that you try to avoid using '''/etc/crontab''' unless you need the flexibility offered by it, or if you'd like to create your own simplified '''anacron'''-like system using '''run-parts''' for example. For all cron jobs that you want to have run under your own user account, you should stick with using '''crontab -e''' to edit your local cron jobs rather than editting the system-wide '''/etc/crontab'''. | |||
=== Crontab Example === | === Crontab Example === | ||
Below is an example of how to setup a crontab to run updatedb, which updates the slocate database: Open a term, type "crontab -e" (without the double quotes) and press enter. Type the following line, substituting the full path of the application you wish to run for the one shown below, into the editor: | Below is an example of how to setup a crontab to run updatedb, which updates the slocate database: Open a term, type "crontab -e" (without the double quotes) and press enter. Type the following line, substituting the full path of the application you wish to run for the one shown below, into the editor: | ||
<pre><nowiki> | <pre><nowiki> | ||
45 04 * * * /usr/bin/updatedb | 45 04 * * * /usr/bin/updatedb | ||
</nowiki></pre> | </nowiki></pre> | ||
Save your changes and exit the editor. | Save your changes and exit the editor. | ||
Crontab will let you know if you made any mistakes. The crontab will be installed and begin running if there are no errors. That's it. You now have a cronjob setup to run updatedb, which updates the slocate database, every morning at 4:45. | Crontab will let you know if you made any mistakes. The crontab will be installed and begin running if there are no errors. That's it. You now have a cronjob setup to run updatedb, which updates the slocate database, every morning at 4:45. | ||
Note: The double-ampersand (&&) can also be used in the "command" section to run multiple commands consecutively. | |||
Note: The double-ampersand (&&) can also be used in the "command" section to run multiple commands consecutively. | |||
<pre><nowiki> | <pre><nowiki> | ||
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb | 45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb | ||
</nowiki></pre> | </nowiki></pre> | ||
The above example will run chkrootkit followed by updatedb at 4:45am daily - providing you have all listed apps installed. | The above example will run chkrootkit followed by updatedb at 4:45am daily - providing you have all listed apps installed. | ||
=== Cron Jobs Alternatives === | |||
Some hosting companies don’t allow access to cron, but you can find a lot of cron jobs alternatives which are free or paid. | |||
Here are some services: | |||
* [http://www.setcronjob.com SetCronJob] | |||
* [http://www.OnlineCronJobs.com OnlineCronJobs] | |||
* [http://www.CronJobs.org CronJobs.org] | |||
* [http://www.easyCron.com easyCron.com] | |||
---- | ---- | ||
[[category:UbuntuHelp]] | [[category:UbuntuHelp]] |
2011年1月13日 (四) 03:13的最新版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/CronHowto }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/af | • {{#if: UbuntuHelp:CronHowto|Afrikaans| [[::CronHowto/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ar | • {{#if: UbuntuHelp:CronHowto|العربية| [[::CronHowto/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/az | • {{#if: UbuntuHelp:CronHowto|azərbaycanca| [[::CronHowto/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/bcc | • {{#if: UbuntuHelp:CronHowto|جهلسری بلوچی| [[::CronHowto/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/bg | • {{#if: UbuntuHelp:CronHowto|български| [[::CronHowto/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/br | • {{#if: UbuntuHelp:CronHowto|brezhoneg| [[::CronHowto/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ca | • {{#if: UbuntuHelp:CronHowto|català| [[::CronHowto/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/cs | • {{#if: UbuntuHelp:CronHowto|čeština| [[::CronHowto/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/de | • {{#if: UbuntuHelp:CronHowto|Deutsch| [[::CronHowto/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/el | • {{#if: UbuntuHelp:CronHowto|Ελληνικά| [[::CronHowto/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/es | • {{#if: UbuntuHelp:CronHowto|español| [[::CronHowto/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/fa | • {{#if: UbuntuHelp:CronHowto|فارسی| [[::CronHowto/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/fi | • {{#if: UbuntuHelp:CronHowto|suomi| [[::CronHowto/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/fr | • {{#if: UbuntuHelp:CronHowto|français| [[::CronHowto/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/gu | • {{#if: UbuntuHelp:CronHowto|ગુજરાતી| [[::CronHowto/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/he | • {{#if: UbuntuHelp:CronHowto|עברית| [[::CronHowto/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/hu | • {{#if: UbuntuHelp:CronHowto|magyar| [[::CronHowto/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/id | • {{#if: UbuntuHelp:CronHowto|Bahasa Indonesia| [[::CronHowto/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/it | • {{#if: UbuntuHelp:CronHowto|italiano| [[::CronHowto/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ja | • {{#if: UbuntuHelp:CronHowto|日本語| [[::CronHowto/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ko | • {{#if: UbuntuHelp:CronHowto|한국어| [[::CronHowto/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ksh | • {{#if: UbuntuHelp:CronHowto|Ripoarisch| [[::CronHowto/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/mr | • {{#if: UbuntuHelp:CronHowto|मराठी| [[::CronHowto/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ms | • {{#if: UbuntuHelp:CronHowto|Bahasa Melayu| [[::CronHowto/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/nl | • {{#if: UbuntuHelp:CronHowto|Nederlands| [[::CronHowto/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/no | • {{#if: UbuntuHelp:CronHowto|norsk| [[::CronHowto/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/oc | • {{#if: UbuntuHelp:CronHowto|occitan| [[::CronHowto/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/pl | • {{#if: UbuntuHelp:CronHowto|polski| [[::CronHowto/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/pt | • {{#if: UbuntuHelp:CronHowto|português| [[::CronHowto/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ro | • {{#if: UbuntuHelp:CronHowto|română| [[::CronHowto/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/ru | • {{#if: UbuntuHelp:CronHowto|русский| [[::CronHowto/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/si | • {{#if: UbuntuHelp:CronHowto|සිංහල| [[::CronHowto/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/sq | • {{#if: UbuntuHelp:CronHowto|shqip| [[::CronHowto/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/sr | • {{#if: UbuntuHelp:CronHowto|српски / srpski| [[::CronHowto/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/sv | • {{#if: UbuntuHelp:CronHowto|svenska| [[::CronHowto/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/th | • {{#if: UbuntuHelp:CronHowto|ไทย| [[::CronHowto/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/tr | • {{#if: UbuntuHelp:CronHowto|Türkçe| [[::CronHowto/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/vi | • {{#if: UbuntuHelp:CronHowto|Tiếng Việt| [[::CronHowto/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/yue | • {{#if: UbuntuHelp:CronHowto|粵語| [[::CronHowto/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/zh | • {{#if: UbuntuHelp:CronHowto|中文| [[::CronHowto/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/zh-hans | • {{#if: UbuntuHelp:CronHowto|中文(简体)| [[::CronHowto/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:CronHowto | UbuntuHelp:CronHowto | {{#if: | :}}CronHowto}}/zh-hant | • {{#if: UbuntuHelp:CronHowto|中文(繁體)| [[::CronHowto/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:CronHowto|:CronHowto|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :CronHowto/zh | | {{#ifexist: CronHowto/zh | | {{#ifeq: {{#titleparts:CronHowto|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:CronHowto|1|-1|}} | zh | | }}
What is Cron?
Cron is a daemon used for scheduling tasks to be executed at a certain time. Each user has a crontab file, allowing them to specify actions and times that they should be executed. There is also a system crontab, allowing tasks such as log rotation and locate database updating to be done regularly. The following directions tell you how to set up scheduled tasks the traditional way using the command line, but it is much easier to use the Gnome Scheduled tasks tool (from the gnome-schedule package) in Applications --> System Tools.
What is Crontab?
A crontab is a simple text file that holds a list of commands that are to be run at specified times. These commands, and their related run times, are controlled by the cron daemon and are executed in the system's background. More information can be found by viewing the crontab's man page.
Using Cron
To use cron, simply add entries to your crontab file. To do this, open a terminal window and enter crontab -e
. An editor will open displaying your current crontab file. Edit the crontab using the format described below. Then save your changes and your new crontab will be installed. Exiting without saving will leave your crontab untouched.
Crontab Sections
Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out: minute (0-59), hour (0-23, 0 == midnight), day (1-31), month (1-12), weekday (0-6, 0 == Sunday), command
01 04 1 1 1 /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on January 1st plus every Monday in January. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code:
01 04 * * * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month. Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-seperated values can be used to run a command continuously. Code:
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June. The "/usr/bin/somedirectory/somecommand" text in the above examples indicates the task which will be run at the specified times. It is recommended that you use the full path to the desired commands as shown in the above examples. Enter which somecommand in the terminal to find the full path to somecommand. The crontab will begin running as soon as it is properly edited and saved. You may want to run a script some number of times per time unit. For example if you want to run it every 10 minutes use the following crontab entry (runs on minutes divisible by 10: 0, 10, 20, 30, etc.)
*/10 * * * * /usr/bin/somedirectory/somecommand
which is also equivalent to the more cumbersome
0,10,20,30,40,50 * * * * /usr/bin/somedirectory/somecommand
Crontab Options
- The -l option causes the current crontab to be displayed on standard output.
- The -r option causes the current crontab to be removed.
- The -e option is used to edit the current crontab using the editor specified by the EDITOR environment variable.
After you exit from the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically. The file is stored in /var/spool/cron/crontabs but should only be edited via the crontab command.
Enable User Level Cron
If the /etc/cron.allow file exists, then users must be listed in it in order to be allowed to run the crontab command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does, then users must not be listed in the /etc/cron.deny file in order to run crontab. In the case where neither file exists, the default on current Ubuntu (and Debian, but not some other Linux and UNIX systems) is to allow all users to run jobs with crontab. No cron.allow or cron.deny files exist in a standard Ubuntu install, so all users should have cron available by default, until one of those files is created. If a blank cron.deny file has been created, that will change to the standard behavior users of other operating systems might expect: cron only available to root or users in cron.allow.
Further Considerations
The above commands are stored in a crontab file belonging to your user account and executed with your level of permissions. If you want to regularly run a command requiring a greater level of permissions, set up a root crontab file using:
sudo crontab -e
Depending on the commands being run, you may need to expand the root users PATH variable by putting the following line at the top of their crontab file:
PATH=/usr/sbin:/usr/bin:/sbin:/bin
It is sensible to test that your cron jobs work as intended. One method for doing this is to set up the job to run a couple of minutes in the future and then check the results before finalising the timing. You may also find it useful to put the commands into script files that log their success or failure, eg:
echo "Nightly Backup Successful: $(date)" >> /tmp/mybackup.log
For more information, see the man pages for cron and crontab (man is detailed on the BasicCommands page). If your machine is regularly switched off, you may also be interested in at and anacron, which provide other approaches to scheduled tasks. For example, anacron offers simple system-wide directories for running commands hourly, daily, weekly, and monthly. Scripts to be executed in said times can be placed in /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/. All scripts in each directory are run as root, and a specific order to running the scripts can be specified by prefixing the scripts' filenames with numbers (see the man page for run-parts for more details). Although the directories contain periods in their names, run-parts will not accept a file name containing one and will fail silently when encountering them (bug #38022). Either rename the file or use a symlink (without a period) to it instead.
Troubleshooting and Common Problems
Edits to a user's crontab and jobs that are run on their behalf are all logged by default to /var/log/syslog and that's the first place to check if things are not running as you expect. When adding a new entry to a blank crontab, forgetting to add a newline at the end is a common source for the job not running. If the last line in the crontab does not end with a newline, no errors will be reported at edit or runtime, but that line will never run. See man crontab for more information. This has already been suggested as a bug. If a user was not allowed to execute jobs when their crontab was last edited, just adding them to the allow list won't do anything. The user needs to re-edit their crontab after being added to cron.allow before their jobs will run. When creating a crontab for the root user, the user name must be specified as a parameter after the date/time parameters. Accidentally including the user name that way in a user-specific crontab will result in trying to run the user's name as a command, rather than what was expected. Entries in cron may not run with the same environment, in particular the PATH, as you expect them to. Try using full paths to files and programs if they're not being located as you expect. The "%" character is used as newline delimiter in cron commands. If you need to pass that character into a script, you need to escape it as "\%". If you're having trouble running a GUI application using cron, see the GUI Applications section below.
Advanced Crontab
The Crontabs discussed above are user crontabs. Each of the above crontabs is associated with a user, even the system crontab which is associated with the root user. There are two other types of crontab. Firstly, as mentioned above anacron uses the run-parts command and /etc/cron.hourly, /etc/cron.weekly, and /etc/cron.monthly directories. However anacron itself is invoked from the /etc/crontab file. This file could be used for other cron commands, but probably shouldn't be. Here's an example line from a ficticious /etc/crontab:
00 01 * * * rusty /home/rusty/rusty-list-files.sh
This would run Rusty's command script as user rusty from his home directory. However, it is not usual to add commands to this file. While an experienced user should know about it, it is not recommended that you add anything to /etc/crontab. Apart from anything else, this could cause problem if the /etc/crontab file is affected by updates! Rusty could lose his command. The second type of crontab is to be found in /etc/cron.d. Within the directory are small named crontabs. The directory is often used by packages, and the small crontabs allows a user to be associated with the commands in them. Instead of adding a line to /etc/crontab which Rusty knows is not a good idea, Rusty might well add a file to /etc/cron.d with the name rusty, containing his cron line above. This would not be affected by updates but is a well known location. When would you use these alternate crontab locations? Well, on a single user machine or a shared machine such as a school or college server, a user crontab would be the way to go. But in a large IT department, where several people might look after a server, then /etc/cron.d is probably the best place to install crontabs - it's a central point and saves searching for them! You may not need to look at /etc/crontab or /etc/cron.d, let alone edit them by hand. But an experienced user should perhaps know about them and that the packages that he/she installs may use these locations for their crontabs.
Special strings
Cron also offers some special strings:
string | meaning |
@reboot | Run once, at startup. |
@yearly | Run once a year, "0 0 1 1 *". |
@annually | (same as @yearly) |
@monthly | Run once a month, "0 0 1 * *". |
@weekly | Run once a week, "0 0 * * 0". |
@daily | Run once a day, "0 0 * * *". |
@midnight | (same as @daily) |
@hourly | Run once an hour, "0 * * * *". |
Usage: "@reboot /path/to/execuable1" will execute /path/to/executable1 when the system starts. See "man 5 crontab" for more info.
GUI Applications
It is possible to run gui applications via cronjobs. This can be done by telling cron which display to use.
00 06 * * * env DISPLAY=:0 gui_appname
The env DISPLAY=:0 portion will tell cron to use the current display (desktop) for the program "gui_appname". And if you have multiple monitors, don't forget to specify on which one the program is to be run. For example, to run it on the first screen (default screen) use :
00 06 * * * env DISPLAY=:0.0 gui_appname
The env DISPLAY=:0.0 portion will tell cron to use the first screen of the current display for the program "gui_appname". Note: In Karmic(9.10), you have to enable X ACL for localhost to connect to for GUI applications to work.
~$ xhost +local: non-network local connections being added to access control list ~$ xhost access control enabled, only authorized clients can connect LOCAL: ...
Tips
crontab -e uses the EDITOR environment variable. to change the editor to your own choice just set that. You may want to set EDITOR in you .bashrc because many commands use this variable. Let's set the EDITOR to nano a very easy editor to use:
export EDITOR=nano
There are also files you can edit for system-wide cron jobs. The most common file is located at /etc/crontab, and this file follows a slightly different syntax than a normal crontab file. Since it is the base crontab that applies system-wide, you need to specify what user to run the job as; thus, the syntax is now:
minute(s) hour(s) day(s)_of_month month(s) day(s)_of_week user command
It is recommended, however, that you try to avoid using /etc/crontab unless you need the flexibility offered by it, or if you'd like to create your own simplified anacron-like system using run-parts for example. For all cron jobs that you want to have run under your own user account, you should stick with using crontab -e to edit your local cron jobs rather than editting the system-wide /etc/crontab.
Crontab Example
Below is an example of how to setup a crontab to run updatedb, which updates the slocate database: Open a term, type "crontab -e" (without the double quotes) and press enter. Type the following line, substituting the full path of the application you wish to run for the one shown below, into the editor:
45 04 * * * /usr/bin/updatedb
Save your changes and exit the editor. Crontab will let you know if you made any mistakes. The crontab will be installed and begin running if there are no errors. That's it. You now have a cronjob setup to run updatedb, which updates the slocate database, every morning at 4:45. Note: The double-ampersand (&&) can also be used in the "command" section to run multiple commands consecutively.
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
The above example will run chkrootkit followed by updatedb at 4:45am daily - providing you have all listed apps installed.
Cron Jobs Alternatives
Some hosting companies don’t allow access to cron, but you can find a lot of cron jobs alternatives which are free or paid. Here are some services: