linux定时计划的学习

来源:互联网 发布:mac的flash屏蔽 编辑:程序博客网 时间:2024/05/21 14:58

计划任务的启动布骤
1确保服务启动
service crond status
chkconfig –list |grep crond
2制定计划任务
crontab -e : 修改 crontab 文件,如果文件不存在会自动创建。
crontab -l : 显示 crontab 文件。
crontab -r : 删除 crontab 文件。
crontab -ir : 删除 crontab 文件前提醒用户。
格式
minute hour day-of-month month-of-year day-of-week commands
**#每天(凌晨4:02)执行/etc/cron.daily内的脚本
0 2 4 * * root run-parts /etc/cron.daily
每隔10分钟同步时间
*/10 * * * ntpdate
3重启计划任务
service crond restart

举例说明:
需求:每天6:00-8:00把/usr/local/test.txt文件的每一行的第一个域加到/usr/local/test2.txt文件中
crontab -e 后在里面输入:
0 6-8 * * * /usr/local/test/crontab_test.sh
退出保存,编写shelll脚本

#!/bin/bashline=`sed -n '$=' /usr/local/test.txt`echo $lineawk 'NR==1,NR=='$line' {print $1}' /usr/local/test.txt > /usr/local/test2.txt