我写了一个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显然是脚本的路径。
答案 0 :(得分:0)
简单的解决方案通常是最好的:让脚本在启动时运行,然后休眠15分钟。
答案 1 :(得分:0)
我不确定通过python做与cron相关的任务会帮助你解决问题。 Crontab采用特殊语法在重新启动后调度命令,因此您可能会执行
@reboot python /my/python/path/message.py
如果你希望大部分代码在重启后15分钟运行,可能会告诉python脚本本身在15分钟内休眠:
import time
time.sleep(900) # 900 = 60 * 15