Issue
I created a Java application which is supposed to run as an ubuntu service. I followed this article: rel="nofollow" title="Running a Java program as a daemon in Ubuntu Linux">Running a Java program as a daemon in Ubuntu Linux
This is my script in /etc/init.d/
dir:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/stop myservice
### END INIT INFO
case $1 in
status)
if [ -f /opt/myservice/pid ]; then
PID=$(cat /opt/myservice/pid);
echo "myservice is running. PID="$PID
else
echo "myservice is not running."
fi
;;
start)
if [ ! -f /opt/myservice/pid ]; then
nohup java -jar /opt/myservice/billing_consumer.jar /opt/myservice 2>> /dev/null >> /dev/null &
echo $! > /opt/myservice/pid
echo "myservice started ..."
else
echo "myservice is already running ..."
fi
;;
stop)
if [ -f /opt/myservice/pid ]; then
PID=$(cat /opt/myservice/pid);
kill $PID;
echo "myservice stopped ..."
rm /opt/myservice/pid
else
echo "myservice is not running ..."
fi
;;
restart)
if [ -f /opt/myservice/pid ]; then
PID=$(cat /opt/myservice/pid);
echo "Stopping myservice ...";
kill $PID;
echo "myservice stopped ...";
rm /opt/myservice/pid
echo "Starting myservice ..."
nohup java -jar /opt/myservice/billing_consumer.jar /opt/myservice 2>> /dev/null >> /dev/null &
echo $! > /opt/myservice/pid
echo "myservice started ..."
else
echo "myservice is not running ..."
fi
;;
esac
It seems that this script closes the billing_consumer.jar
standard input and redirects output to /dev/null
; which, I read somewhere that it's the bitbucket
which is used for redirecting console's output to that in this place. Also I don't have anything to be sent to stderr
or stdout
except some third party components which use those outputs. I myself use java's builting logging API (java.util.logging.Logger
) to log information. I use the java.util.logging.FileHandler
as the handler for logging outputs. But the problem is that, when I run the program as a service logging does not work. But when I run the program manually, I can see the logs written in the files. This is my logging.properties
file used for logging configuration:
handlers = java.util.logging.FileHandler
config =
java.util.logging.FileHandler.level = ALL
java.util.logging.FileHandler.filter =
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.encoding =
java.util.logging.FileHandler.limit = 1048576
java.util.logging.FileHandler.count = 20
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.pattern = logs/billing-log.%u.%g.log
...
I don't think this has anything to do with the /etc/init.d/myservice
script, Since I changed that to redirect the output to a file also and I could see that the third party components' outputs are written in the file. Although, I am not writing to terminals' output. Can anyone figure out what is the problem here. Do I have configured logging the wrong way?
Solution
In your logging properties you are using a relative path. When you run this as a service the current/working directory is probably different from when you run this manually. The FileHandler will fail to create a new log file if the directory path doesn't exist. Set the working directory or change the path to an absolute path.
The 'config =' line looks wrong since there is no value.
If that doesn't work you should modify the script to pipe the out/err streams to a file to debug what is going on.
Answered By - jmehrens Answer Checked By - Pedro (WPSolving Volunteer)