How to set up something to run at boot on Linux

1. Set up a script in /etc/init.d/

For example, you want to run tightVNC server on a Raspberry Pi

first use a text editor to write a script

sudo nano /etc/init.d/tightvncserver

Then add the following lines to the file /etc/init.d/tightvncserver

#!/bin/sh
#/etc/init.d/tightnvcserver
VNCUSER='pi'
case "$1" in
 start)
   echo "Starting tightVNC Server for $VNCUSER"
   sudo $VNCUSER -c '/usr/bin/tightvncserver :1'
   ;;

 stop)
   echo "Stopping VNC Server for $VNCUSER"
   pkill Xtightvnc
   ;;

 *)
   echo "Usage: /etc/init.d/tightvncserver {start|stop}"
   exit 1
   ;;

esac
exit 0

Then, you should make this script executable...

sudo chmod 775 /etc/init.d/tightvncserver
update-rc.d tightvncserver defaults
sudo reboot

2. Add the commands to /etc/rc.local

For instance, you want to run a java program (myjavacode) or python at boot

java -classpath /home/username/pi_locator:/home/username/pi_locator/lib/sample.jar mypackage.myjavacode
python /home/username/my_python_code.py
Advertisements