Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

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

share|improve this answer
    
How should the script be rewritten? –  ewwhite Dec 7 '12 at 15:21
    
yes please let us know –  petey Dec 7 '12 at 17:22
    
This might help with your Java program: commons.apache.org/daemon –  toppledwagon Dec 7 '12 at 21:34

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?

share|improve this answer
    
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 7 '12 at 0:25
    
The first approach. Try it. –  ewwhite Dec 7 '12 at 0:27
    
@JoeEstes Did it work? –  ewwhite Dec 7 '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 7 '12 at 21:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.