send notification email if process is not running - shell

I am using the following shellscript to check running process in a cron job:
ps -ef|grep myprocess|grep -v "grep"
I then need to send an email if the result is empty(meaning process is not running), how do I script this?

One solution:
pgrep processname &>/dev/null && exit 0
echo |mail -s"Aiie, process processname not running!" mail#addresse.here
Then put it in a crontab like already suggested. Of course, it means you must have the mail command installed.

You can do it this way
PROCESS_FOUND=`ps -ef|grep myprocess|grep -v grep`
if [ "$PROCESS_FOUND" = "" ]
then
#send mail from here ...Process not running
fi

Related

To check whether nohup service is running or not using shell script?

I had created a nohup service using the below command in putty.
nohup php /var/www/html/XYZ/sample.php &
This command executes the sample.php file in background.
Now what i need is i want a shell script which checks whether this service is running or not.Incase if the service is not running i want that shell script to create a service by its own. Below is the code what i tried.
#!/bin/bash
email_to="xyz#gmail.com";
export DISPLAY=:0.0
PIDS=`ps -aux | grep sample.php|awk '{print $2}'`
if [ -z "$PIDS" ]; then
echo "$(date) - The service is not running. Sending email to :$email_to" >> /var/www/html/XYZ/sample.php;
echo "SERVICE is not running - $(date)" | mail -s "service is not running - $(date)" $email_to
echo "" >> /var/www/html/XYZ/sample.php;
exit 1
else
echo "$(date) - Service already running. Sending email to : $email_to" >> /var/www/html/XYZ/sample.php;
echo "SERVICE is running - $(date)" | mail -s "SERVICE is running - $(date)" $email_to
fi
when i execute the file i get the mail as service is running ,and once i kill the sample.php and when i get execute this file i get the same mail "as service is running" but its wrong ,so can anyone direct me where have i gone wrong?
where have i gone wrong?
With ps -aux | grep sample.php, the grep is finding sample.php in its own process command line grep sample.php, also output by ps. This can be avoided by modifying the grep command so that it doesn't contain sample.php literally, e. g. grep 'sample\.php' (which by the way averts the risk of matching another character instead of the .). You'll probably also need wide output from ps to not truncate the command, so change the above pipeline to ps waux | grep 'sample\.php'.

