特殊:Badtitle/NS100:CronHowto
文章出处: |
{{#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.
Using Cron
To use cron, simply add entries to your crontab file. A crontab entry gives a patten specifying when to run and the action to perform, e.g.:
5 3 * * * /usr/bin/apt-get update
The first part of the crontab entry describes when the action will be performed. There are five fields, separated by whitespace, each of which accept a number, an asterisk (*), or appropriate text. The fields specify, in order, the minute (0-59), hour (0-23, 0 == midnight), day of month (1-31), month (1-12) and day of week (0-6, 0 == Sunday). The month and day of week fields allow the use of an abbreviation, such as "jan" for January or "thu" for Thursday.
The example above will execute "/usr/bin/apt-get update" every day of every month at 03:05 (cron assumes 24 hour time). You could have cron annoy you every 5 minutes throughout your work day (9am-5pm) with a message:
*/5 9-17 * * mon,tue,wed,thu,fri echo "Are we there yet?" | wall
or remind you of a birthday at 9 in the morning on the 10th January every year:
0 9 10 jan * echo "It's your mother's birthday today!" > ~/readme
Comma-seperated 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.
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.
To view the current contents of your crontab file, type:
crontab -l
To edit the file, with the editor specified in your environment (which defaults to vim - :q! is the command to escape without saving if you get stuck and need to read up on it or change your editor), use:
crontab -e
To remove the file, with the editor specified in your environment (which defaults to vim - :q! is the command to escape without saving if you get stuck and need to read up on it or change your editor), use:
crontab -r
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".
When you exit the editor, the new crontab file will be installed. The file is stored in /var/spool/cron/crontabs but should only be edited via the crontab command.
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 interest in at (part of the Ubuntu base install) and anacron (found in the Universe repository) that 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' file names with numbers (see the man page for run-parts for more details).
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.
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. We will run through a simple crontab example later.
How Does It Work?
The system maintains a crontab for each user on the system. In order to edit or create a crontab, you must use the text editor that is specified by your system. The nano text editor is the default text editor on Ubuntu 6.06 systems. This text editor must be opened with the command crontab using the -e option. To create a crontab open a term and type:
crontab -e
The nano text editor will open with a blank window for the desired times and commands to be entered. Each line represents a seperate crontab entry - also known as a "cronjob". If you are not familiar with the nano text editor you should obtain a tutorial for it as that information is beyond the scope of this tutorial.
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 layed 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 any Monday which falls on January 1st. 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-seperated 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. The crontab will begin running as soon as it is properly edited and saved.
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.
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.