Unix script on startup in /etc/init.d not working - java

I've been trying to get my Java application to run as a daemon in the background after startup. I've followed the instructions given in the top answer here and to no avail.
This is my /etc/init.d/myapp file:
#!/bin/bash
# MyApp
#
# description: bla bla
case $1 in
start)
/bin/bash /var/lib/myapp/start.sh
;;
stop)
/bin/bash /var/lib/myapp/stop.sh
;;
restart)
/bin/bash /var/lib/myapp/stop.sh
/bin/bash /var/lib/myapp/start.sh
;;
esac
exit 0
as for the /var/lib/myapp/start.sh, it looks like this:
#!/bin/bash
java -jar myapp-1.0.0RC.jar &
and works fine when run from a terminal via ssh.
i also ran the update-rc.d myscript defaults command, and was only given a warning about headers and LSB
After this, once i reboot the server, the app isnt running. Any help is appreciated.
Thanks.

When bash scripts are run, they are not automatically ran from the same directory that contains them.
You will either need to update your scripts to change directory to that which holds the scripts before starting the jar:
#!/bin/bash
cd /var/lib/myapp/
java -jar myapp-1.0.0RC.jar &
Or, refer to the jar file with a full path:
#!/bin/bash
java -jar /var/lib/myapp/myapp-1.0.0RC.jar &

Check if your service is registered properly via chkconfig
$ chkconfig --list
If not you can see your service listed on the output, then try adding this lines to your script
#!/bin/bash
# chkconfig: 2345 95 20
# description: bla bla
# processname: myapp
and then run
chkconfig --add myapp
For more information you can check the man page for chkconfig

Related

Docker image openjdk:8-jdk-alpine fails to execute a simple command

I created a docker image from openjdk:8-jdk-alpine using the below Dockerfile:
But when I try to execute simple commands I get the following errors:
/bin/sh: ./run.sh: not found
my run.sh looks like this
enter image description here
I try to "docker run -it [images] bash" enter to the interactive environment,I can see the file "run.sh".In the directory /bin bash exist,but I execute run.sh also display " /bin/sh: ./run.sh: not found"
PS:Sorry for my poor english,I am a chinese student
The printed content of the run.sh, indicates that my original assessment was incorrect; however based on the error message, and the image of the run.sh file, I have a lead.
Your run.sh script has an exec line of #!/bin/sh, which means that it does not need bash to operate so my previous assessment was incorrect.
Starting on a mac, I created a run.sh script, duplicated the dockerfile (mostly), and it ran correctly, producing a valid run.
I then converted the run.sh to use dos line endings and got the following:
$ file run.sh
run.sh: POSIX shell script text executable, ASCII text, with CRLF line terminators
$ docker run --rm -it bob
/bin/sh: ./run.sh: not found
Which looks suspiciously like your error message.
From this, it would lead me to believe that your run.sh file contains dos line endings. Based on the images, I'm guessing that you're on windows, which is where the problem with the run.sh script originates.
how to convert the line endings (some examples):
dos2unix run.sh
perl -pi -e 's/\r\n/\n/g' run.sh
Previous Answer
The most likely reason for this issue is that the shebang line in the run.sh contains: #!/usr/bin/bash, or something of that ilk - i.e. it doesn't reference the valid path to the binary that will run the shell script.
On alpine, bash is installed into /bin, so if you try to run the script you will see the error:
/ # ./run.sh
/bin/sh: ./run.sh: not found
/ # cat run.sh
#!/usr/bin/bash
echo "Hi"
workaround (1): after the apk add bash, do an:
RUN ln -s /bin/bash /usr/bin
in your Dockerfile. This will create a symlink for bash, allowing the program to run:
/ # ln -s /bin/bash /usr/bin
/ # ./run.sh
Hi
workaround(2) - if you don't want to make a symlink like this, you can always invoke bash as part of the CMD -
CMD [ 'bash', './run.sh' ]

Webapp in Tomcat 6 cannot find environment variables after start

