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.
Related
Well as in title. I have Java network application which is running on raspberry pi 3. When i run application from console like java -jar myApp.java or sudo nohup java -jar myApp.java and then i leave this it works fine for many hours.
Then I tried run this application as a service. After configure wlan auto, and run systmd script and all stuff. Application is starting properly, but after few seconds it's losing network connection. I can add that there is wifi connection. I was wandering whats the difference between application run manually and run as service and i did't find anything specific. Is there something i should know about? As far i did't find solution.
I've tired many combinations of systemd scripts but nothing solves my problem. And I also tried to run this app from another on raspberry, but it works like running from this service.
Now i can add that, when i hit from console systemctl stop myService and then systemctl start myService it also works fine.
systemd script below:
[Unit]
Description = Java Service
After network.target = MyService.service
[Service]
Type = forking
ExecStart = /usr/local/bin/MyService.sh start
ExecStop = /usr/local/bin/MyService.sh stop
ExecReload = /usr/local/bin/MyService.sh reload
[Install]
WantedBy=multi-user.target
Bash script to run the service:
#!/bin/bash
SERVICE_NAME=app
PATH_TO_JAR=/home/user/app.jar
RE='^[0-9]+$'
APP_PID=\`ps -ef | grep -v grep | grep "app.jar" | awk '{print $2}'\`
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if ! [[ $APP_PID =~ $RE ]]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null &
APP_PID=`ps -ef | grep -v grep | grep "app.jar" | awk '{print $2}'`
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [[ $APP_PID =~ $RE ]]; then
echo "$SERVICE_NAME stoping ..." &
kill $APP_PID &
APP_PID="" &
echo "$SERVICE_NAME stopped ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [[ $APP_PID =~ $RE ]]; then
echo "$SERVICE_NAME stopping ...";
kill $APP_PID;
echo "$SERVICE_NAME stopped ...";
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> >> /dev/null &
echo "$SERVICE_NAME started ..."
else
echo "$SE`enter code here`RVICE_NAME is not running ..."
fi
;;
esac
I found solution. I don't know exactly what is the reason, but when i give sleep before run this application it works properly. When I start application after this this time, after which it was losing connetion it work fine.
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
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 .
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.