Shell script using if statement to check if process is running:
================================================================
#!/bin/bash
sendmail=`(ps -ef |grep -v grep |grep sendmail)`
if [ -z "$sendmail" ];
then
echo "Sendmail is not running"
else
echo "Sendmail is running"
fi
WHat each line means:
---------------------
1) #!/bin/bash
This generates a Process ID for shell
2) PROCESS=`(ps -ef |grep -v grep |grep sendmail)`
Defining Variable Process:
# When we grep a process ps -ef |grep sendmail, the output result also shows "grep sendmail" line, in order to avoid that we use grep -v grep.
3) if [ -z "$sendmail" ];
-z option tells it should return a null value(no value)
Check for more : man bash
-n str True if string str is not a null string
-z str True if string str is a null string
Showing posts with label if statement. Show all posts
Showing posts with label if statement. Show all posts
Sunday, September 07, 2008
Tuesday, May 27, 2008
Check OS Script ...
I had to check all hosts in a list , if its windows or unix.
Here's what helps identify whether it is Unix or Windows,
Note: Our Windows systems did not have ssh daemon running,
bash-2.03$ cat checkos.sh
#!/bin/sh
for host in `cat list_of_host`
do
if version=`(sleep 1 ; echo "") | telnet $host 22 | grep -i SSH`
then
echo "$host $version" >> HOSt-SSH-Running
else
echo "$host" >> HOST-SSH-Not-running
fi
done
The above scripts verify if the list of host in file has ssh daemon
running on port 22, in case if it finds running then it appends
that host to HOSt-SSH-Running and rest in HOST-SSH-Not-running.
Here's what helps identify whether it is Unix or Windows,
Note: Our Windows systems did not have ssh daemon running,
bash-2.03$ cat checkos.sh
#!/bin/sh
for host in `cat list_of_host`
do
if version=`(sleep 1 ; echo "") | telnet $host 22 | grep -i SSH`
then
echo "$host $version" >> HOSt-SSH-Running
else
echo "$host" >> HOST-SSH-Not-running
fi
done
The above scripts verify if the list of host in file has ssh daemon
running on port 22, in case if it finds running then it appends
that host to HOSt-SSH-Running and rest in HOST-SSH-Not-running.
Subscribe to:
Posts (Atom)