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
/etc/init.d/weather

#!/bin/sh /etc/rc.common
# 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 &
}
START=99 will create an entry in rc.d named S99weather when the script is enabled.
chmod +x /etc/init.d/weather
/etc/init.d/weather enable  #places entry S99weather in /etc/rc.d/
The 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
LD_LIBRARY_PATH
and 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 disable
You 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 

Comments

  1. crontab -e
    and adding @reboot line into the cron file works pretty good on Raspberry Pi, after re-powering:)

    ReplyDelete
  2. Thanks a lot.
    . /etc/profile is very helpful for me.

    ReplyDelete
  3. what is the "&" for after the python /opt/scripts/wunderground_parse.py &

    ReplyDelete
  4. Run the job in the background. Get more info by doing a Google search:
    "linux run script with &" or "linux run script with ampersand"

    ReplyDelete
  5. Thank you very much grandpa! I used whole afternoon and not get it work. Now finally it works after reading your article.

    ReplyDelete
  6. Thank you grandpa! This has disturbed me for a whole afternoon and it works now finally!

    ReplyDelete
  7. You 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....

    ReplyDelete
  8. Great article!! It still works in 2017..

    ReplyDelete
    Replies
    1. Thanks for your comment! Good to know something still works after four years - rare in this fast-moving world.

      Delete
  9. Can you guys please look into my start up script (below)
    #!/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 :)

    ReplyDelete

Post a Comment

Popular Posts