start-stop-daemon does not start java process - java

I am trying to execute this script with the "start" option:
#!/bin/sh
DESC="Jenkins CI Server"
NAME=jenkins
PIDFILE=$NAME.pid
RUN_AS=alex
COMMAND="/usr/bin/java"
COMMAND_ARGS="-jar jenkins.war"
d_start() {
start-stop-daemon --start --verbose --background --make-pidfile --pidfile "$PIDFILE" --chuid "$RUN_AS" --exec "$COMMAND" -- $COMMAND_ARGS
}
d_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case $1 in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
exit 0
It creates the pid file in the current directory, however I cannot find the process using:
ps -ef | grep java
The process does not exist. The stop command also complains regarding the missing process.
I am just trying to follow instructions for Starting and Accessing Jenkins .

Related

How to create Linux service

I am trying to run my jar as a service because my server should not shouting down when the system is getting shutdown. So I plan to start the server as a service.
My script is,
#!/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 server
### END INIT INFO
JARPATH="/usr/local/myservice/lib"
PID=$JARPATH/pid
start() {
echo "Starting myservice ..."
if [ ! -f $PID ]; then
nohup java -jar $JARPATH/myservice-1.0.0.jar $JARPATH 2>> /dev/null >> /dev/null &
echo $! > $PID
echo "myservice started ..."
else
echo "myservice is already running ..."
fi
}
stop() {
if [ -f $PID ]; then
#PID=$(cat /usr/local/myservice/pid);
echo "Stopping myservice ..."
kill $(cat "$PID");
echo "myservice stopped ..."
rm $PID
else
echo "myservice is not running ..."
fi
}
case $1 in
start)
echo $JARPATH
echo $PID
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
esac
And I did the following steps,
sudo chmod +x /etc/init.d/myservice
sudo update-rc.d myservice defaults
And then start the service using the following command, But the above script is not working...
sudo service myservice start
Here what did I wrong??
Kindly provide your thoughts.
The above script works fine when I put a double quotes in $1,
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
esac

Selenium as a service on Ubuntu 14.04

I'm trying to run selenium(headless) as a service on my Ubuntu 14.04 LTS following bash script:
#!/bin/bash
case "${1:-''}" in
'start')
if test -f /tmp/selenium.pid
then
echo "Selenium is already running."
else
export DISPLAY=localhost:99.0
java -jar /opt/selenium-server-standalone-3.3.1.jar -port 4444 > /var/log/selenium/output.log 2> /var/log/selenium/error.log & echo $! > /tmp/selenium.pid
echo "Starting Selenium..."
error=$?
if test $error -gt 0
then
echo "${bon}Error $error! Couldn't start Selenium!${boff}"
fi
fi
;;
'stop')
if test -f /tmp/selenium.pid
then
echo "Stopping Selenium..."
PID=`cat /tmp/selenium.pid`
kill -3 $PID
if kill -9 $PID ;
then
sleep 2
test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
else
echo "Selenium could not be stopped..."
fi
else
echo "Selenium is not running."
fi
;;
'restart')
if test -f /tmp/selenium.pid
then
kill -HUP `cat /tmp/selenium.pid`
test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
sleep 1
export DISPLAY=localhost:99.0
java -jar /opt/selenium-server-standalone-3.3.1.jar -port 4444 > /var/log/selenium/output.log 2> /var/log/selenium/error.log & echo $! > /tmp/selenium.pid
echo "Reload Selenium..."
else
echo "Selenium isn't running..."
fi
;;
*) # no parameter specified
echo "Usage: $SELF start|stop|restart"
exit 1
;;
esac
My virtual screen starts as on reboot through the following crontab entry
#reboot sh -c 'Xvfb :99 -ac -screen 0 1024x768x8 > /tmp/xvfb.log 2>&1 &'
I get an error above because it seems like selenium is not running headless.
If I use the code below to start selenium manually, it works as expected.
DISPLAY=:1 xvfb-run java -jar /opt/selenium-server-standalone-3.3.1.jar
How would I run the above command on start-up to set-up the selenium listender?

Logging java jar stdout & stderr to a file in linux

I found a script that I can use for my jar file.
My question is how can I modify it to log stdout and stderr to file.
As I understand that this line should be modified:
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
What does "/tmp" indicate? /dev/null means it is not directing anuthing to anywhere?
Here is the script I'm using:
#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
I would like to log everything to /var/log/myservice.log file.
Any help is appreciated!
I found this solution:
nohup java -jar $PATH_TO_JAR > /var/log/myservice.log 2>&1 &
When I deleted the log file while the servce was running it didn't create a new one altough it was streaming to stdout.

How to run java service as a non-root user on CentOs 6

I have a script that starts java application as a service on CentOs 6.
Here it is:
#!/bin/sh
# chkconfig: - 80 20
SERVICE_NAME=cn4server
PATH_TO_JAR=/usr/local/share/myserver/cn4server.jar
PID_PATH_NAME=/usr/local/share/myserver/cn4server-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
It works well, but there is a problem: it runs as a root.
How can I change the script to run java application on behalf of another user?
The solution for me was:
crontab for a non-root user with the line
#reboot /usr/local/share/myserver/cn4server.sh restart
It workes fine, although it's a workaround.

JSVC to start as daemon - Enable JMX?

i'm using JSVC to start my java program as a daemon which should run 24/7.
However, i used to enable JMX to monitor my application until i decided to convert it to a daemon.
My init.d Script is like this
#!/bin/sh
# Setup variables
EXEC=/usr/bin/jsvc
JAVA_HOME=/usr/lib/jvm/java-6-openjdk
CLASS_PATH="/usr/share/java/commons-daemon.jar":"/fullpath/Myserver.jar"
CLASS=myserver.Main
USER=myserver
PID=/var/run/myserver.pid
LOG_OUT=/var/log/myserver/client.out
LOG_ERR=/var/log/myserver/client.err
do_exec()
{
$EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS -Xmx30M -Djava.rmi.server.hostname=123.234.12.34 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=/fullpath/jmxremote.password -Dcom.sun.management.jmxremote.access.file=/fullpath/jmxremote.access -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1234
}
case "$1" in
start)
do_exec
;;
stop)
do_exec "-stop"
;;
restart)
if [ -f "$PID" ]; then
do_exec "-stop"
do_exec
else
echo "service not running, will do nothing"
exit 1
fi
;;
*)
echo "usage: daemon {start|stop|restart}" >&2
exit 3
;;
esac
The -D options don't seem to take effect, because port 1234 (or any other port i specify) is not used by JMX.
The application runs just fine, but not JMX. I don't know where to start searching, do you have an idea?
I need to replace:
$EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS -Xmx30M -Djava.rmi.server.hostname=123.234.12.34 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=/fullpath/jmxremote.password -Dcom.sun.management.jmxremote.access.file=/fullpath/jmxremote.access -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1234
with:
$EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID -Xmx30M -Djava.rmi.server.hostname=123.234.12.34 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=/fullpath/jmxremote.password -Dcom.sun.management.jmxremote.access.file=/fullpath/jmxremote.access -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1234 $1 $CLASS
so, don't put the additional arguments after $CLASS

Categories

Resources