import datetime
import time
import os
import pymysql
def doSth():
os.system("python ./脚本1.py")
print('Waiting a minute ...')
# print('test')
conn = pymysql.Connect(
host='192.0.9.169',
port=5507,
user='writer',
passwd='Apsdf',
db='api_data',
charset='utf8'
)
cur = conn.cursor()
cur.execute("""select * from table1""")
conn.commit()
cur.close()
conn.close()
# 假装做这件事情需要一分钟
time.sleep(60)
def main(h=23, m=0):
'''h表示设定的小时,m为设定的分钟'''
while True:
# 判断是否达到设定时间,例如23:00
while True:
now = datetime.datetime.now()
# 到达设定时间,结束内循环
if now.hour==h and now.minute==m:
break
# 不到时间就等20秒之后再次检测
time.sleep(20)
# 做正事,一天做一次
doSth()
if __name__ == '__main__':
main()
Python如何实现一个守护进程来定时每天在十点的时候跑shell脚本
TaskScheduler.py
import datetime
import time
import os
def doSth():
print('hello')
n=os.system('./desktop/apache-tomcat-8.5.28/bin/startup.sh')
print('thats all')
time.sleep(60)
def main(h=10,m=52):
while True:
while True:
now = datetime.datetime.now()
print(now)
if now.hour == h and now.minute == m:
break
time.sleep(20)
doSth()
if __name__ == '__main__' :
main()
运行时
nohup python TaskScheduler.py &
Linux Crontab 定时任务
https://www.runoob.com/w3cnote/linux-crontab-tasks.html
https://blog.youkuaiyun.com/yangmaodi8303/article/details/81095617
https://blog.youkuaiyun.com/generalfu/article/details/114933838
查看进程是否存在,不存在退出shell
#!/bin/bash/
waitProcessFinish(){
while true
do
process=`ps -ef |grep $1 |grep -v grep`
if["$process"==""]; then
echo "no peocess,`date+%Y-%m-%d-%H:%M:%S`";
break;
else
echo "process exit"
sleep 5
fi
done
echo "process finish"
}
waitProcessFinish testing_tools.py