frank February 2016

How to make a python script run 15 minutes after system startup?

I wrote a script a python which reminds me to open WhatsApp Web whenever chrome browser is opened. To run this script each time, I have to use the terminal to make the python script run. I want to make the script run automatically 15-20 minutes after the system starts.

Here's my code:

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!")

I tried editing the crontab, but that did not prove very helpful. Is there any way to do so?

Edit-1 Here's my updated code, and it still doesn't seem to work. When I manually run the script, it works, but otherwise not(on reboot).

    #!/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"

Apart from this, I added the following lines in crontab:

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

Further, I made a .conf file which looks like this :

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

exec /path/to/whatsapp.py

And the most recent log file fo

Answers


kazagistar February 2016

Simple solutions are often the best: Make the script run on boot, and then sleep for 15 min.


Tim Mullen February 2016

I'm not sure doing cron-related tasks via python will help you solve your issue. Crontab takes a special syntax for scheduling a command after a reboot, so you would likely do a

@reboot python /my/python/path/message.py

If you want the bulk of this code to run 15 minutes after reboot, probably tell the python script itself to sleep for the fifteen minutes:

import time
time.sleep(900) # 900 = 60 * 15

Post Status

Asked in February 2016
Viewed 3,132 times
Voted 8
Answered 2 times

Search




Leave an answer