Saturday, November 21, 2015

Run script at boot as service

This is achieved using the init run-level based rc scripts. Basically the command "service" used to start and stop system services. for example "service <your script> start".

  # update boot / reboot files   
sudo cp &lt;your script&gt; /etc/init.d/ # &lt;your script&gt; your script file with the path
# do it as soon as the device is going down,
# both for shutdown and reboot
sudo chmod u+x &lt;your script&gt;
sudo update-rc.d /etc/init.d/&lt;your script&gt; defaults
The <your script> is : 

   #! /bin/sh   
### BEGIN INIT INFO
# Provides: &lt;your script name&gt;
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage my cool stuff
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
. /lib/init/vars.sh
. /lib/lsb/init-functions
# If you need to source some other scripts, do it here
case "$1" in
start)
log_begin_msg "Starting my super cool service"
# do something
log_end_msg $?
exit 0
;;
stop)
log_begin_msg "Stopping the coolest service ever unfortunately"
# do something to kill the service or cleanup or nothing
log_end_msg $?
exit 0
;;
*)
echo "Usage: /etc/init.d/&lt;your script&gt; {start|stop}"
exit 1
;;
esac

Then replace # do something with the content of your original script. for example : 
 
 sudo service hostapd stop  
sudo service udhcpd stop
sudo service hostapd start
sudo service udhcpd start

No comments:

Post a Comment