Write the script to check remote host services running or not [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 6 years ago.
This is the script but output is wrong even Apache is running its show stop. I'm using Ubuntu 12.04.
ssh -qn root# ip
if ps aux | grep [h]ttpd > /dev/null
then
echo "Apcache is running"
else
echo "Apcahe is not running"
fi
Try the following:
if ssh -qn root#ip pidof httpd &>/dev/null ; then
echo "Apache is running";
exit 0;
else
echo "Apache is not running";
exit 1;
fi
These exit commands will send the correct EXIT_SUCCESS and EXIT_FAILURE too ( Will be usefull to extend this script in future, if you need ).
But ONE ADVICE : Is better to put the script as a remote process to run with a sudoer user over ssh account
You are not running the commands on the remote host.
Try this instead.
if ssh -qn root#ip ps aux | grep -q httpd; then
echo "Apache is running"
else
echo "Apache is not running"
fi
Just to be explicit, ps aux is the argument to ssh and so that is what is being executed on the remote host. The grep runs as a child of the local script.
First of all httpd is not available in ubuntu. For ubuntu apache2 is available.
So this command ps aux | grep [h]ttpd will not work on ubuntu.
No need to write any script to check the apache status. From ubuntu terminal run this command to get the status:
sudo service apache2 status
Output will be:
A > if apache is running: Apache2 is running (pid 1234)
B > if apache is not running: Apache2 is NOT running.
Since ssh returns with exit status of the remote command check man page for ssh and search for exit status
so Its as simple as
ssh root#ip "/etc/init.d/apache2 status"
if [ $? -ne 0 ]; then # if service is running exit status is 0 for "/etc/init.d/apache2 status"
echo "Apache is not running"
else
echo "Apache is running"
fi
You do not need ps or grep for this

run command in bash and then exit without killing the command

I am attempting to run a couple commands in a bash script however it will hang up on my command waiting for it to complete (which it wont). this script is simply making sure its running.
#!/bin/bash
ps cax | grep python > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running... Starting..."
python likebot.py
echo $(ps aux | grep python | grep -v color | awk {'print $2'})
fi
Once it gets to the python command it hangs up while the command is being executed. its not till i cntrl c before it gives the pid. is there anyway i can have it run this bash script and exit the bash script once the commands were run (without waiting for them to complete).
In general, if you want to execute a command and not wait for it, you can simply use & as the delimiter rather than ; or a newline. When doing so, the pid of that process is available to the shell in the special variable !. If you want to wait for that process to complete, you can use wait. If you do not wish to wait for it, then simply omit the wait. In your case:
python likebot.py & # Start command asynchronously
echo $! # echo the pid of the most recent asynchronous process
Since it looks like likebot should be always running you might want to consider 'nohup' as well, with a bare & the job is still a child of your login process and will die if that dies.

killing the background process in a shell script

I have a script that starts background processes.
#!/bin/sh
./process1.sh &
./process2.sh &
I need to kill these processes using a separate script.
Here is what I did:
#!/bin/sh
# the kill.sh
pid=$(ps | grep './process1.sh' |grep -v grep| awk '{print $1}')
kill -9 $pid
Question time:
When the kill.sh is called processes are stoped. But I get the message
"sh: you need to specify whom to kill".
Why is that?
After I kill the process using the described script, it doesn't stop immediately.For a while I see the output on the screen as if the process is still running. Why?
What could be an alternative solution to kill the processes?
Worth to mention that I am working with busybox do I have limited choice of utilities.
You could store the process ids in a temporary file like this:
#!/bin/sh
./process1.sh &
echo $! > /tmp/process1.pid
./process2.sh &
echo $! > /tmp/process2.pid
and then delete it with your script. $! returns the PID of the process last executed.
kill -9 `cat /tmp/process*.pid`
rm /tmp/process*.pid
Make sure the process*.pid files get deleted after the corresponding script is finished.
Because your kill command failed as pid is empty.
pid=$(ps | grep './process1.sh' |grep -v grep| awk '{print $1}')
This doesn't give you the pid you want. When you start a process in the background, it's executed in a new shell and you won't see the process.sh in your ps output.
What you can do is save the PIDs when you start the background processes and kill them:
./process1.sh &
pid1=$! # Save the previously started background's PID
./process2.sh &
pid2=$! # Save the previously started background's PID
echo $pid1 " " $pid2 > /tmp/killfile
Then get the PIDs from this file and pass it to kill.

Kill other bash daemons from the same script

I am having a hell of a time trying to write a "kill all other daemon processes" function for use within a bash daemon. I do not ever want more than one daemon running at once. Any suggestions? This is what I have:
#!/bin/bash
doService(){
while
do
something
sleep 15
done
}
killOthers(){
otherprocess=`ps ux | awk '/BashScriptName/ && !/awk/ {print $2}'| grep -Ev $$`
WriteLogLine "Checking for running daemons."
if [ "$otherprocess" != "" ]; then
WriteLogLine "There are other daemons running, killing all others."
VAR=`echo "$otherprocess" |grep -Ev $$| sed 's/^/kill /'`
`$VAR`
else
WriteLogLine "There are no daemons running."
fi
}
killOthers
doService
It works some of the time, it doesn't others. There is almost nothing consistent.
You've already eliminated the current process ID using grep -v so there's no reason to do it again when you issue the kill. There's also no reason to build the kill in a variable. Just do:
kill $otherprocess
But why not just use:
pkill -v $$ BashScriptName
or
pkill -v $$ $0
without any grep.
Then you can do:
if [[ $? ]]
then
WriteLogLine "Other daemons killed."
else
WriteLogLine "There are no daemons running."
fi
Could you try the old 'lock file' trick here? Test for a file: if it doesn't exists, create it and then startup; otherwise exit.
Like:
#!/bin/bash
LOCKFILE=/TMP/lockfile
if [ -f "$LOCKFILE" ]; then
echo "Lockfile detected, exiting..."
exit 1
fi
touch $LOCKFILE
while :
do
sleep 30
done
rm $LOCKFILE # assuming an exit point here, probably want a 'trap'-based thing here.
The downside is you have to clean-up lock-files from time to time, if an orphan is left behind.
Can you convert this to a 'rc' (or S*/K* script ?) so you can specify 'once' in the inittab (or equivalent method - not sure on MacOS) ?
Like what is described here:
http://aplawrence.com/Unixart/startup.html
EDIT:
Possibly this Apple Doc might help here:
http://developer.apple.com/mac/library/DOCUMENTATION/MacOSX/Conceptual/BPSystemStartup/Articles/StartupItems.html
If you run your service under runit — the service mustn't fork into the background — you'll have a guarantee there is exactly one instance of it running. runit starts the service if it isn't running or if it quit or crashed, stops it if you ask, keeps a pidfile around.

Resources