gpt4 book ai didi

python - 如何让python脚本在系统启动15分钟后运行?

转载 作者:太空宇宙 更新时间:2023-11-04 05:24:26 25 4
gpt4 key购买 nike

我用 python 编写了一个脚本,它会在 Chrome 浏览器打开时提醒我打开 WhatsApp Web。为了每次运行这个脚本,我必须使用终端来运行 python 脚本。我想让脚本在系统启动后15-20分钟自动运行。

这是我的代码:

import webbrowser
import os
import signal
import tkMessageBox
from subprocess import check_output
from crontab import CronTab


def get_pid(name):
return int(check_output(["pidof","-s",name]))
'''Script to open whatsapp web whenever chrome is opened'''

cron=CronTab()
job=cron.new(command='/usr/bin/echo')
job.minute.during(1,50).every(1)
name="chrome"
if (get_pid(name)):
webbrowser.open('http://web.whatsapp.com')
tkMessageBox.showinfo(title="Greetings", message="Connect your phone to chrome to open whatsapp!")

我尝试编辑 crontab,但这并没有什么帮助。有什么办法可以做到吗?

编辑-1这是我更新的代码,但它似乎仍然不起作用。当我手动运行脚本时,它可以工作,但否则不能(重新启动时)。

    #!/usr/bin/env python
import webbrowser
import os
import signal
import tkMessageBox
from subprocess import check_output
import time

def get_pid(name):
return int(check_output(["pidof","-s",name]))


'''Script to open whatsapp web whenever chrome is opened'''

name="chrome"
while(1):
time.sleep(600)
while(get_pid(name)):
webbrowser.open('http://web.whatsapp.com')
tkMessageBox.showinfo(title="Greetings", message="Connect your phone to chrome to open whatsapp!")
print "Hey"

除此之外,我在 crontab 中添加了以下几行:

@reboot /usr/bin/python /path/to/whatsapp.py &

此外,我制作了一个如下所示的 .conf 文件:

start on runlevel [2345]
stop on runlevel [!2345]

exec /path/to/whatsapp.py

最新的日志文件如下所示:

SyntaxError: invalid syntax
Traceback (most recent call last):
File "/path/to/whatsapp.py", line 17, in <module>
while(get_pid(name)):
File "/path/to/whatsapp.py", line 10, in get_pid
return int(check_output(["pidof","-s",name]))
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['pidof', '-s', 'chrome']' returned non-zero exit status 1
Traceback (most recent call last):
File "/path/to/whatsapp.py", line 20, in <module>
tkMessageBox.showinfo(title="Greetings", message="Connect your phone to chrome to open whatsapp!")
File "/usr/lib/python2.7/lib-tk/tkMessageBox.py", line 83, in showinfo
return _show(title, message, INFO, OK, **options)
File "/usr/lib/python2.7/lib-tk/tkMessageBox.py", line 72, in _show
res = Message(**options).show()
File "/usr/lib/python2.7/lib-tk/tkCommonDialog.py", line 44, in show
w = Frame(self.master)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2537, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2049, in __init__
BaseWidget._setup(self, master, cnf)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2024, in _setup
_default_root = Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

其中path/to显然是脚本的路径。

最佳答案

简单的解决方案通常是最好的:让脚本在启动时运行,然后休眠 15 分钟。

关于python - 如何让python脚本在系统启动15分钟后运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35276529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com