Tomcat start and stop using shell script? - java

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

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.

Java App as a Linux Service issue with permissions

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

spring boot init.d script start-stop-daemon: unrecognized option --no-close

after symlink my app to the /etc/init.d/myappname.
/etc/init.d/myappname start gives "Failed to start"
/var/log/appname.log tells
"start-stop-daemon: unrecognized option '--no-close'"
when i remove the --no-close, the jar becomes corrupted and cannot run anymore. i am struck.
bdw my jar is fullyexecutable jar. i.e., when i run the jar alone it starts up the springboot normally.
whats going wrong here?
EDIT:
do_start() {
working_dir=$(dirname "$jarfile")
pushd "$working_dir" > /dev/null
if [[ -n "$run_user" ]]; then
mkdir "$PID_FOLDER" &> /dev/null
checkPermissions || return $?
chown "$run_user" "$PID_FOLDER"
chown "$run_user" "$pid_file"
chown "$run_user" "$log_file"
if [ $USE_START_STOP_DAEMON = true ] && type start-stop-daemon > /dev/null 2>&1; then
arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $jarfile $RUN_ARGS "$#")
start-stop-daemon --start --quiet \
--chuid "$run_user" \
--name "$identity" \
--make-pidfile --pidfile "$pid_file" \
--background --no-close \
--startas "$javaexe" \
--chdir "$working_dir" \
-- "${arguments[#]}" \
>> "$log_file" 2>&1
await_file "$pid_file"
else
su -s /bin/sh -c "$command >> \"$log_file\" 2>&1 & echo \$!" "$run_user" > "$pid_file"
fi
pid=$(cat "$pid_file")
else
checkPermissions || return $?
$command >> "$log_file" 2>&1 &
pid=$!
disown $pid
echo "$pid" > "$pid_file"
fi
[[ -z $pid ]] && { echoRed "Failed to start"; return 1; }
echoGreen "Started [$pid]"
}
I assume you already created an executable JAR of your Spring Boot app.
Copy your app to /var/appname/appname.jar
Make sure it's given execute permission:
sudo chmod +x "/var/appname/appname.jar"
Create a config file /var/appname/appname.conf with the following content
USE_START_STOP_DAEMON=false
Follow instructions from Spring Boot Reference Guide
To install a Spring Boot application as an init.d service simply create a symlink:
$ sudo ln -s /var/appname/appname.jar /etc/init.d/appname
Once installed, you can start and stop the service in the usual way. For example, on a Debian based system:
$ service appname start
i finally solve this problem.
--no-close is a parameter that was "recently" added to start-stop-daemon
http://manpages.ubuntu.com/manpages/wily/man8/start-stop-daemon.8.html
I run my app.jar on Ubuntu 12.04 LTS that has
start-stop-daemon 1.16.1.2 for Debian
You could know what version you are running using:
start-stop-daemon --version
on linux console.
I downloaded a newer version of start-stop-daemon on
https://pkgs.org/ubuntu-14.04/ubuntu-main-amd64/dpkg_1.17.5ubuntu5_amd64.deb.html
Install the deb package and spring boot jar will finally run.
Run "service myappname start" as mentioned in the document http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html
There is a difference between /etc/init.d/myappname start and server myappname start

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.

Reach JBoss EAP 6 server in local network

