OpenWRT: Start a python script at boot time
The following script will start a python weather parsing script at boot time (that script not included here). Place the script in
This can also be started with /etc/init.d/weather start. You can stop the init script from starting up with
See the entry at the OpenWRT wiki.
UPDATE 2017-12-18:
Modified the init.d script to stop and restart:
#!/bin/sh
# Copyright (C) 2008 OpenWrt.org
# Updated December 18, 2017 bob murphy/thisoldgeek
START=99
start() {
sleep 5 # make sure boot process is done, no more console msgs
. /etc/profile
python /opt/scripts/wunderground_parse.py &
echo "Weather VFD App Started"
}
stop() {
pid=`ps -ef | grep '[p]ython /opt/scripts/wunderground_parse.py' | awk '{ print $1 }'`
echo $pid
kill $pid
sleep 2
echo "Weather VFD App Stopped"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/weather {start|stop|restart}"
exit 1
esac
exit 0
/etc/init.d/weather
#!/bin/sh /etc/rc.commonSTART=99 will create an entry in rc.d named S99weather when the script is enabled.
# Copyright (C) 2008 OpenWrt.org
START=99
start() {
sleep 5 # make sure boot process is done, no more console msgs
echo "Weather VFD App Started"
. /etc/profile
echo $PATH
python /opt/scripts/wunderground_parse.py &
}
chmod +x /etc/init.d/weatherThe profile command is [dot] [SPACE]/etc/profile. This will invoke the environment variables which otherwise wouldn't be included at boot time. Those variables will include the
/etc/init.d/weather enable #places entry S99weather in /etc/rc.d/
LD_LIBRARY_PATHand other env variables.
This can also be started with /etc/init.d/weather start. You can stop the init script from starting up with
/etc/init.d/weather disableYou could optionally add other commands to this startup script.
See the entry at the OpenWRT wiki.
UPDATE 2017-12-18:
Modified the init.d script to stop and restart:
#!/bin/sh
# Copyright (C) 2008 OpenWrt.org
# Updated December 18, 2017 bob murphy/thisoldgeek
START=99
start() {
sleep 5 # make sure boot process is done, no more console msgs
. /etc/profile
python /opt/scripts/wunderground_parse.py &
echo "Weather VFD App Started"
}
stop() {
pid=`ps -ef | grep '[p]ython /opt/scripts/wunderground_parse.py' | awk '{ print $1 }'`
echo $pid
kill $pid
sleep 2
echo "Weather VFD App Stopped"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: /etc/init.d/weather {start|stop|restart}"
exit 1
esac
exit 0
crontab -e
ReplyDeleteand adding @reboot line into the cron file works pretty good on Raspberry Pi, after re-powering:)
Thanks a lot.
ReplyDelete. /etc/profile is very helpful for me.
what is the "&" for after the python /opt/scripts/wunderground_parse.py &
ReplyDeleteRun the job in the background. Get more info by doing a Google search:
ReplyDelete"linux run script with &" or "linux run script with ampersand"
Thank you very much grandpa! I used whole afternoon and not get it work. Now finally it works after reading your article.
ReplyDeleteThank you grandpa! This has disturbed me for a whole afternoon and it works now finally!
ReplyDeleteYou are welcome, Chris! I'm glad this still worked for you. My post is old now, and the way to do things can change quickly....
ReplyDeleteGreat article!! It still works in 2017..
ReplyDeleteThanks for your comment! Good to know something still works after four years - rare in this fast-moving world.
DeleteCan you guys please look into my start up script (below)
ReplyDelete#!/bin/sh /etc/rc.common
SCRIPT_NAME="connection_monitor.py"
SCRIPT_PATH="/usr/sbin"
START=99
start() {
sleep 3
. /etc/profile
echo "Starting $SCRIPT_NAME"
python $SCRIPT_PATH/$SCRIPT_NAME &
At start it is working but i don't why it is suddenly stopping. Any help will be highly appreciated.
Thanks in Advance :)