I have the following problem:
I am using Tomcat 6.0.32 and Java JDK 6.0_26. I have installed it successfully and the Tomcat start page is visible in the browser at port 8080.
I have also created $CATALINA_HOME/setenv.sh script and put some webapp-specific environment variables in it (along with the CATALINA_HOME, JAVA_HOME and CLASSPATH).
I have created a new user "tomcat", set a new home directory for him, and also passwd-ed it.
This script is being sourced from within a init script I created to start and stop Tomcat automatically on reboot. I do not use the standart startup.sh and shutdown.sh found in $CATALINA_HOME, but rather then jsvc daemon starter, so I can use port 8080 from a non-root process (Tomcat itself).
The actual problem is that, after restarting Tomcat my webapp does not receive or see the environment variable I set in setenv.sh and so it won't start.
I have tried to put the environment variable definition in various places:
.bashrc in the tomcat home directory
/etc/init.d/tomcat script
$CATALINA_HOME/bin/setenv.sh
$CATALINA_HOME/webapps/myapp/META-INF/context.xml
to no avail, after start of Tomcat my webapp does not see the required environment variables.
My question is - what the heck am I doing worng? Any suggsetions? How am I supposed to transfer env vars to an webapp if the setenv.sh does not work? What could make this mechanism faulty (allegedly this is the way to hand env vars to webapps)?
Here is the startup script I worte:
#!/bin/sh
### BEGIN INIT INFO
# Provides: allfaweb
# Required-Start: $syslog $apache $apache2 $httpd
# Should-Start:
# Required-Stop: $syslog $apache $apache2 $httpd
# Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: ALLFAweb service
### END INIT INFO
ALLFAWEB_BIN=/install/apache-tomcat-6.0.32-allfaweb/bin/jsvc
test -x $ALLFAWEB_BIN || { echo "$ALLFAWEB_BIN not installed";
if [ "$1" = "stop" ]; then exit 0;
else exit 5; fi; }
# Check for existence of setenv.sh file and read it
ALLFAWEB_CONFIG=/install/apache-tomcat-6.0.32-allfaweb/bin/setenv.sh
test -r $ALLFAWEB_CONFIG || { echo "$ALLFAWEB_CONFIG not existing";
if [ "$1" = "stop" ]; then exit 0;
else exit 6; fi; }
. /etc/rc.status
rc_reset
. /install/apache-tomcat-6.0.32-allfaweb/bin/setenv.sh;
case "$1" in
start)
echo -n "Starting ALLFAweb ";
$ALLFAWEB_BIN \
-user tomcat \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-pidfile $ALLFAWEB_PID \
-outfile $CATALINA_HOME/logs/catalina.out \
-errfile $CATALINA_HOME/logs/catalina.err \
-cp $CLASSPATH org.apache.catalina.startup.Bootstrap
rc_status -v
;;
stop)
echo -n "Shutting down ALLFAweb "
$ALLFAWEB_BIN \
-stop \
-pidfile $ALLFAWEB_PID \
org.apache.catalina.startup.Bootstrap
rc_status -v
;;
restart)
$0 stop
$0 start
rc_status
;;
status)
echo -n "Checking for service ALLFAweb ";
/sbin/checkproc $ALLFAWEB_BIN
rc_status -v
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
rc_exit
The system I am using is a SUSE SP2:
# uname -a
Linux testmachine 3.0.51-0.7.9-default #1 SMP Thu Nov 29 22:12:17 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Any help would be highly appreciated! Thanks in advance :)
You should be using system properties and not environment variables. Check the source for Tomcat's bin/daemon.sh script, which passes all of the standard variables to Tomcat when it is launched.
I found it. Allegedly the reason is that at first I started Tomcat 6 as root, and then changed the ownership of files to user "tomcat". The root process had written files all over the file system wiht the appropriate permissions, that is, the "tomcat" user could not read them.
The default shell of the "tomct" user was /bin/sh and not /bin/bash
I also deleted the files in $CATALINA_HOME/work and renamed the directory to which my custom env var was pointing to (and the env var accordingly).
There were also some logfiles generated from log4j in the / dicrectory, those are gone too and now everything works as expected :)
Thanks for all the feedback, you kept me going during these 3 days of frustration :)
Cheers!

Specifying Play 2.0 port with "dist"

I'm creating a packaged project usingdist and am trying to modify the generated start script to run the app on port 9001.
Here is what is generated:
exec java $* -cp "`dirname $0`/lib/*" play.core.server.NettyServer `dirname $0`
Here is what I tried, which doesn't seem to work.
exec java $* -Dhttp.port=9001 -cp "`dirname $0`/lib/*" play.core.server.NettyServer `dirname $0`
Any ideas?
I've also tried specifying http.port=9001 in application.conf with no avail. It was very easy to do this in Play 1.2.X, seems a step backward.
After running play dist and then extracting the generated bundle, you can start Play 2 on a different port by running:
./start -Dhttp.port=5432
Or if you would rather edit the start script you can update it to be:
#!/usr/bin/env sh
exec java $* -Dhttp.port=5432 -cp "`dirname $0`/lib/*" play.core.server.NettyServer `dirname $0`
And then run:
./start

Run a Java Application as a Service on Linux

I have written a Java server application that runs on a standard virtual hosted Linux solution. The application runs all the time listening for socket connections and creating new handlers for them. It is a server side implementation to a client-server application.
The way I start it is by including it in the start up rc.local script of the server. However once started I do not know how to access it to stop it and if I want to install an update, so I have to restart the server in order to restart the application.
On a windows PC, for this type of application I might create a windows service and then I can stop and start it as I want. Is there anything like that on a Linux box so that if I start this application I can stop it and restart it without doing a complete restart of the server.
My application is called WebServer.exe. It is started on server startup by including it in my rc.local as such:
java -jar /var/www/vhosts/myweb.com/phpserv/WebServer.jar &
I am a bit of a noob at Linux so any example would be appreciated with any posts. However I do have SSH, and full FTP access to the box to install any updates as well as access to a Plesk panel.
I wrote another simple wrapper here:
#!/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
You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here
If you need the output log replace the 2
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
lines for
nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&
A simple solution is to create a script start.sh that runs Java through nohup and then stores the PID to a file:
nohup java -jar myapplication.jar > log.txt 2> errors.txt < /dev/null &
PID=$!
echo $PID > pid.txt
Then your stop script stop.sh would read the PID from the file and kill the application:
PID=$(cat pid.txt)
kill $PID
Of course I've left out some details, like checking whether the process exists and removing pid.txt if you're done.
Linux service init script are stored into /etc/init.d. You can copy and customize /etc/init.d/skeleton file, and then call
service [yourservice] start|stop|restart
see http://www.ralfebert.de/blog/java/debian_daemon/. Its for Debian (so, Ubuntu as well) but fit more distribution.
Maybe not the best dev-ops solution, but good for the general use of a server for a lan party or similar.
Use screen to run your server in and then detach before logging out, this will keep the process running, you can then re-attach at any point.
Workflow:
Start a screen: screen
Start your server: java -jar minecraft-server.jar
Detach by pressing: Ctl-a, d
Re-attach: screen -r
More info here: https://www.gnu.org/software/screen/manual/screen.html
Another alternative, which is also quite popular is the Java Service Wrapper. This is also quite popular around the OSS community.
Referring to Spring Boot application as a Service as well, I would go for the systemd version, since it's the easiest, least verbose, and best integrated into modern distros (and even the not-so-modern ones like CentOS 7.x).
The easiest way is to use supervisord. Please see full details here: http://supervisord.org/
More info:
https://askubuntu.com/questions/779830/running-an-executable-jar-file-when-the-system-starts/852485#852485
https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps
Here is a sample shell script (make sure you replace the MATH name with the name of the your application):
#!/bin/bash
### BEGIN INIT INFO
# Provides: MATH
# Required-Start: $java
# Required-Stop: $java
# Short-Description: Start and stop MATH service.
# Description: -
# Date-Creation: -
# Date-Last-Modification: -
# Author: -
### END INIT INFO
# Variables
PGREP=/usr/bin/pgrep
JAVA=/usr/bin/java
ZERO=0
# Start the MATH
start() {
echo "Starting MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "The service is already running"
else
#Run the jar file MATH service
$JAVA -jar /opt/MATH/MATH.jar > /dev/null 2>&1 &
#sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service was successfully started"
else
echo "Failed to start service"
fi
fi
echo
}
# Stop the MATH
stop() {
echo "Stopping MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
#Kill the pid of java with the service name
kill -9 $($PGREP -f MATH)
#Sleep time before the service verification
sleep 10
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Failed to stop service"
else
echo "Service was successfully stopped"
fi
else
echo "The service is already stopped"
fi
echo
}
# Verify the status of MATH
status() {
echo "Checking status of MATH..."
#Verify if the service is running
$PGREP -f MATH > /dev/null
VERIFIER=$?
if [ $ZERO = $VERIFIER ]
then
echo "Service is running"
else
echo "Service is stopped"
fi
echo
}
# Main logic
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart|reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
exit 1
esac
exit 0
From Spring Boot application as a Service, I can recommend the Python-based supervisord application. See that stack overflow question for more information. It's really straightforward to set up.
Other answers do a good job giving custom scripts and setups depending on your platform. In addition to those, here are the mature, special purpose programs that I know of:
JSW from TanukiSoftware
YAJSW is an open source clone from the above. It is written in Java, and it is a nanny process that manages the child process (your code) according to configurations. Works on windows / linux.
JSVC is a native application. Its also a nanny process, but it invokes your child application through the JNI, rather than as a subprocess.
You can use Thrift server or JMX to communicate with your Java service.
From Spring Boot Reference Guide
Installation as an init.d service (System V)
Simply symlink the jar to init.d to support the standard start, stop, restart and status commands.
Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service simply create a symlink:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp
Once installed, you can start and stop the service in the usual way. For example, on a Debian based system:
$ service myapp start
If your application fails to start, check the log file written to /var/log/<appname>.log for errors.
Continue reading to know how to secure a deployed service.
After doing as written I've discovered that my service fails to start with this error message in logs: start-stop-daemon: unrecognized option --no-close. And I've managed to fix it by creating a config file /var/myapp/myapp.conf with the following content
USE_START_STOP_DAEMON=false
It is possible to run the war as a Linux service, and you may want to force in your pom.xml file before packaging, as some distros may not recognize in auto mode. To do it, add the following property inside of spring-boot-maven-plugin plugin.
<embeddedLaunchScriptProperties>
<mode>service</mode>
</embeddedLaunchScriptProperties>
Next, setup your init.d with:
ln -s myapp.war /etc/init.d/myapp
and you will be able to run
service myapp start|stop|restart
There are many other options that you can find in Spring Boot documentation, including Windows service.
Im having Netty java application and I want to run it as a service with systemd. Unfortunately application stops no matter of what Type I'm using. At the end I've wrapped java start in screen. Here are the config files:
service
[Unit]
Description=Netty service
After=network.target
[Service]
User=user
Type=forking
WorkingDirectory=/home/user/app
ExecStart=/home/user/app/start.sh
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
start
#!/bin/sh
/usr/bin/screen -L -dmS netty_app java -cp app.jar classPath
from that point you can use systemctl [start|stop|status] service.
To run Java code as daemon (service) you can write JNI based stub.
http://jnicookbook.owsiak.org/recipe-no-022/
for a sample code that is based on JNI. In this case you daemonize the code that was started as Java and main loop is executed in C. But it is also possible to put main, daemon's, service loop inside Java.
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo029
Have fun with JNI!
However once started I don't know how to access it to stop it
You can write a simple stop script that greps for your java process, extracts the PID and calls kill on it. It's not fancy, but it's straight forward.
Something like that may be of help as a start:
#!/bin/bash
PID = ps ax | grep "name of your app" | cut -d ' ' -f 1
kill $PID

JSVC initscript doesn't exit

I'm trying to deamonize my Java app using jsvc. This is my initscript
#!/bin/sh
# CONFIG
JSVC=/opt/jsvc/jsvc
JAVA_HOME=/usr/lib/jvm/jre-1.6.0-openjdk.x86_64
USER=gserv
ARGS=none
# END CONFIG
PIDFILE=/var/run/silvercar-gameserver.pid
LOGDIR=/var/log/silvercar-gameserver
case "$1" in
start)
export JAVA_HOME
cd `dirname $0`
$JSVC -jvm server -pidfile $PIDFILE -user $USER -outfile $LOGDIR/stdout -errfile $LOGDIR/stderr \
-cp `cat classpath` tr.silvercar.gameserver.runner.DeamonGameServer $ARGS
;;
stop)
$JSVC -stop -pidfile $PIDFILE
;;
esac
exit 0
When I run ./thisscript.sh start as root two things go wrong, and I suspect they're related:
The app starts, but its output is shown instead of saved to the specified outfile
The script doesn't exit, but blocks until I hit Ctrl+C.
What am I doing wrong?
I don't see anything wrong in your launch script; perhaps there is an issue in your service implementation DeamonGameServer. Try replacing your class with a simple Daemon skeleton implementation and see what happens.
Also, note there is an open defect in jsrv : Jsvc does not exit when all non-daemon threads are dead.

Categories