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
Related
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?
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 .
So after 2 days (yes I'm a complete rookie when it comes to servers) trying to get this working I give up and turn to SO for help :)
I want to start my java app on start, log to a logfile. That's it :)
start on runlevel [2345]
stop on runlevel [!2345]
#Respawn the process if it crashes
#If it respawns more than 10 times in 5 seconds stop
respawn
respawn limit 10 5
expect fork
script
cd /home/ubuntu/admin/
mvn spring-boot:run > /var/log/upstart/admin.log 2>&1
end script
Running "sudo start admin" works and I get "admin start/running" in console.. No log is created and the java app is not started.. ?
What am I missing?
How do I run Java as a service on Ubuntu?
I don't mean to sidetrack, but I've deployed Java applications on Ubuntu in production since 2010 and had very little success with Upstart. I use init.d scripts and start-stop-daemon. Side bonus: it works on more distros.
Create /etc/init.d/my-java-app:
#!/bin/sh
#
# my-java-app My Java App
#
# chkconfig: - 80 05
# description: Enable My Java Application
#
### BEGIN INIT INFO
# Provides: my-java-app
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: My Java Application
# Short-Description: Enable My Java Application
### END INIT INFO
DESC="my java app"
NAME=my-java-app
PIDFILE=/var/run/$NAME.pid
RUN_AS=ubuntu
WORK_DIR=/home/ubuntu/admin
DAEMON=/usr/bin/mvn
DAEMON_OPTS="spring-boot:run"
# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
do_start() {
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE \
--background \
--chuid $RUN_AS \
--chdir $WORK_DIR \
--exec $DAEMON -- $DAEMON_OPTS
}
do_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
sleep 1
do_start
echo "."
;;
status)
status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
Make it belong to root, make it executable, and set it up to run on startup with:
sudo chown root:root /etc/init.d/my-java-app
sudo chmod 755 /etc/init.d/my-java-app
sudo update-rc.d my-java-app defaults
To start the service you can run:
sudo service my-java-app start
To stop the service you can run:
sudo service my-java-app stop
This is based on a simplified version of the /etc/init.d/skeleton file included by Ubuntu.
The man page for start-stop-daemon is worth looking at if you want to tweak this further.b
I created a Java application which is supposed to run as an ubuntu service. I followed this article: 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?
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.
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.