最近一直想着 让一个应用程序可以通过windows 定时任务的方式启动,但是这小小的愿望也没有很快完成。写的脚本 比如bat ,以及python ,以及在 python 里通过各种方式 启动应用程序,手动执行都是可以完成应用程序启动的,但是放在定时任务里,定时任务执行后,都是无法启动应用程序的。
先看看bat 脚本 ,具体意思我也不太明白,但是可以运行, 并且已经在运行的程序, 不会重复执行。但是放在定时里就不行。
@echo off
tasklist /FI "IMAGENAME eq MyApp.exe" 2>NUL | findstr /I /C:"myapp.exe">NUL
if %errorlevel%==1 start "" "E:\myapp.exe"
vbs 脚本目前没有尝试, 虽然之前使用vbs 完成了应用程序的启动,时间久了,也忘了, 另外也不想折腾vbs 了。
看看 python 的代码,之前能够手动运行 并启动 应用程序,但是 不能放在定时任务里运行并启动应用程序。
app=auto.PaneControl(searchDepth=1,Name='Program Manager').ListControl(searchDepth=2,Name='桌面').ListItemControl(searchDepth=1,Name='Google Chrome')
#start_application(application_path)
#app.Click()
def start_application(application_path):
#subprocess.Popen(application_path)
#os.system(application_path)
os.system('cmd.exe /k E:\\myapp.exe ')
最终的解决办法是, 我在windows7 的电脑上安装 supervisord-win windows 版的守护进程管理工具,以及结合windows 定时任务, 以及python 脚本 完成了 指定应用程序的 检测,以及启动运行。参考 以下文章
windows配置使用supervisor_windows supervisor-CSDN博客
https://www.cnblogs.com/liuhaidon/p/12217153.html
- 安装 supervisord-win
pip install supervisor-win
-
准备配置文件 supervisord.conf
[inet_http_server] ; inet (TCP) server disabled by default port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface [supervisord] logfile=C:\\Users\\lws\\desktop\\supervisord.log logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (log level;default info; others: debug,warn,trace) pidfile=%(ENV_TMP)s\\supervisord.pid ; (supervisord pidfile;default supervisord.pid) nodaemon=false ; (start in foreground if true;default false) silent=false ; no logs to stdout if true; default false minfds=1024 ; (min. avail startup file descriptors;default 1024) minprocs=200 ; (min. avail process descriptors;default 200) [supervisorctl] serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket ;username=chris ; should be same as in [*_http_server] if set ;password=123 ; should be same as in [*_http_server] if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available [program:myapp.exe] command= E:\\myapp.exe directory=c:\\Users\\lws\\desktop ; directory to cwd to before exec (def no cwd) autostart=true ; start at supervisord start (default: true) startsecs=1 ; # of secs prog must stay up to be running (def. 1) startretries=3 ; max # of serial start failures when starting (default 3) autorestart=unexpected ; 意外退出才会启动when to restart if exited after running (def: unexpected) redirect_stderr=true ; redirect proc stderr to stdout (default false) stdout_logfile= C:\\Users\\lws\\desktop\\myapp.log ; stdout log path, NONE for none; default AUTO stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10) [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
-
开始测试在命令行启动 supervisord , 并观测 myapp 是否启动。启动过程中可能会遇到各种问题,根据抛出的问题 百度,应该可以解决。运行这个程序 还需要安装 pywin32-306 如下所示
-
命令行前台启动
supervisord.exe -c C:\\Users\\lws\\desktop\\supervisord.conf
5. 后台启动 脚本 supervisord.vbs
Set ws= CreateObject("Wscript.Shell")
ws.run "supervisord.exe -c c:\\Users\\lws\\desktop\\supvisord.conf" ,vbhide
6. python 定时任务脚本 start_app.py
import psutil
import subprocess
import os
import platform
#import uiautomation as auto
import requests as req
import time
'''
app=auto.PaneControl(searchDepth=1,Name='Program Manager').ListControl(searchDepth=2,Name='桌面').ListItemControl(searchDepth=1,Name='Google Chrome')
#start_application(application_path)
#app.Click()
#os.system('C:\\Users\\lws\\desktop\\supervisord.vbs')
def start_application(application_path):
#subprocess.Popen(application_path)
#os.system(application_path)
os.system('cmd.exe /k E:\\myapp.exe ')
'''
def log_record(file,content):
with open(file,'a') as f:
f.write(content)
def is_process_running(process_name):
i=0
for proc in psutil.process_iter(['name']):
try:
if process_name.lower() in proc.info['name'].lower():
i= i + 1
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
return i
content=''
def main():
process_name1= 'supervisord.exe'
process_name = "myapp.exe"
i= is_process_running(process_name1)
j= is_process_running(process_name)
if i == 1 and j < 1:
date_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
content=f"{date_time} {process_name} supervisord.exe 在运行,启动 myapp.exe ..."
a=req.get('http://127.0.0.1:9001/index.html?processname=Z-Bot.exe&action=start')
log_record('C:\\Users\\lws\\desktop\\start_app.log',content)
elif i == 0 and j < 1 :
date_time=time.strftime('%Y:%m:%d %H:%M:%S',time.localtime())
content=f"{date_time} {process_name} supervisord 没有运行,启动supervisord"
os.system('C:\\Users\\lws\\desktop\\supervisord.vbs')
log_record('C:\\Users\\lws\\desktop\\start_app.log',content)
elif i > 0 and j > 0 :
date_time=time.strftime('%Y:%m:%d %H:%M:%S',time.localtime())
content=f'{date_time} supervisord.exe and myapp.exe is all running'
log_record('C:\\Users\\lws\\desktop\\start_app.log',content)
if __name__ == "__main__":
main()