Java App as a Linux Service issue with permissions - java

I want to start my Java app as a Linux service. I created a wrapper file:
SERVICE_NAME=ConfigurationService PATH_TO_JAR=/opt/Applications/Configuration-Service/configuration-service.jar PID_PATH_NAME=/tmp/ConfigurationService-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
However, when I start it with command:
service ConfigurationService start
The app start but does not have permissions to connect to DataBase. When I invoke my web API I can see such error:
There was an unexpected error (type=Internal Server Error, status=500).
Could not get JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
If I start my application using command it works without issues:
java -jar ConfigurationService.jar &
I understand that it is something related to users and permissions but I am new in Linux configuration. Appreciate any help

Related

How to deploy Java war file using Jenkinsfile in jenkins pipeline

In jenkins maven project we can use BUILD_ID=DontKillMe to prevent hudson script shutdown the shell calls.
Like: BUILD_ID=DontKillMe java -jar target.jar
But add BUILD_ID is not work in Jenkinsfile.
Jenkinsfile:
#!/usr/bin/env groovy
node {
stage('Build') {
checkout scm
sh '/opt/gradle/gradle-4.1/bin/gradle clean build'
}
stage('Deploy') {
sh 'mkdir -p /opt/www/foobar'
sh 'cp build/libs/*.war /opt/www/foobar/newest.war'
sh 'chmod 755 ./deploy.sh'
sh 'nohup ./deploy.sh &'
sh 'while ! httping -qc1 http://localhost:10000 ; do sleep 1 ; done'
}
}
After hudson script executed, all the shells called by hudson script would be shutdown. Even double nohup still not work.
deploy.sh:
#!/bin/bash
nohup java -jar -Dspring.profiles.active=prod /opt/www/foobar/newest.war /var/log/foobar.log 2>&1 &
You can use BUILD_ID=dontKillMe in sh step the same way
sh 'BUILD_ID=dontKillMe nohup ./deploy.sh &'
I found an another way to execute scripts and cannot be killed.
Linux service
/etc/init.d/www-project
#!/bin/bash
. /etc/init.d/functions
SERVICE_NAME="www-project"
RETVAL=0
PID=-1
PIDFILE=/var/run/${SERVICE_NAME}.PID
start () {
if [ -f ${PIDFILE} ]; then
echo "PID file ${PIDFILE} already exists, please stop the service !"
exit
fi
echo "Starting service ${SERVICE_NAME} ..."
java -jar -Dspring.profiles.active=prod /opt/www/project/newest.war > /var/log/www-project.log 2>&1 &
PID = $!
if [ -z ${PID} ]; then
echo "Failed to get the process id, exit!"
exit
else
echo "Starting successfully, whose pid is ${PID}"
fi
touch $PIDFILE
echo ${PID} > ${PIDFILE}
}
stop () {
if [ -f $PIDFILE ]; then
PID = `cat ${PIDFILE}`
if [ -z $PID ]; then
echo "PIDFILE $PIDFILE is empty!"
exit
fi
if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
echo "Process dead but pidfile exists!"
exit
else
kill -9 $PID
echo "Stopping service successfully, whose pid is $PID"
rm -f $PIDFILE
fi
else
echo "File $PIDFILE does NOT exist!"
fi
}
restart () {
stop
start
}
status () {
if [ -f $PIDFILE ]; then
PID=`cat $PIDFILE`
if [ -z $PID ] ; then
echo "No effective pid but pidfile exists!"
else
if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
echo "Process dead but pidfile exist"
else
echo "Running"
fi
fi
else
echo "Service not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: www-project {start|stop|restart|status}"
;;
esac
systemd unit service
/usr/lib/systemd/system/www-project.service
[Unit]
Description=project
After=mysqld.service
Wants=mysqld.service
[Service]
ExecStart=/usr/lib/jvm/java/bin/java -jar -Dspring.profiles.active=prod /opt/www/project/newest.war > /var/log/www-project.log
PIDFile=/var/run/www-project.pid
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/bin/kill -9 $MAINPID
[Install]
WantedBy=multi-user.target
In Jenkinsfile, we can call
service restart www-project
or
systemctl restart www-project
And hudson script will never kill the services.

Tomcat 7.0.26 failing to start jsvc

So I had a tomcat 7.026 smoothly running on a CentOS 6.7 Server on its default port 8080.
Recently it stopped running on its own.
Here are the out put of each:
sudo service easy-tomcat7 start
Failed to start Tomcat
sudo service easy-tomcat7 status
pidof: invalid options on command line!
pidof: invalid options on command line!
jsvc.exec is stopped
ps aux | grep tomcat
root 4293 1.4 2.3 5939868 389996 pts/0 Sl 14:16 0:14
/usr/java/jdk1.7.0_05/bin/java
-Djava.util.logging.config.file=/usr/share/apache-tomcat-7.0.26/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/share/apache-tomcat-7.0.26/endorsed -classpath /usr/share/apache-tomcat-7.0.26/bin/bootstrap.jar:/usr/share/apache-tomcat-7.0.26/bin/tomcat-juli.jar
-Dcatalina.base=/usr/share/apache-tomcat-7.0.26 -Dcatalina.home=/usr/share/apache-tomcat-7.0.26 -Djava.io.tmpdir=/usr/share/apache-tomcat-7.0.26/temp org.apache.catalina.startup.Bootstrap start
root 16821 0.0 0.0 8548 384 ? Ss 14:30 0:00
jsvc.exec -user tomcat -cwd /usr/local/easy/share/easy-tomcat7
-pidfile /var/run/easy-tomcat7.pid -cp /usr/local/easy/bin/bootstrap.jar:/usr/local/easy/bin/tomcat-juli.jar:/usr/local/easy/share/java/commons-daemon.jar
-Djava.endorsed.dirs=/usr/local/easy/share/easy-tomcat7/endorsed -outfile /var/log/easy-tomcat7/catalina.out -errfile /var/log/easy-tomcat7/catalina.err -verbose
org.apache.catalina.startup.Bootstrap start
tomcat 16823 9.4 3.5 6190668 574344 ? Sl 14:30 0:15
jsvc.exec -user tomcat -cwd /usr/local/easy/share/easy-tomcat7
-pidfile /var/run/easy-tomcat7.pid -cp /usr/local/easy/bin/bootstrap.jar:/usr/local/easy/bin/tomcat-juli.jar:/usr/local/easy/share/java/commons-daemon.jar
-Djava.endorsed.dirs=/usr/local/easy/share/easy-tomcat7/endorsed -outfile /var/log/easy-tomcat7/catalina.out -errfile /var/log/easy-tomcat7/catalina.err -verbose
org.apache.catalina.startup.Bootstrap start
Killing those processes aren't helping , as well as service start, restart, force-reload ... etc
Finally here's the script of easy-tomcat7
. /etc/rc.d/init.d/functions
case $1 in
start)
file=/usr/sbin/starttomcat
# no way to start tomcat
if [ ! -x $file ]; then
echo "Missing $file"
ERROR=1
else
# tomcat disabled by whm
if [ -e /etc/tomcatdisable ]; then
echo "Tomcat is disabled by cPanel/WHM"
ERROR=0
else
ERROR=0
status jsvc.exec &>/dev/null
# tomcat already running
if [ $? -eq 0 ]; then
echo "Tomcat already started"
# start tomcat, not running
else
$file &>/dev/null
sleep 1 # just to make sure
status jsvc.exec &>/dev/null
if [ $? -eq 0 ]; then
ERROR=0
else
echo "Failed to start Tomcat"
ERROR=1
fi
fi
fi
fi
;;
stop)
file=/usr/sbin/stoptomcat
if [ ! -x $file ]; then
echo "Missing $file"
ERROR=1
else
$file
ERROR=0
fi
;;
restart)
file=/usr/local/cpanel/scripts/restartsrv_tomcat
if [ ! -x $file ]; then
echo "Missing $file"
ERROR=1
else
$file
fi
;;
status|fullstatus)
status jsvc.exec
;;
*)
echo $"Usage: $0 {start|stop|restart|status|fullstatus}"
ERROR=2 esac
exit $ERROR
I finally resolved the issue.
After running
java -version
It seemed that the Java Installation was somehow corrupted so all I had to do was to uninstall and re-install Java and that made the trick for me.

