Script guide to prevent duplicate execution of jar in Linux - linux

Jar should run in the background and not run redundantly.
Even if duplicate execution occurs, all must be terminated

#!/bin/bash
ENV=dev
SER_NAME=batch
JAR_FULL=/was/batch/test.jar
case $1 in
restart)
if apid=$(pgrep -f $SER_NAME)
then
for pid in $apid; do
echo "Stop $SER_NAME pid : $pid"
kill -9 $pid
done
else
echo "$SER_NAME is not running ... "
fi
nohup java -jar $JAR_FULL --spring.profiles.active=$ENV > /dev/null 2>&1 &
echo "$SER_NAME" start"
;;
stop)
if apid=$(pgrep -f $SER_NAME)
then
for pid in $apid; do
echo "Stop $SER_NAME - pid : $pid"
kill -9 $pid
done
else
echo "$SER_NAME is not running ..."
fi
;;
start)
if apid=$(pgrep -f $SER_NAME)
then
echo "$SER_NAME is already running ..."
for pid in $apid; do
echo "Running $SER_NAME -pid : $pid"
done
else
nohup java -jar $JAR_FULL --spring.profiles.active=$ENV > /dev/null 2>&1 &
echo "$SER_NAME start!"
fi
;;
esac
example) run.sh
console) chmod 755 run.sh
console) ./run.sh start
start or stop or restart

Related

How to create a service on linux Centos 6

How to create service on centos 6 to kill service and run script
For example :
If i run this command service test start
it should make the below
pkill test
nohup ./opt/test/test/bin/standalone.sh -c standalone-full.xml
Also if i run this command service test stop
it should make the below
pkill test
Create a file like /etc/init.d/test
#!/bin/bash
start() {
/usr/bin/kill -9 $(cat /tmp/test_nohup_pid.txt)
nohup ./opt/test/test/bin/standalone.sh -c standalone-full.xml 2>&1 &
echo $! > /tmp/test_nohup_pid.txt
}
stop() {
/usr/bin/kill -9 $(cat /tmp/test_nohup_pid.txt)
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

How to kill all processes named "shairport" that do not have the pid 12345

I am using shairport at work to stream music. I am running it on a Debian machine(a raspberry). In its /etc/init.d/shairport file it has only the start|stop commands.
I want to add a restart one. Here is the code so far:
case "$1" in
restart)
service shairport stop
service shairport start
;;
start)
/usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
;;
stop)
killall shairport
;;
*)
echo "Usage: /etc/init.d/shairport {start|stop|restart}"
exit 1
;;
esac
exit 0
The issue is that when I run "service shairport restart", the service is stopped, thus running "killall shairport" and killing the bash script process itself. So "start" is never executed.
How do I make killall kill every shairport except the current script?
My idea was to get the pid and exclude it, but I cant find how to do that.
The start part should write down the PID of the started process.
Something like echo $? > /var/run/shairport.pid will do.
Stop part of your script will use the PID from the .pid file we have created, and kill the right process. This is what most of Linux services do as far as I know.
In linux you can know the process ID that the script file is running under with : $$.
You just have to check you're not killing yourself with :
stop)
for pid in $(pgrep shairport); do
if[$pid != $$]
kill $pid
fi
done
Since the answer I chose did not work straight out of the box, here is the code I ended up with:
case "$1" in
restart)
for pid in $(pgrep shairport); do
if [ "$pid" != $$ ]; then
kill $pid
fi
done
/usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
;;
start)
/usr/local/bin/shairport -d -a "$NAME" -p 5002 -k "madafaka" -w -B "mpc stop"
;;
stop)
killall shairport
;;
*)
echo "Usage: /etc/init.d/shairport {start|stop|restart}"
exit 1
;;
esac

My custom init shell script stops deployment script

I have a deployment script which works fine. At the end of it I added this block:
cat << EOF > /etc/init.d/uwsgi
#!/bin/bash
daemon=$APPVENV/bin/uwsgi
args="--emperor $APPCONF/uwsgi/app.ini --daemonize /var/log/emperor.log --emperor-pidfile $APPDIR/emperor.pid --gid `id -g $APPUSER`"
pid=$APPDIR/emperor.pid
case "$1" in
start)
echo "Starting uwsgi"
start-stop-daemon -p $pid --start --exec $daemon -- $args
;;
stop)
echo "Stopping script uwsgi"
start-stop-daemon --signal INT -p $pid --stop $daemon -- $args
;;
reload)
echo "Reloading conf"
kill -HUP $(cat $pid)
;;
*)
echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
exit 1
;;
esac
exit 0
EOF
Now when I run my deployment script it gets stuck here, and I just see a blinking cursor and it doesn't run any lines after it.
Have I done anything wrong with my formatting, as I know creating a file with cat was something very picky about how it was formatted, tabulated etc.
variables are still substituted in heredocs, also subshelling with $( ), which you do in your script here:
kill -HUP $(cat $pid)
your installer attempts to cat that file, with $pid probably empty, therefore it waits for input from standard input.
You want to escape those "$" to prevent expansion, like
\$(cat \$foo)
And of course with all those variables you don't want to get expanded during installation too.

