Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write init script

Tags:

java

linux

fedora

Hi I am using 64bit Fedora 10 linux. I have created a sample java application. Now I want to write init script for that application. so that my application should start on bootup.

How to write init script to start on bootup.

Thanks Sunil Kumar Sahoo

like image 787
Sunil Kumar Sahoo Avatar asked Nov 16 '09 06:11

Sunil Kumar Sahoo


2 Answers

There's quite a good guide here:

http://www.novell.com/coolsolutions/feature/15380.html

I'd suggest taking a look at the tomcat startup.sh and shutdown.sh scripts, and then modifying the following init.d script:

#!/bin/bash
#
# tomcat
#
# chkconfig:
# description:  Start up the Tomcat servlet engine.

# Source function library.
. /etc/init.d/functions


RETVAL=$?
CATALINA_HOME="/usr/apps/apache/tomcat/jakarta-tomcat-4.0.4"

case "$1" in
 start)
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
      echo $"Starting Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/startup.sh
        fi
  ;;
 stop)
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
      echo $"Stopping Tomcat"
            /bin/su tomcat $CATALINA_HOME/bin/shutdown.sh
        fi
  ;;
 *)
  echo $"Usage: $0 {start|stop}"
  exit 1
  ;;
esac

The above script is missing much of the stuff to make it fully Linux Standard Base compliant. You may want to copy an existing init.d script from your distro. A slightly better script can be found here: http://blog.valotas.com/2011/05/tomcat-initd-script.html

like image 131
brianegge Avatar answered Oct 04 '22 13:10

brianegge


I usually just take one of the smaller init scripts from /etc/init.d and modify it.

Edit

The easiest thing to do is just add your program to the /etc/rc.local file. It will be the last start script executed. You won't have to mess around with the "start" and "stop" stuff.

However, if you're interested in being able to start and stop your program at will, you'll need to write a script.

Some of the other answers here will get you started.

like image 24
Barry Brown Avatar answered Oct 04 '22 12:10

Barry Brown