What is the proper way to stop hiveserver2?

I've installed hive 0.14 on top of hadoop 2.6.0.
The setup mainly involved just extracting the tar.bin file.
I followed this guide to do the setup.
http://www.ishaanguliani.com/content/hive-0140-setup-ubuntu
I start hiveserver2 with a command line:
( $HIVE_HOME/bin/hiveserver2 &> hiveserver.log & )
Now, I am wondering what is the proper to stop hiveserver2. I can kill it but I doubt that provides a graceful exit.
write a small shell script to find hiveserver2 process and stop it. I used below shell script to stop hiveserver2 and hive metastore process. Hope this helps.
hive2_pid=`pgrep -f org.apache.hive.service.server.HiveServer2`
if [[ -n "$hive2_pid" ]]
then
echo "Found hivesevrer2 PID-- "$hive2_pid
kill $hive2_pid
# if process is still around, use kill -9
if ps -p $hive2_pid > /dev/null ; then
echo "Initial kill failed, killing with -9 "
kill -9 $hive2_pid
fi
echo "Hive server2 stopped successfully"
else
echo "Hiveserver2 process not found , HIveserver2 is not running !!!"
fi
meta_pid=`pgrep -f org.apache.hadoop.hive.metastore.HiveMetaStore`
if [[ -n "$meta_pid" ]]
then
echo "Found hivesevrer2 PID-- "$meta_pid
kill $meta_pid
# if process is still around, use kill -9
if ps -p $meta_pid > /dev/null ; then
echo "Initial kill failed, killing with -9 "
kill -9 $meta_pid
fi
echo "Hive metastore stopped successfully"
else
echo "Hive metastore process not found , Hive metastore is not running !!!"
fi
$ sudo service hive-server2 stop
refer this link : http://www.cloudera.com/documentation/archive/cdh/4-x/4-2-0/CDH4-Installation-Guide/cdh4ig_topic_18_8.html