Run jar file as Daemon on Linux Ubuntu

I want to install a bot to my Teamspeak3 and run this bot as a daemon on startup. I wrote my own script and copied it to init.d and then added it with update-rc.d to defaults.
#!/bin/sh
#
# JTS3ServerBot Script
#
USER="ts"
NAME="jts3"
DIR="/home/ts/jts3/"
case $1 in
start)
echo "Starting ${NAME} ..."
if [ ! -f $DIR/pid ]; then
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
echo $! > $DIR/pid
echo "${NAME} started ..."
else
echo "${NAME} is already running ..."
fi
;;
stop)
if [ -f $DIR/pid ]; then
PID=$(cat $DIR/pid);
echo "Stopping ${NAME} ..."
kill $PID;
echo "${NAME} stopped ..."
rm $DIR/pid
else
echo "${NAME} is not running ..."
fi
;;
restart)
if [ -f $DIR/pid ]; then
PID=$(cat $DIR/pid);
echo "Stopping ${NAME} ...";
kill $PID;
echo "${NAME} stopped ...";
rm $DIR/pid
echo "Starting ${NAME} ..."
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
echo $! > $DIR/pid
echo "${NAME} started ..."
else
echo "${NAME} is not running ..."
fi
;;
esac
A pid file in generated, but if i try to kill the process with this pid i get an error that the process does not exist. If i use top there is no process with the pid listed.
root#vps-1023645-8462:~# service jts3 start
Starting jts3 ...
jts3 started ...
root#vps-1023645-8462:~# cat /home/ts/jts3/pid
10206
root#vps-1023645-8462:~# kill 10206
bash: kill: (10206) - No such process
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1762 ts 20 0 1881m 14m 3408 S 0 1.4 215:47.28 ts3server_linux
32356 ts 20 0 164m 1576 1336 S 0 0.2 0:09.85 tsdnsserver_lin
I have found another solution for my problem. I use upstart (works only with Ubuntu) to run my jar-File as a daemon. Upstart manages the PIDs. Just add myservice.conf to /etc/init (not /etc/inid.d) and the daemon will be started on boot and you can mangage it as a service. You do not have to make the file runnable or anything else
You can manage the service as normal for example
service myservice restart
service myservice status
...
My Config-File:
description "myservice"
author "your name"
start on runlevel [3]
stop on shutdown
expect fork
script
cd /home/username/
sudo -u username java -jar /home/username/myservice/myservice.jar >/home/username/myservice.log 2>&1
emit myservice_running
end script
This solution is really easy and works well on my Ubuntu 12.04 Server.
You have an error in this line:
sudo -u $USER -c nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>>/dev/null >>/dev/null&
You appear to be mixing the syntaxes of sudo and su. Before version 1.8, sudo had no -c option - you just give it the command to run after any other options. In 1.8 there is a -c option but it's not for specifying the command (it's for limiting resource usage to that of a given login class). sudo is printing an error message about this invalid syntax, but you're not seeing it because you're redirecting all the output to /dev/null.
Simply remove the -c to form a valid command:
sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR 2>> /dev/null >> /dev/null &
Also, you can simplify the command a little by using the 2>&1 syntax to send stderr to the same handle as stdout, and there is no need for append mode when writing to /dev/null:
sudo -u $USER nohup java -jar $DIR/JTS3ServerMod.jar $DIR >/dev/null 2>&1 &

Bash script to check if process is running and read user input to start if necessary

Having a problem with using nohup in a script. The script works properly if nohup is not used
to start the process. The following error is received when run:
./iper.sh: line 16: syntax error near unexpected token `;'
./iper.sh: line 16: ` [Yy]*) nohup iperf -s > /dev/null 2>&1&; break;;'
Here is the full script:
#!/bin/bash
echo "Checking to see if Iperf is running:"
sleep 2
ps cax | grep iperf > /dev/null
if [ $? -eq 0 ]; then
echo "Iperf is running."
else
echo "Iperf is not running."
fi
sleep 2
while true; do
read -p "Press Y to start Iperf or N to exit: " yn
case $yn in
[Yy]*) nohup iperf -s > /dev/null 2>&1&; break;;
[Nn]*) exit;;
esac
done
What is happening?
If you're going to terminate your command with & to put it to background, do not terminate it with another semicolon ; as well:
[Yy]*) nohup iperf -s > /dev/null 2>&1& break;;
Previously
2>&1&;
I guess you have an extra & in 2>&1&
Change it to 2>&1
Check with below script
#!/bin/bash
echo "Checking to see if Iperf is running:"
sleep 1
if `pgrep iperf >/dev/null 2>&1`
then
echo "iperf Running"
else
echo "iperf not Running"
fi
sleep 1
while true; do
echo "Do you wanna start Iperf (y/n)"
read -n 1 ch; p=`echo ${ch} | tr A-Z a-z`
case $p in
y)nohup iperf -s > /dev/null 2>&1 break;;
n)exit;;
*)continue;
esac
done
when this been executed it waits for user to press*ctrl + c* to come out
if you are using 2>&1&(for continuing without user interference)allowing user to do other work
replace below line in y) condition
nohup iperf -s > /dev/null 2>&1& break;;

Resources