How can I restart a process If any exception occured in previous running process file(nohup.out) using shell script? - linux

I have one Jar file in SERVICE_PATH directory.Currently it is running .Suppose any exception or below exception occurred,then i need to kill the current process ,delete the nohup.out file from the directory & then restart the same jar.
Below is my sample code.using below code some time it is working but sometime it is not working.
I will schedule my autoRestart.sh file in crontab.
What is the best way to do this?
#! /bin/sh
SERVICE_PATH=/app/application/DataAdapter
STOPPED_MESSAGE="java.net.UnknownHostException"
File='/app/application/DataAdapter/nohup.out'
chmod a+x "$File"
processId=$(ps -ef | grep 'dataAdapter' | grep -v 'grep' | awk '{ printf $2 }')
echo "$processId"
if [ $processId -eq $(ps -ef | grep 'dataAdapter' | grep -v 'grep' | awk '{ printf $2 }')]
then
if grep -Fxq "$STOPPED" "$File"
then
echo "Process Id New---$processId"
kill -9 $processId
rm -rf $File
nohup java -jar dataAdapter.jar &
else
#nohup java -jar dataAdapter.jar &
echo "Msg if not found"
echo "Process Id New---$processId"
fi
else
kill -9 $processId
rm -rf $File
nohup java -jar dataAdapter.jar &
echo "processId not found"
fi
echo "Task Finished"
echo $$

Related

before executing script.sh, Check script.sh running or not, if its running kill and continue

CMD# bash script.sh
#!/bin/bash
PRE_CHECK=$0
PROCESS_ID=`ps -ef | grep "$PRE_CHECK" | egrep -v 'grep' | awk '{print $2}'`
[[ ! -z $PROCESS_ID ]] && kill -9 $PROCESS_ID
echo ""
echo ""
echo ""
In order to know this, you need to know your own process ID. This is stored in the variable $$, so you need to kill all PIDs, except for that one.

My bash script won't execute commands after kill command

I am trying to make a bash script that is killing a process and then it's going to do other stuff.
PID=`ps -ef | grep logstash | grep -v "grep" | awk '{print $2}'`
echo $PID
kill -9 $PID
echo "logstash process is stopped"
rm /home/user/test.csv
echo "test.csv is deleted."
rm /home/example.txt
echo "example.txt is deleted."
When I run the script, it kills logstash as exptected but it terminates also my whole script.
I've also tried: kill -9 $(ps aux | grep 'logstash' | awk '{print $2}').
With this command, my script will be terminated as well.
it looks like your script name includes "logstash".
As a consequence, PID is filled with 2 values, and the kill command kills your script as well.
Rename your script without "logstash" in the name should fix the issue.
This should correct your issue :
PID=$( ps -ef | grep -E '[ ]logstash[ ]' | grep -v "grep" | head -1 | awk '{print $2}')
echo $PID
kill -9 $PID
echo "logstash process is stopped"
rm /home/user/test.csv
echo "test.csv is deleted."
rm /home/example.txt
echo "example.txt is deleted."
Regards!

Check whether a process is running or not Linux

