Putting on the roof

One of the big physical jobs left for Matilda and Hans was putting the roof back on. They realised that they could not do the job by themselves so they wandered down the Adam and Eve Inn to look for volunteers. It turned out that everyone wanted to help so the barkeeper closed the pub and he and the snake came along too.

 

The helpers were of various use, the Dwarves spent most of the time arguing and Farmer Hogg kept saying things like "I'd not have started from here". However the barkeeper and snake were very useful. Whenever the barkeeper wanted the snake to do something he'd command "Python Start" and whenever a job was done he'd say "Python Stop". The roof was quickly done and Hans and Matilda posed for a photo.

CottageRoofIsOn.jpg

They thanked everyone who in turn decided to go back to the inn to celebrate. Hans and Matilda had plenty to do so celebrating would have to wait.

 

Hans had forgotten

Hans realised he'd forgotten a critical step in the process. How to start and stop his Python script. He looked back over his notes and realised that he could use bits from the Handshaking code and from the Fail2Ban setup.

 

Init script

An initialisation script was created.

 

#!/bin/sh /etc/rc.common
# Initialise Python Listening on Serial Port

workdir=/mnt/sda1/Python
START=8
STOP=14

start() {
    cd $workdir
    /usr/bin/python /mnt/sda1/Python/GetWeather.py &
    echo "Weather Server started."
}

stop() {
    pid=`ps -ef | grep '[p]ython /mnt/sda1/Python/GetWeather.py' | awk '{ print $1 }'`
    echo $pid
    kill $pid
    sleep 2
    echo "Weather Server killed."
}





 

Changes to Python Code

The python code needed minor changes so that it would respond to the shutdown request

# Handle exit and kill from OS
def set_exit_handler(func):
    signal.signal(signal.SIGTERM, func)
def on_exit(sig, func=None):
    print "exit handler triggered"
    sys.exit(1)

# Run program
if __name__ == '__main__':
    set_exit_handler(on_exit)
    sys.exit(main())





 

Testing

The init script was tested with

/etc/init.d/weather-daemon start





and

ps





to check it was running

 

Then shutdown using

/etc/init.d/weather-daemon stop





 

Finally, it was installed using

/etc/init.d/weather-daemon enable




 

Next: Enchanted Objects Design Challenge - Planning and Building

 

References

http://www.pietervanos.net/knowledge/start-python-script-from-init-d/

https://wolfpaulus.com/jounal/software/pythonlauncher/

http://wiki.openwrt.org/doc/techref/initscripts