script to check script is running and start it, if it’s stopped - linux

I have a script with this name : Run.sh
I run this script with this command :
./run.sh
I don't like stop this script but this script Suddenly stops and need run again.
I need a script to check it , if my run.sh stopped , run it again.
this is run.sh codes:
#!/usr/bin/env bash
install() {
sudo apt-get update
sudo apt-get upgrade
}
if [ "$1" = "install" ]; then
install
else
if [ ! -f ./tg/tgcli ]; then
echo "tg not found"
echo "Run $0 install"
exit 1
fi
#sudo service redis-server restart
#./tg/tgcli -s ./bot/bot.lua -l 1 -E $#
./tg/tgcli -s ./bot/bot.lua $#
fi
And i want run this script at boot (with screen or tmux) if my server restart
i have Ubuntu 16.04 version
Thank you Ljm Dullaart
Can you help me about this ?

You should not need to run the complete bash script again. Changing
./tg/tgcli -s ./bot/bot.lua $#
to
while :; do
./tg/tgcli -s ./bot/bot.lua $#
done
will restart bot.lua everytime it exits.

You can check if your run.sh is running and re-run it if stopped with a single command:
$ if ! pgrep run.sh ;then /path/to/run.sh;fi
If script runs pgrep will return exit status 0 = success and will print the pid of run.sh
If script does not run pgrep will return exit status 1 and then script will be called.
You can also use pgrep inst.sh >/dev/null to "mute" pgrep in case script is running.

Related

Bash - Without check if command exist different result with and without sudo

I have this test.sh script:
if tesseract --version >/dev/null 2>&1; then
echo Found
else
echo Not found
fi
If I execute ./test.sh , it prints Found, but if I execute it with sudo sudo ./test.sh, then it says that is Not found.
Any help? :)

Bash script iterate over PID's and kill items

I try to kill all occurrences of a process, what's happen actually an iteration stops after first item, what's wrong here ?
#!/usr/bin/env bash
SUPERVISORCLS=($(pidof supervisorctl))
for i in "${SUPERVISORCLS[#]}"
do
echo $i
exec sudo kill -9 ${i}
done
Before I tried sth like this as solution for restart script, but as well script was not always executed at total always only one if block was executed.?
ERROR0=$(sudo supervisord -c /etc/supervisor/supervisord.conf 2>&1)
if [ "$ERROR0" ];then
exec sudo pkill supervisord
exec sudo supervisord -c /etc/supervisor/supervisord.conf
echo restarted supervisord
fi
ERROR1=$(sudo supervisord -c /etc/supervisor/supervisord.conf 2>&1)
if [ "$ERROR1" ];then
exec sudo pkill -9 supervisorctl
exec sudo supervisorctl -c /etc/supervisor/supervisord.conf
echo restarted supervisorctl
fi
exec replaces your process with the executable that's the argument to it, so you will never execute another statement in your script after it hits an exec. Your process will no longer exist. In the first example your process will no longer be your script it will be kill and pkill in the second.
To fix it, just remove exec from all those lines. It's not needed. When executing a script the shell will execute the commands on every line already, you don't have to tell it to do so.

How are PIDs modified with exec?

According to the documentation, an exec does not modify the pid of a process.
I'm using a service to lauch my process, and aim to get his pid to save him in /var/run/.
For that i use $!.
My init script make a call to a .sh file that does an exec to an other .sh file. This file then make an exec call to java.
In the end, the pid the java app has is not the one I get in my init script. Why ?
Note : when i make only one sh script that does exec java, that works. But I don't see why adding one exec would change anything.
Code if it can help understanding.
Init script:
$DAEMON > /var/local/red5/log/jvm.stdout 2>&1 &
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo $! > $PIDFILE
fi
$DAEMON calls :
if [ -z "$RED5_HOME" ]; then export RED5_HOME=`pwd`; fi
ulimit -n 32767
# start Red5
exec /bin/bash $RED5_HOME/red5.sh > $RED5_HOME/log/jvm.stdout 2>&1 &
And my red5.sh calls java : (with a few exports before that)
# start Red5
exec "$JAVA" "$JYTHON" -Dred5.root="${RED5_HOME}" $JAVA_OPTS -cp "${RED5_CLASSPATH}" "$RED5_MAINCLASS" $RED5_OPTS
Works if i do :
if [ -z "$RED5_HOME" ]; then export RED5_HOME=`pwd`; fi
ulimit -n 32767
# start Red5
exec "$JAVA" "$JYTHON" -Dred5.root="${RED5_HOME}" $JAVA_OPTS -cp "${RED5_CLASSPATH}" "$RED5_MAINCLASS" $RED5_OPTS
Result :
pid via ps -ef: 15950.
pid with $! : 15947
Any idea ?
Thank you.
Your original $DAEMON is starting another new process when you call exec as a background process, so your Java program does run in a separate process than the background process started in the init script. Just run exec with
if [ -z "$RED5_HOME" ]; then export RED5_HOME=`pwd`; fi
ulimit -n 32767
# start Red5
# CHANGE: no ampersand at the end of this line
exec /bin/bash $RED5_HOME/red5.sh > $RED5_HOME/log/jvm.stdout 2>&1
You submit your job in the background when appending a & character, and thus it gets its own new pid.
EDIT: Thanks for the downvote. I meant this line:
exec /bin/bash $RED5_HOME/red5.sh > $RED5_HOME/log/jvm.stdout 2>&1 &

Cannot start a script

#!/bin/bash
value=$(<man.txt)
echo "$value"
if [ "$value" == "true" ]; then
echo "startedif_manthan"
ps -ef|grep sym |awk '{ print $2 }'|sudo xargs kill -9;
sleep 30;
sudo sh /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server;
fi
Hi There, I have this script killing a process and restart the script in some time. It is killing the script normally but the restart script (sudo sh /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server) is not running properly. when I run the script manually also it is giving problems. I don't know whether it is a shell script or not. But when I tried to go manually to the script location and execute this command ./sym --port 8082 --server the script running normally.
Any suggestions?
Since you say it works OK when you cd to the script directory, then do that in the script:
#!/bin/bash
value=$(<man.txt)
echo "$value"
if [ "$value" == "true" ]
then
echo "startedif_manthan"
ps -ef|grep sym |awk '{ print $2 }'|sudo xargs kill -9
sleep 30
(cd /var/www/symmetric-ds-3.1.6/bin; sudo sh ./sym --port 8082 --server)
fi

How to tell the status of a Linux Daemon

We have a Linux Daemon in c and a bash script to start it. The daemon sometimes fail to start because of some configuration file errors but the script reports the daemon was started successfully. A snippet of the script is shown as below, could someone tell me what's wrong with the script?
...
case "$1" in
start)
echo -n "Starting Demo Daemon: "
sudo -u demouser env DEMO_HOME=$DEMO_HOME /usr/local/demouser/bin/democtl startup > /dev/null 2> /dev/null
if [ "$?" = "0" ]; then
echo_success
else
echo_failure
fi
echo
;;
...
Thanks!
I feel there is nothing wrong with the script,it is the reponsibility of daemon to return non zero exit status if failed to start properly and based on those the script will display the message.(which i think it does)
You can add following line in your script to get running status of your Linux Daemon
status=`ps -aef |grep "\/usr\/local\/demouser\/bin\/democtl" |grep -v grep|wc -l`
if [ "$status" = "1" ]; then
echo_success
else
echo_failure
fi

Resources