(Rpm) Linux timed task Crontab Timing task Crontab Linux Command Detailed command Detailed

Turn: https: //www.cnblogs.com/intval/p/5763929.html

Timing task Crontab Linux command Detailed

 

linux system is a cron (crond) This system controlled service. We had had a lot of Linux systems on top of planned work, so this system service is enabled by default. In addition, since the users themselves can set a scheduled task, so, Linux system also provides user control commands scheduled task: crontab command.

A, crond Profile

crond is under linux to periodically perform some task or a daemon pending certain events, and scheduled tasks under the windows Similarly, when the installation is complete operating system, installed by default this service tool, and will automatically start crond process, crond process checks per minute on a regular basis whether there are tasks to be performed, if there are tasks to be performed automatically perform this task.

Task scheduling of Linux into two categories, task scheduling system and user tasks scheduling.

Task scheduling system: the system work to be performed periodically, such as writing data to the hard disk cache, log clean-up. There is a crontab file in the / etc directory, this is the task scheduling system configuration files.

/ Etc / crontab file contains the following lines:

cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=HOME=/

# run-parts

51 * * * * root run-parts /etc/cron.hourly

24 7 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly


The first four lines are used to configure the environment variables crond task to run, the first line SHELL variable specifies the system which shell to use, here is bash, the second line system PATH variable specifies the path to execute a command, the third line MAILTO variable specified the crond task execution information will be sent via e-mail to the root user, if MAILTO variable is empty, said they did not send task execution information to the user, HOME variable fourth line specifies the main use when executing commands or scripts table of Contents. Meaning sixth to ninth lines indicated in the next sub-sections describe in detail. There is not much to say.

User task scheduling: Users work to be performed on a regular basis, such as user data backup, regular e-mail reminders. Users can use the tools to customize their own crontab scheduled tasks. All user-defined crontab files are stored in / var / spool / cron directory. The same name as the user name of its file.

User file permissions:

file:

/etc/cron.deny

Description:

The document listed in the user allowed to use crontab command

file:

/etc/cron.allow

Description:

Allows the users listed in the command file using crontab

file:

/ Var / spool / cron /

Description:

User crontab file stored in directories, with your name

crontab file meanings:

crontab file created by the user, each row represents a task, each field representing each row of a setting, its format is divided into six fields, set the time period is the period of the first five, the sixth paragraph is command section to be executed in the following format:

minute hour day month week command

among them:

minute: min expressed, may be any integer from 0 to 59.

hour: the hour, may be any integer from 0 to 23.

day: the date, can be any integer from 1 to 31.

month: the month, can be any integer from 1-12.

week: represents the day of the week, it can be any integer from 0 to 7, where 0 or 7 represents Sunday.

command: Command to be executed, the system can command, you can also write their own script files.

In each of the above fields, you can also use the following special characters:

An asterisk (*): On behalf of all possible values, such as month if the field is an asterisk, it means that the command is executed operation after a month constraints to meet other fields.

Comma (,): a comma-separated values ​​can be used to specify a list of ranges, e.g., "1,2,5,7,8,9"

The bar (-): can represent an integer in the range of a bar between the integer, e.g., "2-6" indicates "2,3,4,5,6"

Forward slash (/): n frequency intervals can slash specified time, for example, "0-23 / 2" is performed once every two hours. At the same time slash can be used with an asterisk, for example * / 10, if used in minute field representing performed once every ten minutes.

Two, crond service

Installation crontab:

yum install crontabs

Service Instructions:

/ Sbin / service crond start // Start Service

/ Sbin / service crond stop // shut down service

/ Sbin / service crond restart // restart the service

/ Sbin / service crond reload // reload the configuration

/ Sbin / service crond status // start the service


See if crontab service is set to boot, execute the command:

ntsysv

Join at startup:

chkconfig –level 35 crond on

Three, crontab command Detailed

1. Format:

crontab [-u user] file

crontab [-u user] [ -e | -l | -r ]

2. Function:

By crontab command, we can execute the specified system command or shell script script at fixed intervals. Unit time interval can be minutes, hours, days, months, weeks and any combination of the above. This command is very close set of log periodic analysis of data backup or work.

3. Command parameters:

-u user: to set a user's crontab service, for example, "- u ixdba" represents a set ixdba user's crontab service, this parameter generally have root user to run.

