个人工具

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

来自Ubuntu中文

跳转至: 导航, 搜索
第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.
 
 
== What Is 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.
 
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 use cron, simply add entries to your crontab file.  
 
 
=== Crontab Sections ===
 
=== 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:
 
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
 
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
 
01 04 1 1 1 /usr/bin/somedirectory/somecommand
 
</nowiki></pre>
 
</nowiki></pre>
 
 
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:
 
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
 
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.
 
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:
 
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.
 
 
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.
 
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 ===
 
=== Crontab Options ===
 
 
* The -l option causes the current crontab to be displayed on standard output.
 
* The -l option causes the current crontab to be displayed on standard output.
 
* The -r option causes the current crontab to be removed.
 
* 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.
 
* 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.
 
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.
 
The file is stored in ''/var/spool/cron/crontabs'' but should only be edited via the crontab command.
 
 
=== 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行: 第44行:
 
</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 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).
 
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 ==
 
 
'''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:
 
'''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
 
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:
 
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:
 
<pre><nowiki>
 
<pre><nowiki>
第79行: 第54行:
 
</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'''.
 
 
It is possible to run gui applications via cronjobs. This can be done by telling cron which display to use.
 
It is possible to run gui applications via cronjobs. This can be done by telling cron which display to use.
 
<pre><nowiki>
 
<pre><nowiki>
第85行: 第59行:
 
</nowiki></pre>
 
</nowiki></pre>
 
The ''env DISPLAY=:0.'' portion will tell cron to use the current display (desktop) for the program "gui_appname".
 
The ''env DISPLAY=:0.'' portion will tell cron to use the current display (desktop) for the program "gui_appname".
 
 
=== 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.
 
----
 
----

2007年11月30日 (五) 16:37的版本


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.