Here is my code:
#!/bin/bash
ps cax | grep testing > /dev/null
while [ 1 ]
do
if [ $? -eq 0 ]; then
echo "Process is running."
sleep 10
else
nohup ./testing.sh &
sleep 10
fi
done
I run it as nohup ./script.sh &
and it said nohup: failed to run command './script.sh': No such file or directory
What is wrong?
The file script.sh simply does not exist in the directory that you are issuing the command from.
If it did exist and was not executable you would get:
`nohup: failed to run command ‘./script.sh’: Permission denied
For each newly created scripts on Linux, you should first change the permission as you can see the permission details by using
ls -lah
The following content may help you:
#!/bin/bash
while [ 1 ];
do
date=`date`
pid=`ps -ef | grep "your process" | grep -v grep | awk -F' ' '{print $2}'`
if [[ -n $pid ]]; then
echo "$date - processID $pid is running."
else
echo "$date - the process is not running"
# script to restart your process
say: start the process
fi
sleep 5m
done
Make sure your script is saved as script.sh
and your executing nohup ./script.sh & from the same directory in which script.sh.
Also you can give executable permission for script.sh by
chmod 776 script.sh
or
nohup ./script.sh &
Run as
nohup sh ./script.sh &

How to write a shell script to find PID and Kill

I am trying to write a script that looks for the PID of another script that ran previously. Once it finds that PID it then sends a kill signal.
I can find the PID but the kill signal does not process, I get a return message that it is not a PID.
Here is what I am doing:
#!/bin/bash
PID=`ps -eaf | grep "start.sh" | awk '{print $2}'`
echo "$PID"
if [[ -z "$PID" ]];
then(
echo "Start script is not running!"
)else(
kill -9 $PID
)fi
The script it is trying to kill starts many other scripts so I am hoping that killing start.sh will kill all child processes.
When you run
ps -eaf | grep "start.sh" | awk '{print $2}'
you create a subshell with the word start.sh in it. grep will then pick up on its own process and the start.sh one so you will get two PIDs back.
This means when you are trying to kill both start.sh and the
ps -eaf | grep "start.sh" | awk '{print $2}'
processes. The start.sh will die but the other will no longer exist so can't be killed, so it gives you an error.
If you were to split up the commands you might have better luck:
PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "start.sh" | awk '{print $2}')
Try using pgrep:
PID=$(pgrep yourprocesname)
You could use pidof rather then ps -ef
PID=`pidof start.sh | awk '{print $1}'`
echo "$PID csshst will be stopped"
if [[ -z "$PID" ]]; then
(
kill -9 $PID
)
fi
Here is another solution that may be useful for someone:
#!/bin/bash
kill-process-by-name(){
processes=$(ps -A)
kill -9 `echo "$processes" | grep "$1" | cut -f2 -d" "`
}
kill-process-by-name "start.sh"
trashPID=`pidof start.sh | awk '{print $1}'`
echo "$PID csshst will be stopped"
if [[ -z "$PID" ]]; then
(
kill -9 $PID
)
fi
purely

Why part of the script cannot execute in the crontab

I have a script stopping the application and zipping some files:
/home/myname/project/stopWithZip.sh
With the properties below:
-rwxrwxr-x. 1 myname myname778 Jun 25 13:48 stopWithZip.sh
Here is the content of the script:
ps -ef | grep project | grep -v grep | awk '{print $2}' |xargs kill -15
month=`date +%m`
year=`date +%Y`
fixLogs=~/project/log/fix/$year$month/*.log.*
errorLogs=~/project/log/error/$year$month/log.*
for log in $fixLogs
do
if [ ! -f "$log.gz" ];
then
gzip $log
echo "Archived:"$log
else
echo "skipping" $log
fi
done
echo "Archived fix log files done"
for log in $errorLogs
do
if [ ! -f "$log.gz" ]; then
gzip $log
echo "Archived:"$log
else
echo "skipping" $log
fi
done
echo "Archived errorlog files done"
The problem is except this ps -ef | grep project | grep -v grep | awk '{print $2}' |xargs kill -15 command, other gzip commands are not executed. I totally don't understand why.
I cannot see any compression of the logs in the directory.
BTW, when I execute the stopWithZip.sh explicitly in command line, it works perfectly fine.
In crontab:
00 05 * * 2-6 /home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1 (NOT work)
In command line:
/home/myname/project>./stopWithZip.sh (work)
Please help
The script fails when run under cron because your script is invoked with project in its path, so the kill pipeline kills the script too.
You could prove (or disprove) this by adding some tracing. Log the output of ps and of awk to log files:
ps -ef |
tee /tmp/ps.log.$$ |
grep project |
grep -v grep |
awk '{print $2}' |
tee /tmp/awk.log.$$ |
xargs kill -15
Review the logs and see that your script is one of the processes being killed.
The crontab entry contains:
/home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1
When ps lists that, it contains 'project' and does not contain 'grep' so the kill in the script kills the script itself.
When you run it from the command line (using a conventional '$' as the prompt), you run:
$ ./stopWithZip.sh
and when ps lists that, it does not contain 'project' so it is not killed.
If you ran:
$ /home/myname/project/stopWithZip.sh >> /home/myname/project/cronlog/$(date +"\%F")-stop.log 2>&1
from the command line, like you do with cron (crontab), you would find it fails.

Resources