I have a Java application running on JBoss EAP 6 which I can "hit" from mobile app on chrome using urls like: 127.0.0.1:8280/myAwesomeJavaApp/coolService ... so when I package the app I am using urls like this
192.168.1.64:8280/myAwesomeJavaApp/coolService //this is not working
192.168.1.64 // this gets me to my localhost
(connected to WiFi (testing purposes))
I can access all other Apache web apps in my localhost , but can't consume JBoss EAP 6 services.
What am I missing ?
Start Jboss server with -b 0.0.0.0
./standalone.sh -b.0.0.0.0
How to share jboss over network
I am using service script:
JBOSS_HOST_CONFIG=host-master.xml
JBOSS_BIND_IP=0.0.0.0
JBOSS_NODE_NAME=master
JBOSS_USER=root
JBOSS_HOME=/opt/jboss-eap-6.4
#!/bin/sh
#
# JBoss domain control script
#
# chkconfig: - 80 20
# description: JBoss AS Domain
# processname: domain
# pidfile: /var/run/jboss-as/jboss-as-domain.pid
# config: /etc/jboss-as/jboss-as.conf
# Source function library.
#. /etc/init.d/functions
# Load Java configuration.
[ -r /etc/java/java.conf ] && . /etc/java/java.conf
export JAVA_HOME
# Load JBoss AS init.d configuration.
if [ -z "$JBOSS_CONF" ]; then
JBOSS_CONF="/etc/jboss-as/jboss-as.conf"
fi
[ -r "$JBOSS_CONF" ] && . "${JBOSS_CONF}"
# Set defaults.
if [ -z "$JBOSS_HOME" ]; then
JBOSS_HOME=/usr/share/jboss-as
fi
export JBOSS_HOME
if [ -z "$JBOSS_PIDFILE" ]; then
JBOSS_PIDFILE=/var/run/jboss-as/jboss-as-domain.pid
fi
export JBOSS_PIDFILE
if [ -z "$JBOSS_CONSOLE_LOG" ]; then
JBOSS_CONSOLE_LOG=/var/log/jboss-as/console.log
fi
if [ -z "$STARTUP_WAIT" ]; then
STARTUP_WAIT=60
fi
if [ -z "$SHUTDOWN_WAIT" ]; then
SHUTDOWN_WAIT=30
fi
if [ -z "$JBOSS_DOMAIN_CONFIG" ]; then
JBOSS_DOMAIN_CONFIG=domain.xml
fi
if [ -z "$JBOSS_HOST_CONFIG" ]; then
JBOSS_HOST_CONFIG=host.xml
fi
if [ -z "$JBOSS_BIND_IP" ]; then
JBOSS_BIND_IP=
fi
if [ -z "$JBOSS_NODE_NAME" ]; then
JBOSS_NODE_NAME=
fi
JBOSS_SCRIPT=$JBOSS_HOME/bin/domain.sh
prog='jboss-as'
CMD_PREFIX=''
if [ ! -z "$JBOSS_USER" ]; then
if [ -x /etc/rc.d/init.d/functions ]; then
CMD_PREFIX="daemon --user $JBOSS_USER"
else
CMD_PREFIX="su - $JBOSS_USER -c"
fi
fi
start() {
echo -n "Starting $prog: "
if [ -f $JBOSS_PIDFILE ]; then
read ppid < $JBOSS_PIDFILE
if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
echo -n "$prog is already running"
echo 'failure!'
return 1
else
rm -f $JBOSS_PIDFILE
fi
fi
mkdir -p $(dirname $JBOSS_CONSOLE_LOG)
cat /dev/null > $JBOSS_CONSOLE_LOG
mkdir -p $(dirname $JBOSS_PIDFILE)
chown $JBOSS_USER $(dirname $JBOSS_PIDFILE) || true
#$CMD_PREFIX JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT 2>&1 > $JBOSS_CONSOLE_LOG &
#$CMD_PREFIX JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT &
if [ ! -z "$JBOSS_USER" ]; then
if [ -r /etc/rc.d/init.d/functions ]; then
daemon --user $JBOSS_USER LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -b $JBOSS_BIND_IP -Djbos.node.name=$JBOSS_NODE_NAME --domain-config=$JBOSS_DOMAIN_CONFIG --host-config=$JBOSS_HOST_CONFIG 2>&1 > $JBOSS_CONSOLE_LOG &
else
su - $JBOSS_USER -c "LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -b $JBOSS_BIND_IP -Djbos.node.name=$JBOSS_NODE_NAME --domain-config=$JBOSS_DOMAIN_CONFIG --host-config=$JBOSS_HOST_CONFIG" 2>&1 > $JBOSS_CONSOLE_LOG &
fi
fi
count=0
launched=false
until [ $count -gt $STARTUP_WAIT ]
do
grep 'JBAS015874:' $JBOSS_CONSOLE_LOG > /dev/null
if [ $? -eq 0 ] ; then
launched=true
break
fi
sleep 1
count=$(($count+1))
done
if [ "$launched" = "false" ] ; then
echo "$prog failed to startup in the time allotted"
echo 'failure!'
return 7
fi
echo 'successfuly done!'
return 0
}
stop() {
echo -n $"Stopping $prog: "
count=0;
if [ -f $JBOSS_PIDFILE ]; then
read kpid < $JBOSS_PIDFILE
let kwait=$SHUTDOWN_WAIT
# Try issuing SIGTERM
kill -15 $kpid
until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
do
sleep 1
let count=$count+1;
done
if [ $count -gt $kwait ]; then
kill -9 $kpid
fi
fi
rm -f $JBOSS_PIDFILE
echo 'successfuly done!'
echo
}
status() {
if [ -f $JBOSS_PIDFILE ]; then
read ppid < $JBOSS_PIDFILE
if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
echo "$prog is running (pid $ppid)"
return 0
else
echo "$prog dead but pid file exists"
return 1
fi
fi
echo "$prog is not running"
return 3
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
$0 start
;;
status)
status
;;
*)
## If no parameters are given, print which are avaiable.
echo "Usage: $0 {start|stop|status|restart|reload}"
exit 1
;;
esac
and start with:
/etc/init.d/jbosseap6 start

Categories