file: file is the file name of the command, said it would file as a crontab task list file and loaded into crontab. If you do not specify the file on the command line, crontab command will accept commands entered on standard input (keyboard), and load them crontab.

-e: crontab edit the contents of a user's files. If you do not specify a user, then edit the current user's crontab file.

-l: Displays the contents of a user's crontab file, if you do not specify a user, then displays the current contents of the user's crontab file.

-r: delete a user's crontab file from / var / spool / cron directory, if you do not specify a user, the default is to delete the current user's crontab file.

-i: When you delete a user's crontab file to the confirmation prompt.

4. Common methods:

1) Create a new crontab file

Before considering submitting a crontab file to cron process, the first thing to do is to set the environment variable EDITOR. According to its cron process to determine which editor to edit the crontab file. 99% of the UNIX and LINUX users are using vi, if you are too, then you edit the $ HOME directory profile file, in which the addition of this line:

EDITOR = vi; export EDITOR

Then save and exit. May wish to create a file called <user> cron file, where <user> is the user name, for example, davecron. Add the following contents in the file.

# (put your own initials here)echo the date to the console every

# 15minutes between 6pm and 6am

0,15,30,45 18-06 * * * /bin/echo ‘date’ > /dev/console

Save and exit. 5 domain sure front separated by spaces.

In the above example, the system for 15 minutes every time the current time is output to the console. If the system crashes or hangs, the last time from the displayed you can see at a glance what time the system stopped working. In some systems, represented by tty1 console, we can make the appropriate modifications to the examples above, according to the actual situation. In order to submit crontab file you just created, you can put the newly created file as a parameter cron command:

$ crontab davecron

Now that the file has been submitted to the cron process, which will run once every 1 5 minutes.

At the same time, a copy of the newly created files have been placed in / var / spool / cron directory, the file name is the user name (ie dave).

2) List the crontab file

To list the crontab file, you can use:

$ crontab -l

0,15,30,45,18-06 * * * /bin/echo `date` > dev/tty1

You will see above similar content. You can use this method in the $ HOME directory to make a crontab file backup:

$ crontab -l > $HOME/mycron

Thus, once not accidentally delete a crontab file, can be used for the method is about a speedy recovery.

3) Edit the crontab file

If you want to add, delete or edit an entry in the crontab file, and EDI TO R environment variable and set to vi, you can use vi to edit the crontab file, the appropriate command is:

$ crontab -e

As can modify the crontab file and exit using the vi editor like any other file. If you modify certain entries or add new entries, then when you save the file, cron will be necessary integrity checks. If one domain appeared beyond the permitted range of values, it will prompt you.

When we edit the crontab file, chances will add a new entry. For example, an addition of the following:

# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month

30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;

Now save and exit. Best to add a comment on each entry crontab file, so that you can know its function, the running time, more importantly, who know that this is the user's job.

Now let's use the crontab -l command said earlier it lists all the information:

$ crontab -l

# (crondave installed on Tue May 4 13:07:43 1999)

# DT:ech the date to the console every 30 minites

0,15,30,45 18-06 * * * /bin/echo `date` > /dev/tty1

# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month

30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;

4) Remove the crontab file

To delete a crontab file, you can use:

$ crontab -r

5). Crontab recover lost files

If you do not accidentally delete a crontab file, assuming that you have a backup in their own $ HOME directory, you can copy it to / var / spool / cron / <username>, where <username> is the user name. If you can not complete the copy due to permission issues, you can use:

$ crontab <filename>

Where, <filename> is a copy of your $ HOME directory in the file name.

I suggest that you save a copy of the file in your $ HOME directory. I have had a similar experience, there are several mistakenly deleted the crontab file (because e r key immediately to the right of the key). This is why some system documentation is not recommended to edit the crontab file, but editing a copy of the file, and then re-submit a new file.

Some variants crontab some weird, so use the crontab command to be extra careful. If you missed any of the options, crontab may open an empty file, or looks like an empty file. Then knock delete key to exit, do not press <Ctrl-D>, otherwise you will lose crontab file.

5. Use Case

Example 1: every 1 minute to perform a command
commands:
* * * * * command

Example 2: 3 hour and 15 minutes execute
command:
3,15 * * * * Command

Example 3: In the first 15 minutes and 3 perform 8:00 to 11:00 of
the command:
3,15 Command 8-11 * * *

Example 4: every day 8:00 to 11:00 of the first 15 minutes and 3 execute
the command:
3,15 8-11 * / 2 * * Command

