Bash shell script to check if script itself is running
hi guys
we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the affected client servers that monitors the processes, and if its not running it will use Logger to produce a string of sentence that I can easily pick up with nagios monitoring. all i have to do then is just add the "key words" into nagios check configuration which already monitors /var/log/messages
So the script that looks for process to check if its running or not is as such:
Code:
# more /usr/local/bin/process
#!/bin/bash
PROCESS="httpd"
if /etc/init.d/$PROCESS status | grep running.> /dev/null
then
logger -s "$PROCESS is running"
else
logger -s "$PROCESS is not running"
fi
so how do i monitor to see if the script itself is running? I tried with ps -ef but you know ps is still going to show me two lines (one with ps, the other if script is running) so its not really a good way to check, isnt it?
when i put the job in the background with nohup and grep with ps aux I get nothing
I see your script is checking if the process is running, using logger to log the status and terminating. This is the reason why you are not seeing it running. You should put this in an infinite while loop with a sleep time of your choice for periodically checking and logging the status.
E.g:-
Code:
#!/bin/bash
PROCESS="httpd"
while (true)
do
if /etc/init.d/$PROCESS status | grep running.> /dev/null
then
logger -s "$PROCESS is running"
else
logger -s "$PROCESS is not running"
fi
sleep 300
done
Now you can start your script using nohup and open another terminal to check if it is running:-
if i use flock i can see messages in my /var/log but how do i ensure that it only gets written into if a. the script is running b. the script is not running c. the process is running d. the process is not running
Code:
#!/bin/bash
SERVICE="httpd"
CHECK_SCRIPT="/usr/local/bin/process"
if flock -n /var/run/your.lockfile -c /usr/local/bin/process >/dev/null
then
logger -s "$CHECK_SCRIPT is running. Do nothing"
else
logger -s "$CHECK_SCRIPT is not running. Please restart script"
fi
if pgrep -l $SERVICE | grep $SERVICE >/dev/null
then
logger -s "$SERVICE process is running. Do nothing"
else
logger -s "$SERVICE process is not running. Please restart service"
fi
now, this script only detects the script is running and if the process is running/not running...a little help please to make it reliase the script is not running
OMG bipin you're totally right. hang on let me try this again.
so heres whats happening.
If i run the script after starting httpd i get this:
Code:
Nov 4 23:56:06 hedkandi root: /usr/local/bin/test is not running. Please restart script--this isnt right, it should be showing /usr/local/bin/test is running. Do nothing
Nov 4 23:56:06 hedkandi root: httpd process is not running. Please restart service
If i run the script after stopping httpd I get this:
Code:
Nov 4 23:56:06 hedkandi root: /usr/local/bin/test is running. Do nothing
Nov 4 23:56:06 hedkandi root: httpd process is not running. Please restart service
and the other two thing thats totally not working is if
i stop script and process is running it should give me a
/usr/local/bin/test is not running. Please restart script
httpd process is running. Do nothing
if i stop script and stop process it should give me a
/usr/local/bin/test is not running. Please restart script
httpd process is not running. Please restart service
I am working on script. it reads a file which contains multiple lines
Ex;
curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=1
curl --write-out %{http_code} --silent --output /dev/null http://hostname:port/input=2
curl --write-out %{http_code} --silent ... (2 Replies)
How to check the day name,is it saturday in bash shell script.
If dayname = saturday then
run the full load
else
run just the incremental loads
end if
Thank you very much for the helpful information. (4 Replies)
I have one shell script in NAS shared location. Since NAS is mounted in many servers, script can be run from any of those servers.
I want to make sure if the script is already running, it should not allow others to run it. I googled it and got some idea that i can touch one empty file in the... (8 Replies)
Hi,
I want to write a script which continuously checking status of a script running in background by nohup command. And if same script is not running then immediately start the script...please help..
i am using below command to run script
nohup system_traps.sh &
but in some... (9 Replies)
I'm a bit new to bash programming and I was assigned the job of writing a script that will check to see if a program server is running and to restart the program if it is not up. The script is supposed to check the program every hour (which I have looked up and I believe I know how to do) and send... (3 Replies)
Hey guys, so I wrote a small script that pretty much just takes in two numbers and counts from the first to the second, e.g.
unknown-hacker|544> count.sh 1 3
1
2
3
My problem is I want to make it so that if you input invalid parameters, such as non-numerical characters, more than 2... (2 Replies)
Hi guys,
I'm new to scripting and I'm wondering if I can run a matlab script from within a bash script.
Basically, at the minute I have a bash script which creates a file "bottfile.xyz"
I then import "bottfile.xyz" into matlab using importdata('bottfile.xyz'). Then I just run the matlab... (0 Replies)
Hi,
I am a unix newbie. I need to write a script to check wheteher another script is still running. If it is, then sleep for 30m and then check again if the script is running. If the script has stopped running then, I need to come out of the loop.
I am using RHEL 5.2 (2 Replies)
I want to write a unix shell script that will check if a process (say debu) is running in the server or not. If no , then send a mail to the corresponding person to start the process??? (2 Replies)