Shell脚本实现每个工作日定时执行指定程序(学习记录)
来源:互联网 发布:网络大v引导舆情走向 编辑:程序博客网 时间:2024/05/19 03:45
我们可能会遇到这样的情景:必须在每个工作日定时执行Linux服务器上的某个程序。
那么有没有办法实现这一功能呢?答案是肯定的。我们可以运用Shell脚本简单实现这一功能。
原理很简单,Shell脚本内部每隔1秒查询一次当前时间、日期以及星期数,当检测到当前星期数week非0或6(0代表星期天,6代表星期六)且时间curTime大于指定时间startTime时,执行指定的程序program。
为了保证每天仅执行一次指定程序program,还引用了变量isFirstTime做标记。具体代码如下:
#!/bin/sh#Section configuration(配置部分)#Task Time ,example:203000(Time 20:30:00);190000(Time 19:00:00);startTime=113200#the programs you want to execute(要执行的程序)program=ps#Section promgram (程序执行部分)perDate=$(date "+%Y%m%d")isNewDay=1isFirstTime=1echo 'Task schedule Time: ('$startTime') program: ('$program') Waiting...'while true ; do curTime=$(date "+%H%M%S") curDate=$(date "+%Y%m%d") #Check week day(周末不执行) week=`date +%w` if [ $week -eq 6 ] || [ $week -eq 0 ];then isNewDay=0 sleep 1 continue else #check and run script(工作日执行) if [ "$isNewDay" -eq "1" ];then if [ "$curTime" -gt "$startTime" ];then if [ "$isFirstTime" -eq "0" ];then echo 'The program ('$program') Running...' $program echo 'The program ('$program') Stopped...' fi isNewDay=0 else if [ "$isFirstTime" -eq "1" ];then echo 'New Day: ('$curDate') Task schedule Time: ('$startTime') Waiting...' isFirstTime=0 fi fi else #new day start(开始新的一天) if [ "$curDate" -gt "$perDate" ];then echo 'New Day: ('$curDate') Task schedule Time: ('$startTime') Waiting...' isNewDay=1 perDate=$curDate fi fi sleep 1 fidone
该Shell脚本的功能为每个工作日的11点32分执行一次ps命令,执行的效果如下图所示。
当然该脚本只是为了演示这一定时原理,实际应用中可以指定其他的程序或者脚本,并利用nohup命令让其后台运行。
原文地址:http://ju.outofmemory.cn/entry/2058
0 0
- Shell脚本实现每个工作日定时执行指定程序(学习记录)
- Shell脚本实现每个工作日定时执行指定程序
- shell脚本定时执行php程序
- Java程序定时执行shell脚本
- linux 定时执行shell脚本
- linux 定时执行shell脚本
- crontab 定时执行shell脚本
- crontab 定时执行shell脚本
- linux 定时执行shell脚本
- linux 定时执行shell脚本
- linux 定时执行shell脚本
- linux 定时执行shell脚本
- linux定时执行shell脚本
- LINUX定时执行shell脚本
- shell脚本定时、后台执行
- linux 定时执行shell脚本
- crontab 定时执行shell脚本
- linux 定时执行shell 脚本
- 坑。。待填
- 安装php5
- DataInputStream的过期方法readLine()
- js_day3
- Android下载repo文件报错
- Shell脚本实现每个工作日定时执行指定程序(学习记录)
- 在Github和Git上fork之简单指南
- GUN学习笔记之make变量
- 医疗博文锚文本
- Linux makefile 教程 非常详细,且易懂
- Android中传递对象的三种方法
- mimetype1
- caret包应用之一:数据预处理
- eclipse hibernate tools安装和入门使用