linux - How to make a python script run 15 minutes after system startup? -
i wrote script python reminds me open whatsapp web whenever chrome browser opened. run script each time, have use terminal make python script run. want make script run automatically 15-20 minutes after system starts.
here's code:
import webbrowser import os import signal import tkmessagebox subprocess import check_output crontab import crontab def get_pid(name): return int(check_output(["pidof","-s",name])) '''script open whatsapp web whenever chrome 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 phone chrome open whatsapp!") i tried editing crontab, did not prove helpful. there way so?
edit-1 here's updated code, , still doesn't seem work. when manually run script, works, otherwise not(on reboot).
#!/usr/bin/env python import webbrowser import os import signal import tkmessagebox subprocess import check_output import time def get_pid(name): return int(check_output(["pidof","-s",name])) '''script open whatsapp web whenever chrome opened''' name="chrome" while(1): time.sleep(600) while(get_pid(name)): webbrowser.open('http://web.whatsapp.com') tkmessagebox.showinfo(title="greetings", message="connect phone chrome open whatsapp!") print "hey" apart this, added following lines in crontab:
@reboot /usr/bin/python /path/to/whatsapp.py & further, made .conf file looks :
start on runlevel [2345] stop on runlevel [!2345] exec /path/to/whatsapp.py and recent log file looks this:
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 phone chrome 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 , no $display environment variable where path/to path script.
simple solutions best: make script run on boot, , sleep 15 min.
Comments
Post a Comment