Tomcat start and stop using shell script?

I have below shell script to stop/stop the tomcat.
#!/bin/bash
export BASE=/home/programs/jakarta-tomcat-5.0.28/bin
prog=jakarta-tomcat-5.0.28
stat() {
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat is running.
else
echo Tomcat is not running.
fi
}
case "$1" in
start)
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat seems to be running. Use the restart option.
else
$BASE/startup.sh 2>&1 > /dev/null
fi
stat
;;
stop)
$BASE/shutdown.sh 2>&1 > /dev/null
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ' '|cut -d ' ' -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
stat
;;
restart)
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ' '|cut -d ' ' -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
$BASE/startup.sh 2>&1 > /dev/null
stat
;;
status)
stat
;;
*)
echo "Usage: tomcat start|stop|restart|status"
esac
Now above script works with local tomcat. Now how can i modify above script to stop/start remote tomcat?
Thanks!
You could use ssh to execute a local script on the remote machine to start/stop Tomcat, so if you are in a linux terminal, you could do something like:
ssh username#remoteMachine /home/username/myScipts/start_tomcat.sh
where start_tomcat.sh would be a script in the remote machine, of course you would need a valid username/password on the remote machine, and also the remote machine would need to have sshd installed and running

nohup in sh script not working

I'm having a problem with a sh script. I'm working on a sh script that allows me to execute a java script in background, and before executing it, check if it is currently running.
The code is here:
## START ##
start)
echo $" "
echo $"Checking for agents running, wait please..."
MI_PS=$(ps -ef|grep $JAR_NAME |awk '{print $2, $7}' |egrep -v "00:00:00" |awk '{print$1}')
sleep 1
for kill_process in $MY_PS; do
/bin/kill -9 $kill_process
done
# Verificamos que se ha realizado correctamente
VERIFY=$?
if [ "$?" = "0" ]; then
echo -n "Jar instance stopped correctly"
echo -ne "\033[0;32m [OK] \033[0m"
echo $" "
else
echo -n "Jar instance stopped incorrectly"
echo -ne "\033[0;31m [Error] \033[0m"
echo ""
echo "It is required to kill process "$MY_PS" manually."
echo "Ask your administrator"
echo $" "
fi
echo $" "
echo -n "Starting Jar"
echo $" "
nohup java -Dprops.dir=conf -jar func.jar &
echo $" "
echo -n "Jar running"
echo -ne "\033[0;32m [OK] \033[0m"
echo $" "
;;
The problem I'm having is that when setting the nohup ... & in order to execute in background jar file is not doing anything. If I write:
java -Dprops.dir=conf -jar func.jar
Then it works. My jar file is using a quartz library (scheduler library for Java), I don't know if some of you knows if there is any incompatibility between nohup and quartz...
Or maybe there is somekind of problem with setting JVM variables (-Dprops.dir=conf)
Thanks!
I figured out my problem, it turned out to not be related to Quartz at all. I was using SwingUtilities.invokeLater() for some cheap asynchronous tasks. For some reason the program was working only if I had xwindows running on my desktop, but would terminate with no error logged when I shut down my computer (also shutting down xwindows). The fix was to start the program with -Djava.awt.headless=true

Categories