Example 5: Each of the first 15 minutes and 3 perform Monday 8:00 to 11:00 of
the command:
3,15. 1 Command 8-11 * *

Example 6: night 21:30 smb restart 
command:
30 * * * 21 is the restart /etc/init.d/smb


Example 7: 1 month, 10, 22, the 4:45 smb restart 
command:
45. 4 1,10,22 * * the restart /etc/init.d/smb


Example 8: every Saturday and Sunday 1:10 to restart the smb
command:
10 * 1 * 6,0 /etc/init.d/smb restart


Example 9:18 per day: 00-23: every 30 minutes between 00 smb restart 
command:
0,30 18-23 * * * the restart /etc/init.d/smb


Example 10: Every Saturday night, 11: 00 pm to restart the smb 
command:
0 23 * 6 * /etc/init.d/smb restart


Example 11: smb per hour restart 
command:
* * / * * * /etc/init.d/smb the restart. 1


Example 12: between 11 pm to 7 am, every hour smb restart 
command:
* 23-7 / /etc/init.d/smb the restart. 1 * * *

Example 13: Monthly No. 4 and 11:00 from Monday to Wednesday to restart the smb 
command:
0 11 4 * Mon-Wed /etc/init.d/smb restart

Example 14: 1 January of 4:00 smb restart 
command:
0 4 1 jan * /etc/init.d/smb restart


Example 15: execute scripts per hour in /etc/cron.hourly directory
command:
01 * * * * root RUN-Parts /etc/cron.hourly
Description:
RUN-Parts of this argument, if this parameter is removed, then back on you can write a script name to run, instead of the directory name


Fourth, the use precautions

Note environment variable
and sometimes we create a crontab, but they can not perform this task automatically, manually perform this task, but there is no problem, this is usually because there is no configuration environment variable in the crontab file caused.

When you define multiple scheduled tasks in the crontab file, a problem in need of special attention is the environment variable settings, because when we manually perform a task, is carried out in the current shell environment, of course, the program can find the environment variables, and the system automatic scheduling tasks, it will not load any environment variables, therefore, we need all environment variables to specify the tasks required to run the crontab file, so that the system would be no problem when performing task scheduling.

Do not assume that special circumstances require cron know, it does not really know. So make sure you provide all the necessary paths and environment variables shelll script, except for some global variables set automatically. Therefore, note the following three points:

1) write a script file path when it comes to the global path;

2) to use java script execution or other environmental variables, variables introduced into the environment through source command, such as:

cat start_cbp.sh

#!/bin/sh

source /etc/profile

export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf

/usr/local/jboss-4.0.5/bin/run.sh -c mev &

3) When manually execute the script OK, but crontab life and death is not performed. In this case the environment variable must be bold suspected to blame, and you can try to solve the problem directly into the environment variable in the crontab. Such as:

0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh

Note that the user's mail system to clean up the log
of each task scheduler is finished, the system will output the task information sent via e-mail to the current users of the system, so that over time, the log information will be very large, may affect the normal operation of the system, therefore, each task will redirect process is very important.

For example, it may be provided in the form of crontab file, ignoring the log output:

0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1

"/ Dev / null 2> & 1" represents a first standard output to / dev / null, and then redirect the standard error output to the standard, because the standard output has been redirected to / dev / null, and therefore the weight also standard error directed to / dev / null, so the log output problem is solved.

System-level and user-level task scheduling task scheduling
system-level tasks to complete some major scheduled maintenance operations, user-level task scheduling system is mainly to complete some of the tasks a user-defined, user-level task scheduling can put the system-level task scheduling is done (not propose to do so), but the reverse is not true, root user task scheduling operation can be set by "crontab -uroot -e", also scheduled tasks can be written to / etc / crontab file directly, to note that, if you want to define a scheduled reboot task system, it must be the task into / etc / crontab file, even if you create a scheduled restart of the system under the root user task it is invalid.

Other considerations
cron job newly created, will not be executed immediately, at least two minutes before the execution. If you restart cron is executed immediately.

When crontab suddenly fails, you can try /etc/init.d/crond restart to solve the problem. Or check the log to see a job has not been implemented / being given tail -f / var / log / cron.

Do Freeze run crontab -r. It deletes files from the user's Crontab Crontab directory (/ var / spool / cron). Delete all of the user's crontab gone.

In the crontab% have a special meaning, represents the meaning of a new line. If it must be escaped use \%, often used as date '+% Y% m% d' in crontab Lane will not be implemented, should be replaced by date '+ \% Y \% m \% d' .

linux system is a cron (crond) This system controlled service. We had had a lot of Linux systems on top of planned work, so this system service is enabled by default. In addition, since the users themselves can set a scheduled task, so, Linux system also provides user control commands scheduled task: crontab command.

A, crond Profile

crond is under linux to periodically perform some task or a daemon pending certain events, and scheduled tasks under the windows Similarly, when the installation is complete operating system, installed by default this service tool, and will automatically start crond process, crond process checks per minute on a regular basis whether there are tasks to be performed, if there are tasks to be performed automatically perform this task.

Task scheduling of Linux into two categories, task scheduling system and user tasks scheduling.

Task scheduling system: the system work to be performed periodically, such as writing data to the hard disk cache, log clean-up. There is a crontab file in the / etc directory, this is the task scheduling system configuration files.

/ Etc / crontab file contains the following lines:

cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=HOME=/

# run-parts

51 * * * * root run-parts /etc/cron.hourly

24 7 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly


The first four lines are used to configure the environment variables crond task to run, the first line SHELL variable specifies the system which shell to use, here is bash, the second line system PATH variable specifies the path to execute a command, the third line MAILTO variable specified the crond task execution information will be sent via e-mail to the root user, if MAILTO variable is empty, said they did not send task execution information to the user, HOME variable fourth line specifies the main use when executing commands or scripts table of Contents. Meaning sixth to ninth lines indicated in the next sub-sections describe in detail. There is not much to say.

User task scheduling: Users work to be performed on a regular basis, such as user data backup, regular e-mail reminders. Users can use the tools to customize their own crontab scheduled tasks. All user-defined crontab files are stored in / var / spool / cron directory. The same name as the user name of its file.

User file permissions:

file:

/etc/cron.deny

Description:

The document listed in the user allowed to use crontab command

file:

/etc/cron.allow

Description:

Allows the users listed in the command file using crontab

file:

/ Var / spool / cron /

Description:

User crontab file stored in directories, with your name

crontab file meanings:

crontab file created by the user, each row represents a task, each field representing each row of a setting, its format is divided into six fields, set the time period is the period of the first five, the sixth paragraph is command section to be executed in the following format:

minute hour day month week command

among them:

minute: min expressed, may be any integer from 0 to 59.

hour: the hour, may be any integer from 0 to 23.

day: the date, can be any integer from 1 to 31.

month: the month, can be any integer from 1-12.

week: represents the day of the week, it can be any integer from 0 to 7, where 0 or 7 represents Sunday.

command: Command to be executed, the system can command, you can also write their own script files.

In each of the above fields, you can also use the following special characters:

An asterisk (*): On behalf of all possible values, such as month if the field is an asterisk, it means that the command is executed operation after a month constraints to meet other fields.

Comma (,): a comma-separated values ​​can be used to specify a list of ranges, e.g., "1,2,5,7,8,9"

The bar (-): can represent an integer in the range of a bar between the integer, e.g., "2-6" indicates "2,3,4,5,6"

Forward slash (/): n frequency intervals can slash specified time, for example, "0-23 / 2" is performed once every two hours. At the same time slash can be used with an asterisk, for example * / 10, if used in minute field representing performed once every ten minutes.

Two, crond service

Installation crontab:

yum install crontabs

Service Instructions:

/ Sbin / service crond start // Start Service

/ Sbin / service crond stop // shut down service

/ Sbin / service crond restart // restart the service

/sbin/service crond reload //重新载入配置

/sbin/service crond status //启动服务


查看crontab服务是否已设置为开机启动,执行命令:

ntsysv

加入开机自动启动:

chkconfig –level 35 crond on

三、crontab命令详解

1.命令格式:

crontab [-u user] file

crontab [-u user] [ -e | -l | -r ]

2.命令功能:

通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本。时间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合。这个命令非常设合周期性的日志分析或数据备份等工作。

3.命令参数:

-u user:用来设定某个用户的crontab服务,例如,“-u ixdba”表示设定ixdba用户的crontab服务,此参数一般有root用户来运行。

file:file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。如果在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。

-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。

-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。

-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。

-i:在删除用户的crontab文件时给确认提示。

4.常用方法:

1). 创建一个新的crontab文件

