0

I have created a subsystem file which runs java program I have created. The subsystem file resides in /etc/init.d and allows me to stop and start the service with a consistent interface (start and stop). It is based off of this tutorial: http://www.tldp.org/HOWTO/HighQuality-Apps-HOWTO/boot.html

Here is what the script looks like:

#!/bin/sh
#
# /etc/init.d/mysystem
# Subsystem file for "MySystem" server
#
# chkconfig: 2345 95 05 
# description: MySystem server daemon
#
# processname: MySystem
# config: /etc/MySystem/mySystem.conf
# config: /etc/sysconfig/mySystem
# pidfile: /var/run/MySystem.pid

# source function library
. /etc/rc.d/init.d/functions

# pull in sysconfig settings
[ -f /etc/sysconfig/mySystem ] && . /etc/sysconfig/mySystem 

RETVAL=0
prog="MySystem"
.
.   
.

start() {   
    echo -n $"Starting $prog:"
    java -jar /bin/app.jar
    RETVAL=$?
    [ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog
    echo
}

stop() {    
    echo -n $"Stopping $prog:"
    .
    .   
    .
    killproc $prog -TERM
    RETVAL=$?
    [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog
    echo
}

reload() {  
    echo -n $"Reloading $prog:"
    killproc $prog -HUP
    RETVAL=$?
    echo
}

case "$1" in    
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    reload)
        reload
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$prog ] ; then
            stop
            # avoid race
            sleep 3
            start
        fi
        ;;
    status)
        status $prog
        RETVAL=$?
        ;;
    *)  
        echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
        RETVAL=1
esac
exit $RETVAL

The problem is that this service stops running after I close my terminal session with the server. How do I keep the service running forever in the background?

I do not want to use a screen or nohup because these time out after some time.

Thanks!

petey
  • 562
  • 2
  • 8
  • 20

2 Answers2

1

You need to background the process in your init script. In the line that executes the program, use an ampersand & after the process name.

Can you show us the script?

ewwhite
  • 194,921
  • 91
  • 434
  • 799
  • I have aded the script. The part that calls my jar is in the start(). Are you saying in this script just add a & to my the process like this: java -jar /bin/app.jar &. Or do you mean call the service that runs this command with & like: service app start & ? – petey Dec 07 '12 at 00:25
  • The first approach. Try it. – ewwhite Dec 07 '12 at 00:27
  • @JoeEstes Did it work? – ewwhite Dec 07 '12 at 17:25
  • yes it did. but it seems from what @toppledwagon pointed out, this is only temporary. I am exploring setting this up as a daemon. – petey Dec 07 '12 at 21:18
1

You need to write your java app as a daemon. Even backgrounded processes can die when you close your terminal.

toppledwagon
  • 4,215
  • 24
  • 15