CentOS7设置定时任务 每隔30分钟执行一次命令


ref   https://blog.csdn.net/xiangxianghehe/article/details/78149094

 

一.安装 crontabs服务并设置开机自启:

yum install crontabs
systemctl enable crond
systemctl start crond

systemctl stop crond
 

二.设置用户自定义定时任务:

vi /etc/crontab

可以看到:

# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 

即:

分钟(0-59) 小时(0-23) 日(1-31) 月(11-12) 星期(0-6,0表示周日) 用户名 要执行的命令1

 

 

每隔30分钟root执行一次updatedb命令:

*/30 * * * * root updatedb

每天早上5点定时重启系统:

0 5 * * * root reboot1

每隔三秒执行一次/home/somedir目录下的scripts.sh脚本:

## For excuting scripts.sh every 3 seconds##on 2014-10-15 */1 * * * * /home/somedir/scripts.sh */1 * * * * sleep 3 && /home/somedir/scripts.sh */1 * * * * sleep 6 && /home/somedir/scripts.sh */1 * * * * sleep 9 && /home/somedir/scripts.sh */1 * * * * sleep 12 && /home/somedir/scripts.sh */1 * * * * sleep 15 && /home/somedir/scripts.sh */1 * * * * sleep 18 && /home/somedir/scripts.sh */1 * * * * sleep 21 && /home/somedir/scripts.sh */1 * * * * sleep 24 && /home/somedir/scripts.sh */1 * * * * sleep 27 && /home/somedir/scripts.sh */1 * * * * sleep 30 && /home/somedir/scripts.sh */1 * * * * sleep 33 && /home/somedir/scripts.sh */1 * * * * sleep 36 && /home/somedir/scripts.sh */1 * * * * sleep 39 && /home/somedir/scripts.sh */1 * * * * sleep 42 && /home/somedir/scripts.sh */1 * * * * sleep 45 && /home/somedir/scripts.sh */1 * * * * sleep 48 && /home/somedir/scripts.sh */1 * * * * sleep 51 && /home/somedir/scripts.sh */1 * * * * sleep 54 && /home/somedir/scripts.sh */1 * * * * sleep 57 && /home/somedir/scripts.sh

三.保存生效:

#加载任务,使之生效 crontab /etc/crontab #查看任务 crontab -l

# stop
 systemctl stop crond


 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM