Webapp in Tomcat 6 cannot find environment variables after start - java

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!

Related

'Error: JAVA_HOME is not defined correctly.' on mvn command

when I run the command
mvn version
in ubuntu I get the following error.
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/lib/jvm/java-7-openjdk-amd64/bin/java
My java version is
java version "1.8.0_91" Java(TM) SE Runtime Environment (build
1.8.0_91-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
And I run
sudo nano /etc/environment
command and set java home and maven as
JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre"
M2_HOME=/usr/local/apache-maven/apache-maven-3.2.5
MAVEN_HOME=/usr/local/apache-maven/apache-maven-3.2.5
M2=/usr/local/apache-maven/apache-maven-3.2.5/bin
And my path variable is -
/usr/share/maven3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/jre:/home/hduser/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/local/hadoop/bin:/usr/local/hadoop/sbin:/usr/local/hadoop/bin:/usr/lib/sqoop/bin:/home/hduser/bin:/usr/lib/jvm/java-8-oracle/jre/bin
And run command
sudo nano ~/.bashrc
and set these at the end
JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin export JAVA_HOME export JRE_HOME
export PATH export M2_HOME=/usr/share/maven3 export M2=$M2_HOME/bin
export PATH=$M2:$PATH
I have install java 8 and remove java 7 and then run the mvn command and I am getting this error. I am curious to know why it is showing java 7 rather then java 8 and Please also suggest me some solution.
Thanks in advance
EDIT
My bashrc is -
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u#\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*al$
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
#=======================================
# Added by TKS to conexport PATH=$JAVA_HOME/bin:$PATH
figure HDFS
#=======================================
#HADOOP VARIABLES START
export HADOOP_INSTALL=/usr/local/hadoop
export PATH=$PATH:$HADOOP_INSTALL/bin
export PATH=$PATH:$HADOOP_INSTALL/sbin
export HADOOP_MAPRED_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_HOME=$HADOOP_INSTALL
export HADOOP_HDFS_HOME=$HADOOP_INSTALL
export YARN_HOME=$HADOOP_INSTALL
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_INSTALL/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_INSTALL/lib"
export HADOOP_HOME=/usr/local/hadoop
#export SQOOP_HOME=/usr/local/sqoop
export PATH=$PATH:$HADOOP_HOME/bin
#export PATH=$PATH:$SQOOP_HOME/bin
export SQOOP_HOME=/usr/lib/sqoop
#HADOOP VARIABLES END
JAVA_HOME=/usr/lib/jvm/java-8-oracle
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
export PATH
export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.5
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
I think your JAVA_HOME is supposed to be JDK and not JRE since Maven compile the Java code and there's no Java compiler (javac) in the JRE.
The following should be enough :
JAVA_HOME=/usr/lib/jvm/java-8-oracle
M2_HOME=/usr/local/apache-maven/apache-maven-3.2.5
PATH=$PATH:$JAVA_HOME/bin
PATH=$PATH:$M2_HOME/bin
Remove other M2 or MAVEN_HOME variables that you have declared.

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

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

Start Java app on boot in Debian 7

I have gone through almost every SO and Google article to try to make this happen but still the Java app isnt starting at boot!
I get a message in syslog to say its 'Starting FXC-API' but the Java doesnt actually run.
This is my startup script.
#! /bin/sh
### BEGIN INIT INFO
# Provides: fxc-api
# Required-Start: $syslog exim4
# Required-Stop: $syslog
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and Stop
# Description:
### END INIT INFO
case "$1" in
start)
logger Starting FXC-API
sh /opt/fix/fttglobal/1.0/start.sh
;;
stop)
logger Stopping FXC-API
sh /opt/fix/fttglobal/1.0/stop.sh
;;
*)
echo "Usage: /etc/init.d/fxc-api {start|stop}"
exit 1
;;
esac
exit 0
If I run this manually, ie service fxc-api start, then it starts no problem.
The start.sh script looks like this
#! /bin/sh
cd "$(dirname "$0")"
java -classpath ".:./libs/*" MainController &
Worked it out, I was missing the following in my /etc/init.d script above case
# Java path
JAVA_HOME=/usr/lib/java/jre
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
A note: Run the java like this
java -classpath ".:./libs/*" MainController > ./logs/log.txt 2> ./logs/errors.txt < /dev/null &

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