Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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

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


start() {
    echo -n $"Starting $NAME: "
    daemon +19 java -Dlog4j.configuration="file:/opt/quotes/properties/log4j/"  -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
    rm -f /var/lock/subsys/$NAME

restart() {

case "$1" in
    [ -f /var/lock/subsys/$NAME ] && restart
    status $NAME

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

exit 0
share|improve this question
Without knowing killproc, I'd guess it's looking for a process called quotes-srv, but the running process is called java. – Gilles Aug 19 '10 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 '10 at 9:01
up vote 0 down vote accepted

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.


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

echo $! /var/run/

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

daemon --pidfile=/var/run/ /path/to/script/

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

share|improve this answer
sorry meant $! not $?, fixed. – troyengel Aug 20 '10 at 1:09

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

share|improve this answer
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 '10 at 21:52
Specify --pidfile in the daemon line e.g. daemon --pidfile=/var/run/${NAME}.pid +19 java ... – symcbean Aug 20 '10 at 9:02

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 :>

share|improve this answer

Your Answer


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.