在 考虑向cron进程提交一个crontab文件之前,首先要做的一件事情就是设置环境变量EDITOR。cron进程根据它来确定使用哪个编辑器编辑 crontab文件。9 9 %的UNIX和LINUX用户都使用vi,如果你也是这样,那么你就编辑$ HOME目录下的. profile文件,在其 中加入这样一行:

EDITOR=vi; export EDITOR

然后保存并退出。不妨创建一个名为<user> cron的文件,其中<user>是用户名,例如, davecron。在该文件中加入如下的内容。

# (put your own initials here)echo the date to the console every

# 15minutes between 6pm and 6am

0,15,30,45 18-06 * * * /bin/echo ‘date’ > /dev/console

保存并退出。确信前面5个域用空格分隔。

在 上面的例子中,系统将每隔1 5分钟向控制台输出一次当前时间。如果系统崩溃或挂起,从最后所显示的时间就可以一眼看出系统是什么时间停止工作的。在有些 系统中,用tty1来表示控制台,可以根据实际情况对上面的例子进行相应的修改。为了提交你刚刚创建的crontab文件,可以把这个新创建的文件作为 cron命令的参数:

$ crontab davecron

现在该文件已经提交给cron进程,它将每隔1 5分钟运行一次。

同时,新创建文件的一个副本已经被放在/var/spool/cron目录中,文件名就是用户名(即dave)。

2). 列出crontab文件

为了列出crontab文件,可以用:

$ crontab -l

0,15,30,45,18-06 * * * /bin/echo `date` > dev/tty1

你将会看到和上面类似的内容。可以使用这种方法在$ H O M E目录中对crontab文件做一备份:

$ crontab -l > $HOME/mycron

这样,一旦不小心误删了crontab文件,可以用上一节所讲述的方法迅速恢复。

3). 编辑crontab文件

如果希望添加、删除或编辑crontab文件中的条目,而E D I TO R环境变量又设置为v i,那么就可以用v i来编辑crontab文件,相应的命令为:

$ crontab -e

可以像使用v i编辑其他任何文件那样修改crontab文件并退出。如果修改了某些条目或添加了新的条目,那么在保存该文件时, c r o n会对其进行必要的完整性检查。如果其中的某个域出现了超出允许范围的值,它会提示你。

我们在编辑crontab文件时,没准会加入新的条目。例如,加入下面的一条:

# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month

30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;

现在保存并退出。最好在crontab文件的每一个条目之上加入一条注释,这样就可以知道它的功能、运行时间,更为重要的是,知道这是哪位用户的作业。

现在让我们使用前面讲过的crontab -l命令列出它的全部信息:

$ crontab -l

# (crondave installed on Tue May 4 13:07:43 1999)

# DT:ech the date to the console every 30 minites

0,15,30,45 18-06 * * * /bin/echo `date` > /dev/tty1

# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month

30 3 1,7,14,21,26 * * /bin/find -name “core’ -exec rm {} \;

4). 删除crontab文件

要删除crontab文件,可以用:

$ crontab -r

5). 恢复丢失的crontab文件

如果不小心误删了crontab文件,假设你在自己的$ H O M E目录下还有一个备份,那么可以将其拷贝到/var/spool/cron/<username>,其中<username>是用户名。如果由于权限问题无法完成拷贝,可以用:

$ crontab <filename>

其中,<filename>是你在$ H O M E目录中副本的文件名。

我建议你在自己的$ H O M E目录中保存一个该文件的副本。我就有过类似的经历,有数次误删了crontab文件(因为r键紧挨在e键的右边)。这就是为什么有些系统文档建议不要直接编辑crontab文件,而是编辑该文件的一个副本,然后重新提交新的文件。

有些crontab的变体有些怪异,所以在使用crontab命令时要格外小心。如果遗漏了任何选项,crontab可能会打开一个空文件,或者看起来像是个空文件。这时敲delete键退出,不要按<Ctrl-D>,否则你将丢失crontab文件。

5.使用实例

实例1:每1分钟执行一次command
命令:
* * * * * command

实例2:每小时的第3和第15分钟执行
命令:
3,15 * * * * command

实例3:在上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 * * * command

实例4:每隔两天的上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 */2 * * command

实例5:每个星期一的上午8点到11点的第3和第15分钟执行
命令:
3,15 8-11 * * 1 command

