个人工具

“UbuntuHelp:CronHowto”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
(新页面: {{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. ...)
 
第4行: 第4行:
  
 
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.
 +
 +
== 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. A crontab entry gives a patten specifying when to run and the action to perform, e.g.:
+
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 layed 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>
5 3 * * * /usr/bin/apt-get update
+
01 04 1 1 1 /usr/bin/somedirectory/somecommand
 
</nowiki></pre>
 
</nowiki></pre>
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:
+
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>
*/5 9-17 * * mon,tue,wed,thu,fri echo "Are we there yet?" | wall
+
01 04 * * * /usr/bin/somedirectory/somecommand
</nowiki></pre>
+
or remind you of a birthday at 9 in the morning on the 10th January every year:
+
<pre><nowiki>
+
0 9 10 jan * echo "It's your mother's birthday today!" > ~/readme
+
 
</nowiki></pre>
 
</nowiki></pre>
  
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.
+
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:
 +
 
 
<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.
  
To view the current contents of your crontab file, type:
+
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.
<pre><nowiki>
+
crontab -l
+
</nowiki></pre>
+
  
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 Options ===
<pre><nowiki>
+
crontab -e
+
</nowiki></pre>
+
  
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:
+
* The -l option causes the current crontab to be displayed on standard output.
<pre><nowiki>
+
* The -r option causes the current crontab to be removed.
crontab -r
+
* The -e option is used to edit the current crontab using the editor specified by the EDITOR environment variable.
</nowiki></pre>
+
  
It is possible to run gui applications via cronjobs. This can be done by telling cron which display to use.
+
After you exit from the editor, the modified crontab will be checked for accuracy and, if there are no errors, installed automatically.
<pre><nowiki>
+
The file is stored in ''/var/spool/cron/crontabs'' but should only be edited via the crontab command.
00 06 * * * env DISPLAY=:0. gui_appname
+
</nowiki></pre>
+
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 ===
 
=== Further Considerations ===
第66行: 第65行:
 
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 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).
+
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).
  
 
== Tips ==
 
== Tips ==
第80行: 第79行:
 
</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'''.
 
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'''.
<pre><nowiki>
 
 
</nowiki></pre>
 
 
== 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:
 
  
 +
It is possible to run gui applications via cronjobs. This can be done by telling cron which display to use.
 
<pre><nowiki>
 
<pre><nowiki>
crontab -e
+
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".
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
+
 
+
<pre><nowiki>
+
01 04 1 1 1 /usr/bin/somedirectory/somecommand
+
</nowiki></pre>
+
 
+
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:
+
 
+
<pre><nowiki>
+
01 04 * * * /usr/bin/somedirectory/somecommand
+
</nowiki></pre>
+
 
+
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:
+
 
+
<pre><nowiki>
+
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
+
</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 "/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 ===
 
=== Crontab Example ===
第158行: 第106行:
 
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.
 
----
 
----
[[category:CategoryDocumentation]]
 
 
 
 
 
[[category:CategoryDocumentation]]
 
[[category:CategoryDocumentation]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2007年11月22日 (四) 12:17的版本


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.

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.

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 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-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. 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 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).

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.

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".

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.