0

I wrote init.d script that suppose to run java CLI proccess.
The problem is that when i stop it, i get [failed] and the proccess is still running.
thanks

#!/usr/bin/env bash
#
# chkconfig:    345 97 03
#
# processname:  quotes-srv
#
#
# source function library

. /etc/rc.d/init.d/functions

NAME=quotes-srv

start() {
    echo -n $"Starting $NAME: "
    daemon +19 java -Dlog4j.configuration="file:/opt/quotes/properties/log4j/log4j.properties"  -Dproperties_folder="/opt/quotes/properties/app/" -jar /opt/quotes/trade-0.0.1-SNAPSHOT-jar-with-dependencies.jar & 
touch /var/lock/subsys/$NAME
}

stop() {
    echo -n $"Stopping $NAME: "
    killproc $NAME
    echo
    rm -f /var/lock/subsys/$NAME
}

restart() {
    stop
    start
}

case "$1" in
  start)
    start
    ;;
  stop) 
    stop
    ;;
  restart|force-reload|reload)
    restart
    ;;
  condrestart|try-restart)
    [ -f /var/lock/subsys/$NAME ] && restart
    ;;
  status)
    status $NAME

    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
    exit 1
esac

exit 0
2
  • 3
    Without knowing killproc, I'd guess it's looking for a process called quotes-srv, but the running process is called java. Aug 19, 2010 at 21:41
  • Agree with Giles comment above (killproc will look for a process with arg[0]=quotes-srv but the process started as argv[0]=java. But IIRC a running proces can change its name.
    – symcbean
    Aug 20, 2010 at 9:01

3 Answers 3

0

The 'daemon' function sadly does not return the actual $! of the process it launched for you to record it to a pid file. What you should do is wrap your Java launch in a shell script, and daemon that script instead.

#!/bin/sh

java -Dlog4j.configuration="file:/opt/quotes/properties/log4j/log4j.properties" \
 -Dproperties_folder="/opt/quotes/properties/app/" \
 -jar /opt/quotes/trade-0.0.1-SNAPSHOT-jar-with-dependencies.jar & 

echo $! /var/run/javaprogram.pid

Then launch your daemon process with the --pidfile option to prevent multiple startups:

daemon --pidfile=/var/run/javaprogram.pid /path/to/script/javaprogram.sh

...and to your stop() add a rm -f /var/run/javaprogram.pid to clean up after yourself.

1
  • sorry meant $! not $?, fixed.
    – user15590
    Aug 20, 2010 at 1:09
1

You might want to save the pid on start in a .pid file in /var/run somewhere.. then use killproc -p pid name

That should kill it

2
  • Thanks but, the problem is, if I want to get the pid, i get empty string in RETVAl variable. daemon java /path/to/blabla RETVAL=$?
    – fatNjazzy
    Aug 19, 2010 at 21:52
  • Specify --pidfile in the daemon line e.g. daemon --pidfile=/var/run/${NAME}.pid +19 java ...
    – symcbean
    Aug 20, 2010 at 9:02
0

propably Gilles is right. Your problem is that the application is being run under java. and as it usually happens with VMs (such as java , python etc) the process is "java something" instead of just "something".

For debugging purposes, check with pidof to see what is returns. (check pidof java to see the pids of your subprocesses)

what you could do is either use ps aux | awk /program/'{print $2}' and kill these pids, or find an init file that starts a java application and see how it is done :>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .