Thursday, January 1, 2015

Linux - Auto init Scripts -2

Add Startup Script or Service with Linux on Bootup


Here is an example of how to create a startup script or service that will auto load on Linux machine boot, in this example I am running a process called "testjava.jar".
 First, create a file "testjava" (or name you desire) in the directory

 /etc/init.d >> touch /etc/init.d/testjava

Now, edit the file:

nano /etc/init.d/testjava

and add your script like:

#! /bin/sh

case "$1" in
  start)
    cd /home/test
    /usr/bin/java -jar testjava.jar &
   ;;
   stop)
     killall -v java
   ;;
esac
exit 0


You can find out where the location of java command is by using the "which" command like:

>> which java

Next, make the file executable:

>> chmod +x /etc/init.d/testjava

Now you should be able to run commands like:

>> /etc/init.d/testjava start
>> /etc/init.d/testjava stop


Now add the script or service to the auto load at bootup, this is Ubuntu example:

 >> update-rc.d testjava defaults

Or on other Linux use:

>> chkconfig --add testjava

If you wanted to remove the registered startup use (Ubuntu example):

>> rm -fv /etc/rc*/*testjava

Or on other Linux use:

>> chkconfig --del testjavaAnd show configured for startup:

>> chkconfig -- list



Thanks
Adarsh Kumar
AK Technosoft Corp.
www.aktechnosoft.com