crontab: daemon regular tasks to the nearest minute, designs seconds we generally write the script -> Alarm equivalent to
a log file: ll / var / log / cron *
Edit the file: vim / etc / crontab
process: ps -ef | grep crond ==> /etc/init.d/crond restart
role: regular backup, real-time backup
Common command parameters
1
2
3
4
5
6
7
8
|
usage:
crontab
[-u user]
file
crontab
[-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's
crontab
)
-l (list user's
crontab
)
-r (delete user's
crontab
)
-i (prompt before deleting user's
crontab
)
-s (selinux context)
|
Cron job classification
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.
In the / etc / crontab file, this is the task scheduling system configuration files.
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.
In crontab files are stored in / var / spool / cron directory. Consistent with its file name and user name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
1.系统定时任务:例如清理系统日志,清理系统缓存 -->不过多的关注
查询系统定时处理任务的路径:
路径1:
cd
/etc/logrotate
.d/ -->可以写定时任务
less
syslog
路径2:
cat
/etc/crontab
-->不推荐使用,但是可以看格式
路径3:
ls
/etc/
|
grep
cron
*
anacrontab
cron
.d -->同路径2 ,可以写定时任务
cron
.daily
cron
.deny -->控制普通用户使用定时任务
crontab
cron
.hourly
cron
.monthly
crontab
cron
.weekly
2.用户的定时任务 -->关注重点
|
crontab file content analysis
cat /etc/crontab
The first four lines are used to configure the environment variables to run crond task
The first line SHELL variable specifies the system which shell to use, here is bash
The second line specifies the path to the PATH variable command system to perform
The third line MAILTO variable specifies the crond task execution information will be sent via e-mail to the root user
If the MAILTO variable is empty, it means not to send information to the user task execution
HOME variable fourth line specifies the home directory to use when executing commands or scripts.
An asterisk (*): On behalf of all possible values, such as the month field is an asterisk, it indicates 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.
Summary:
representation of the number 2 best to use Arabic numerals display
week and day is best not to use
a scheduled task to add annotations
can be directed to a log file or empty file
regular tasks must be an absolute path, and the directory must exist before the results
crontab Service be sure to open the run
crontab log path
1
2
3
|
# 【日志是按照天排列的】
ll
/var/log/cron
*
/var/log/cron
只会记录是否执行了某些计划的脚本,但是具体执行是否正确以及脚本执行过程中的一些信息则linux会每次都发邮件到该用户下。
|
1
|
less
/var/spool/mail/root
|
Frequently used commands show
Installation crontab:
1
|
yum
install
crontabs
|
crontab Service Instructions:
1
2
3
4
5
6
7
|
/sbin/service
crond start
//
启动服务
/sbin/service
crond stop
//
关闭服务
/sbin/service
crond restart
//
重启服务
/sbin/service
crond reload
//
重新载入配置
|
View crontab service status:
1
|
service crond status
|
Crontab manually start the service:
1
|
service crond status
|
See if crontab service is set to boot, execute the command:
1
2
|
方法一: 界面启动 ntsysv
方法二: 加入开机自动启动: chkconfig –level 35 crond on
|
crontab -l to view a list of scheduled tasks
Editing a scheduled task
1
|
crontab
–e
|
1
|
==》vim
/var/spool/cron/root
|
Output time every 2 minutes to a file
1
|
*
/2
* * * *
echo
`
date
` >> $HOME>
test
.txt
|
crontab -r delete a scheduled task
1
2
3
|
==> 从
/var/spool/cron
目录中删除用户的
crontab
文件
==> 如果不指定用户,则默认删除当前用户的
crontab
文件
crontab
–i 在删除用户的
crontab
文件时给确认提示
|
Crontab file backup
1
|
crontab
-l > $HOME
/mycron
|
Recover lost crontab file
1
2
3
|
如果不小心误删了
crontab
文件,假设你在自己的$ H O M E目录下还有一个备份,那么可以将其拷贝到
/var/spool/cron/
<username>,其中<username>是用户名。如果由于权限问题无法完成拷贝,可以用:
crontab
<filename> 其中,<filename>是你在$ H O M E目录中副本的文件名。
有些
crontab
的变体有些怪异,所以在使用
crontab
命令时要格外小心。如果遗漏了任何选项,
crontab
可能会打开一个空文件,或者看起来像是个空文件。这时敲delete键退出,不要按<Ctrl-D>,否则你将丢失
crontab
文件。
|
Execute scripts in a directory /etc/cron.hourly per hour
1
|
0 * * * * root run-parts
/etc/cron
.hourly
|
Once every 2 minutes of Internet time synchronization
1
|
echo
"*/2 * * * * /usr/bin/ntpstat time.windows.com >/dev/null 2>&1"
>>
/var/spool/cron/root
|
3-5,17-20 every 30 minutes a day to perform a script
1
|
echo
"*/30 [3-5],[17-20] * * * /bin/sh /home/omc/h.sh >/dev/null 2>&1"
>>
/var/spool/cron/root
|
23:00 the same day, the next day 0:00 to 7:00 every 1 minutes execute a script
1
|
* 23,00-07
/1
* * *
/bin/sh
/home/omc
.h.sh
|
Every year in April a week from Monday to Wednesday 11:00 execute scripts
1
|
00 11 * 4 1-3
/bin/sh
/home/omc/h
.sh
|
crontab Notes
1. Note that the environment variable
A problem when defining multiple scheduled tasks crontab file, you need to pay special attention to is the environment variable settings
1
2
3
4
5
6
7
8
9
10
|
# 脚本中涉及文件路径时写全局路径;
# 脚本执行要用到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 &
# 当手动执行脚本OK,但是crontab死活不执行时。可以尝试在crontab中直接引入环境变量解决问题。
0 * * * * .
/etc/profile
;
/bin/sh
/var/www/java/audit_no_count/bin/restart_audit
.sh
|
2. The system-level and user-level task scheduling task scheduling
1
|
root用户的任务调度操作可以通过“
crontab
–uroot –e”来设置,也可以将调度任务直接写入
/etc/crontab
文件,需要注意的是,如果要定义一个定时重启系统的任务,就必须将任务放到
/etc/crontab
文件,即使在root用户下创建一个定时重启系统的任务也是无效的。
|
3. Other Considerations
1
2
3
|
当
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’
|
4. Timing task commissioning
1
2
3
4
|
1.增加执行任务的频率调试
2.调整系统时间调试任务,提前5分钟 -->不用于生产环境
3.通过脚本日志输出调试定时 任务
4.注意一些任务命令带来的问题 -->确保命令的正确性
|
5.crontab Proverbs
1
2
3
4
5
6
7
8
9
10
11
|
1.环境变量问题,例如
crontab
不能识别Java的环境变量
crontab
执行shell时,只能识别为数不多的环境变量,普通的环境变量是无法识别的,所以在编写shell时,最好使用
export
重新声明变量,确保脚本执行。
2.命令的执行最好用脚本
3.脚本权限加
/bin/sh
,规范路径
/server/scripts
4.时间变量用反斜线转义,最好用脚本
5.定时任务添加注释
6.>
/dev/null
2>&1 ==>&>
/dev/null
,别随意打印日志文件
7.定时任务里面的程序脚本尽量用全路径
8.避免不必要的程序以及命令输出
9.定时任务之前添加注释
10.打包到文件目录的上一级
|