Mer Balds
Mer Balds

Reputation: 23

Python script to continue running

I am new to python programming. I want my code to continue running and continue checking my ip address. `

#!/usr/bin/env python
import os

hostname = "192.168.254.102" #example
response = os.system("ping -c 1 " + hostname)

and then check the response...

if response == 0:
    print(hostname, 'is up!')
    os.system("sudo python ../aquarium/nightlight_on.py 1")
else:
    print(hostname, 'is down!')

`

Basically, I cant the code to check my phone ip address when i got home then the script will turn on the light. I tested the script and it works well if you run it in the terminal but you need to sudo python scriptname.py first Thank you

Upvotes: 2

Views: 2536

Answers (1)

rofls
rofls

Reputation: 5115

You could use the python schedule open source project like this:

#!/usr/bin/env python
def job():   
    import os
    hostname = "192.168.254.102" #example
    response = os.system("ping -c 1 " + hostname)
    if response == 0:
        print(hostname, 'is up!')
        os.system("sudo python ../aquarium/nightlight_on.py 1")
    else:
        print(hostname, 'is down!')

import schedule
schedule.every(10).seconds.do(job)

And then run your python script as a background process with the unix & flag:

$ sudo python yourScript.py &

You can install schedule with pip. You will still have to restart that process when your computer reboots, or make an upstart or systemd job to handle that.

Upvotes: 2

Related Questions