实例6:每晚的21:30重启smb 
命令:
30 21 * * * /etc/init.d/smb restart


实例7:每月1、10、22日的4 : 45重启smb 
命令:
45 4 1,10,22 * * /etc/init.d/smb restart


实例8:每周六、周日的1 : 10重启smb
命令:
10 1 * * 6,0 /etc/init.d/smb restart


实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb 
命令:
0,30 18-23 * * * /etc/init.d/smb restart


实例10:每星期六的晚上11 : 00 pm重启smb 
命令:
0 23 * * 6 /etc/init.d/smb restart


实例11:每一小时重启smb 
命令:
* */1 * * * /etc/init.d/smb restart


实例12:晚上11点到早上7点之间,每隔一小时重启smb 
命令:
* 23-7/1 * * * /etc/init.d/smb restart

实例13:每月的4号与每周一到周三的11点重启smb 
命令:
0 11 4 * mon-wed /etc/init.d/smb restart

实例14:一月一号的4点重启smb 
命令:
0 4 1 jan * /etc/init.d/smb restart


实例15:每小时执行/etc/cron.hourly目录内的脚本
命令:
01 * * * * root run-parts /etc/cron.hourly
说明:
run-parts这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是目录名了


四、使用注意事项

注意环境变量问题
有时我们创建了一个crontab,但是这个任务却无法自动执行,而手动执行这个任务却没有问题,这种情况一般是由于在crontab文件中没有配置环境变量引起的。

在 crontab文件中定义多个调度任务时,需要特别注意的一个问题就是环境变量的设置,因为我们手动执行某个任务时,是在当前shell环境下进行的,程 序当然能找到环境变量,而系统自动执行任务调度时,是不会加载任何环境变量的,因此,就需要在crontab文件中指定任务运行所需的所有环境变量,这 样,系统执行任务调度时就没有问题了。

不要假定cron知道所需要的特殊环境,它其实并不知道。所以你要保证在shelll脚本中提供所有必要的路径和环境变量,除了一些自动设置的全局变量。所以注意如下3点:

1)脚本中涉及文件路径时写全局路径;

2)脚本执行要用到java或其他环境变量时,通过source命令引入环境变量,如:

cat start_cbp.sh

#!/bin/sh

source /etc/profile

export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf

/usr/local/jboss-4.0.5/bin/run.sh -c mev &

3)当手动执行脚本OK,但是crontab死活不执行时。这时必须大胆怀疑是环境变量惹的祸,并可以尝试在crontab中直接引入环境变量解决问题。如:

0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh

注意清理系统用户的邮件日志
每条任务调度执行完毕,系统都会将任务输出信息通过电子邮件的形式发送给当前系统用户,这样日积月累,日志信息会非常大,可能会影响系统的正常运行,因此,将每条任务进行重定向处理非常重要。

例如,可以在crontab文件中设置如下形式,忽略日志输出:

0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1

“/dev/null 2>&1”表示先将标准输出重定向到/dev/null,然后将标准错误重定向到标准输出,由于标准输出已经重定向到了/dev/null,因此标准错误也会重定向到/dev/null,这样日志输出问题就解决了。

系统级任务调度与用户级任务调度
系 统级任务调度主要完成系统的一些维护操作,用户级任务调度主要完成用户自定义的一些任务,可以将用户级任务调度放到系统级任务调度来完成(不建议这么 做),但是反过来却不行,root用户的任务调度操作可以通过“crontab –uroot –e”来设置,也可以将调度任务直接写入/etc /crontab文件,需要注意的是,如果要定义一个定时重启系统的任务,就必须将任务放到/etc/crontab文件,即使在root用户下创建一个 定时重启系统的任务也是无效的。

其他注意事项
新创建的cron job,不会马上执行,至少要过2分钟才执行。如果重启cron则马上执行。

当crontab突然失效时,可以尝试/etc/init.d/crond restart解决问题。或者查看日志看某个job有没有执行/报错tail -f /var/log/cron。

千万别乱运行crontab -r。它从Crontab目录(/var/spool/cron)中删除用户的Crontab文件。删除了该用户的所有crontab都没了。

在crontab中%是有特殊含义的,表示换行的意思。如果要用的话必须进行转义\%,如经常用的date ‘+%Y%m%d’在crontab里是不会执行的,应该换成date ‘+\%Y\%m\%d’。

Guess you like

Origin www.cnblogs.com/sz-xioabai/p/11519761.html