Linux – How to set a program to start when the linux machine is turned on?

linuxraspberry pi

Possible Duplicate:
Programs on startup in Linux

I have a Linux machine running a program constantly, if power is cut out, the program wont restart until i open an SSH session and type in "./GPIOServer.sh"
This is just in case i lose power to the machine, and i want the program to automatically start on start up.

Thanks!

Best Answer

What version of OS are you using?

Depending on what it is you have essentially 2 options:

/etc/rc.local

You can either add the executable to the rc.local file which is located here on Redhat distros: /etc/rc.local. You can simply add ./GPIOServer.sh to the end of this file.

/etc/init.d/

If you want it to be a service then you'll need to add a script similar to the ones in /etc/init.d/. Again on Redhat based distros there is a tool to help manage these scripts called chkconfig. See the man-page for more info and also look at the other scripts in /etc/init.d for ideas on how to approach this.

Here's a sample to get you started:

#! /bin/sh
# Basic support for IRIX style chkconfig
###
# chkconfig: 235 98 55
# description: Manages the services you are controlling with the chkconfig command
###

case "$1" in
  start)
        echo -n "Starting new-service"
        #To run it as root:
        /path/to/command/to/start/new-service
        #Or to run it as some other user:
        /bin/su - username -c /path/to/command/to/start/new-service
        echo "."
        ;;
  stop)
        echo -n "Stopping new-service"
        #To run it as root:
        /path/to/command/to/stop/new-service
        #Or to run it as some other user:
        /bin/su - username -c /path/to/command/to/stop/new-service
        echo "."
        ;;

  *)
        echo "Usage: /sbin/service new-service {start|stop}"
        exit 1
esac

exit 0