Without much formal programming, a bit of gleaning, trial, and error I've managed to mash together a daemon that is quite useful to me (BTW, just yesterday this involved a cron job and 2 shell scripts :-)). Anyhow, this has two functional problems for me:
1. How can I avoid 'the addrs are different' on first run without using a counter?
2. My logic is off but how can I store value of former loop in myipaddr_then in order
to do a compare with myipaddr_now ?
For initial testing, I can change b.txt at any time. The host, my.example.com is my server.
Code:
import time, os, sys
from daemon import runner
class App:
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/foo.pid'
self.pidfile_timeout = 5
def run(self):
myipaddr_then = 0
while True:
# myipaddr_now = os.popen('curl -s http://icanhazip.com').read()
myipaddr_now = os.popen('curl -s http://my.example.com/b.txt').read()
myipaddr_now = myipaddr_now.rstrip()
if myipaddr_now == myipaddr_then:
# print "ok"
return
else:
print "the addrs are different"
time.sleep(10)
myipaddr_then = myipaddr_now
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()