导读 crondtab进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。这篇文章主要介绍了Shell中的定时任务 crontab,需要的朋友可以参考下
crondtab定时任务

crondtab是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,crondtab进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。

如何让shell脚本每天定时执行?

1.新建需要定时执行的shell脚本,这里为date.sh脚本。

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "hello world"
echo "----------------------------------------------------------------------------"
endDate=`date +"%Y-%m-%d %H:%M:%S"`
echo "★[$endDate] Successful"
echo "当前时间":$(date +"%Y-%m-%d %H:%M:%S") >> /www/wwwroot/date.txt
echo "----------------------------------------------------------------------------"

2.将这个date.sh脚本添加到定时任务中,直接运行“crontab -e”命令添加就行。

[root@localhost ~]# crontab -e
*/1 * * * * /www/wwwroot/date.sh > /dev/null 2>&1
*/1 * * * * /www/wwwroot/date.sh >> /www/wwwroot/result.txt 2>&1
  
# 每天10:30执行
30 10 * * * /www/wwwroot/date.sh

3.最后,我们再查看一下输出文件date.txt和crontab日志。

[root@localhost ~]# tail -n 5 /www/wwwroot/date.txt
[root@localhost ~]# tail -f /var/log/cron
总结

以上所述是小编给大家介绍的Shell中的定时任务 crontab,希望对大家有所帮助!

原文来自:https://www.jb51.net/article/176920.htm

本文地址:https://www.linuxprobe.com/shell-linux-crontab.html编辑:向金平,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/