新增调度任务可用两种方法:
1)、在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出。
test.sh,每分钟执行一次
(/1 * * * * /test/dingshi/test.sh >> /test/dingshi/result.log)
2)、直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务。
(/1 * * * * root /test/dingshi/test.sh >> /test/dingshi/result.log)
crontab -e配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务
定时任务配置:
-
1、编写需要定时跑的任务脚本
cd /test/dingshi vim test.sh #!/bin/bash echo "test+$(date)";
-
2、添加脚本到定时任务
crontab -e 输入定时命令 */1 * * * * /test/dingshi/test.sh >> /test/dingshi/result.log
-
3、定时任务配置完成。
linux系统:
查看系统是否有crontab服务:
service crond status/start/stop/restart
下载crontab服务:
yum install vixie-cron vixie-cron软件包是cron的主程序;
yum install crontabs crontabs软件包是用来安装、卸装、或列举用来驱动查看crontab服务是否已设置为开机启动:
ntsysv加入开机自动启动:
chkconfig -level 35 crond on
查看系统再跑的定时任务:
crontab -l查看test用户的所有调度任务
crontab -l -u test查看删除定时任务:
crontab -r
定时任务时间语法(cron):
分 小时 日月星期 命令
0-59 0-23 1-31 1-12 0-6 command
- eg:0 18 * * * 每天下午六点
- eg: */15 12 * * * 每天12点开始,每十五分钟执行一次
特殊符号:
- “*”----取值范围内的数字,
- “/”----”每”
- “-”----从某个数字到某个数字
- “,”---- 分开几个离散的数字