Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

using shell script

Question posted by: madhu3437 (Newbie) on April 4th, 2008 09:50 AM
help ,please

using shell script
i want know the all process are running or not in a particular path.
the result will shown in web page (html).

process name = running ( server is running)
process name1 = not running( here server is not running)
process name 2 = running ( server is running)

above 3 lines shown in web page.

please help very urgent to me.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
prn's Avatar
prn
Expert
200 Posts
April 4th, 2008
11:05 AM
#2

Re: using shell script
Quote:
Originally Posted by madhu3437
using shell script
i want know the all process are running or not in a particular path.
the result will shown in web page (html).

process name = running ( server is running)
process name1 = not running( here server is not running)
process name 2 = running ( server is running)

The usual way of determining if a particular process is running is to us ps piped to grep for the process. For example, if you want to determine whether Apache is running, you can do something like:
Code: ( text )
  1. ps | grep httpd
and get back some number of lines showing all the processes with httpd in them.

Perhaps unfortunately, one process line you are likely to get back will be "grep httpd". :)

So you may want to filter that out with something like
Code: ( text )
  1. ps -ef | grep -v grep | grep httpd
And this will return only the lines showing actual instances of Apache. Now you are in a position to adapt this to your shell script (presumably being run by the web server---see below) where you just want to return a line saying that apache is or is not running. (You may notice something odd about the particular example I have chosen. :) )

Here, you don't really care about the exact content of the lines, in fact you probably don't want to actually display them. You just want a "count" of the number of lines returned so you know whether there were some or none:
Code: ( text )
  1. if [ `ps -ef | grep -v grep | grep -c httpd` -gt 0 ] ; then
  2.         echo "<P>apache is running</P>"
  3.         exit
  4. else
  5.         echo "<P>apache is not running</P>"
  6.         exit
  7. fi


Now comes the warning: You need root privileges to look at processes being run by other users and your web server should NOT be running with root privileges. It will usually run either as "www" or as "nobody". So you need to grant LIMITED privs to the user that runs the web server. (I'll assume "www" for this example.) By far the best way to do this is to use sudo. Here is another thread with some discussion of how to go about that.

In this case, (let's assume desdemona is the name of your server) the easiest thing might be to put the actual script (let's call it foo.cgi) for this specific function directly into cgi-bin (which may be /usr/local/apache/share/cgi-bin, for example) and set the privs for it as:
Code: ( text )
  1. chmod 0744 foo.cgi
so that only root can execute it and then include the lines:
Code: ( text )
  1. # User privilege specification
  2. root    ALL=(ALL) ALL
  3. www     desdemona=NOPASSWD:/usr/local/apache/share/cgi-bin/foo.cgi

in your /etc/sudoers file.

I know that this is probably more information and less "how-to" than you may have been hoping for, but in fact, it's not quite as simple as dropping in a pre-made script.

HTH,
Paul

Reply
madhu3437's Avatar
madhu3437
Newbie
12 Posts
April 5th, 2008
02:17 PM
#3

Re: using shell script
Thaks very much Paul
one more thing. Below shell script i create all servers are running or not
but i wrote like
i create a file, name of the file is =input_file(in that file put the all servers name)
and run the scripts
getting out put like

SERVER NAME

NETXfnic_uas02_31008 = 1
NETXacs2_uas02_31011 = 0 (this name AMERICAN CENTURY2)
NETXnetxpress_uas02_31013 = 1 (this name NETXPRESS)
NETXjohnston_uas02_31035 = 1
NETXemerald_uas02_31039 = 1
NETXjapan_uas02_31044 = 1
NETXncbank_uas02_31073 = 1
NETXcunamutual_uas02_31069 = 1
NETXhibernia_uas02_31081 = 1

but i want OUTPUT like

AMERICAN CENTURY2 NOT RUNNING
NETXPRESS RUNNING

(NETXacs2_uas02_31011) this is the server name but output like AMERICAN CENTURY2 i want show in the web page


so many servers we have ,so we show the in the web page like
server name = running
server name1 = not running

AMERICAN CENTURY2 NOT RUNNING
NETXPRESS RUNNING

var=`cat input_file`

if [ -f ./out.htm ]
then
rm ./out.htm
fi

for list_of_proc in $var
do
echo $list_of_proc
ps -eaf | grep $list_of_proc >> ./out.htm >&1
done

for list_of_proc in $var
do
flag=`grep "NETXcppServer -name $list_of_proc" out.htm|wc -l`

startTag="<html><head></head><body><h1>"
endTag="</h1></body></html>"
echo "$startTag" "$list_of_proc" "=" "$flag" "$endTag" >> /u/mkulluru/dev/html/abc.htm
done

please reply me it help and carrier will good for me also

Regards,
Madhu















Quote:
Originally Posted by prn
The usual way of determining if a particular process is running is to us ps piped to grep for the process. For example, if you want to determine whether Apache is running, you can do something like:
Code: ( text )
  1. ps | grep httpd
and get back some number of lines showing all the processes with httpd in them.

Perhaps unfortunately, one process line you are likely to get back will be "grep httpd". :)

So you may want to filter that out with something like
Code: ( text )
  1. ps -ef | grep -v grep | grep httpd
And this will return only the lines showing actual instances of Apache. Now you are in a position to adapt this to your shell script (presumably being run by the web server---see below) where you just want to return a line saying that apache is or is not running. (You may notice something odd about the particular example I have chosen. :) )

Here, you don't really care about the exact content of the lines, in fact you probably don't want to actually display them. You just want a "count" of the number of lines returned so you know whether there were some or none:
Code: ( text )
  1. if [ `ps -ef | grep -v grep | grep -c httpd` -gt 0 ] ; then
  2.         echo "<P>apache is running</P>"
  3.         exit
  4. else
  5.         echo "<P>apache is not running</P>"
  6.         exit
  7. fi


Now comes the warning: You need root privileges to look at processes being run by other users and your web server should NOT be running with root privileges. It will usually run either as "www" or as "nobody". So you need to grant LIMITED privs to the user that runs the web server. (I'll assume "www" for this example.) By far the best way to do this is to use sudo. Here is another thread with some discussion of how to go about that.

In this case, (let's assume desdemona is the name of your server) the easiest thing might be to put the actual script (let's call it foo.cgi) for this specific function directly into cgi-bin (which may be /usr/local/apache/share/cgi-bin, for example) and set the privs for it as:
Code: ( text )
  1. chmod 0744 foo.cgi
so that only root can execute it and then include the lines:
Code: ( text )
  1. # User privilege specification
  2. root    ALL=(ALL) ALL
  3. www     desdemona=NOPASSWD:/usr/local/apache/share/cgi-bin/foo.cgi

in your /etc/sudoers file.

I know that this is probably more information and less "how-to" than you may have been hoping for, but in fact, it's not quite as simple as dropping in a pre-made script.

HTH,
Paul

Reply
ghostdog74's Avatar
ghostdog74
Expert
496 Posts
April 6th, 2008
03:02 PM
#4

Re: using shell script
Quote:
Originally Posted by prn

Perhaps unfortunately, one process line you are likely to get back will be "grep httpd". :)

So you may want to filter that out with something like
Code: ( text )
  1. ps -ef | grep -v grep | grep httpd
And this will return only the lines showing actual instances of Apache.l


don't have to use that many greps, when awk does the job :)
Code: ( text )
  1. ps -ef| awk '/httpd/'

Reply
prn's Avatar
prn
Expert
200 Posts
April 7th, 2008
05:41 PM
#5

Re: using shell script
Quote:
Originally Posted by ghostdog74
don't have to use that many greps, when awk does the job :)
Code: ( text )
  1. ps -ef| awk '/httpd/'


Eh?

Code: ( text )
  1. [root@deimos ~]# ps -ef | grep 'bash'
  2. prn       2915  2912  0  2007 pts/1    00:00:00 bash
  3. root      2949  2944  0  2007 pts/1    00:00:00 -bash
  4. prn      22993 22984  0 Mar07 pts/2    00:00:00 bash
  5. prn      26492 26491  0 12:54 pts/3    00:00:00 -bash
  6. root     26526 26525  0 12:54 pts/3    00:00:00 -bash
  7. root     26581 26526  0 12:57 pts/3    00:00:00 grep bash
  8. [root@deimos ~]# ps -ef | grep -v grep | grep -c bash
  9. 5
  10. [root@deimos ~]# ps -ef | awk 'bash'
  11. [root@deimos ~]#

If, in those circumstances, I grep just for "bash", I get back 5 instances plus the grep itself. If I use -v to exclude the "grep" line, I get back the proper count. If I use the awk expression you gave, I am getting back nothing.

I could always use something like:
Code: ( text )
  1. [root@deimos ~]# ps -ef | awk /bash/
  2. prn       2915  2912  0  2007 pts/1    00:00:00 bash
  3. root      2949  2944  0  2007 pts/1    00:00:00 -bash
  4. prn      22993 22984  0 Mar07 pts/2    00:00:00 bash
  5. prn      26492 26491  0 12:54 pts/3    00:00:00 -bash
  6. root     26526 26525  0 12:54 pts/3    00:00:00 -bash
  7. root     26614 26526  0 13:06 pts/3    00:00:00 awk /bash/

but that is simply equivalent to the case of "ps -ef | grep bash" so I'm not clear on what you mean there.

Paul

Reply
prn's Avatar
prn
Expert
200 Posts
April 7th, 2008
05:48 PM
#6

Re: using shell script
Quote:
Originally Posted by madhu3437
Thaks very much Paul
one more thing. Below shell script i create all servers are running or not
but i wrote like
i create a file, name of the file is =input_file(in that file put the all servers name)
and run the scripts
getting out put like

Sorry, Madhu. I am having trouble figuring out what you mean. Can you rephrase your question? And please use "code" tags around your code. I'm not clear on what you're getting, what you want and why. Let's try to get one point at a time. It looks like you're trying to ask several questions together and I'm not understanding which is what.

Paul

Reply
ghostdog74's Avatar
ghostdog74
Expert
496 Posts
April 8th, 2008
01:26 AM
#7

Re: using shell script
Quote:
Originally Posted by prn
but that is simply equivalent to the case of "ps -ef | grep bash" so I'm not clear on what you mean there.
Paul

sorry, i did not finish the code...
here's what i meant
Code: ( text )
  1. # ps -ef | awk '/bash/ && !/awk/'

you don't have to call another grep process just to remove the extra "grep". however, it doesn't matter really..some people have gotten used to grep -v grep. :)

Reply
prn's Avatar
prn
Expert
200 Posts
April 8th, 2008
10:31 AM
#8

Re: using shell script
Ah! I see now. Yes, that is a bit more elegant. :)

Paul

Reply
madhu3437's Avatar
madhu3437
Newbie
12 Posts
May 13th, 2008
11:52 AM
#9

Re: using shell script
Quote:
Originally Posted by prn
Ah! I see now. Yes, that is a bit more elegant. :)

Paul


we have 40 sites each site haveing webserver setup and application servers.
and also we need to find out the each site webserver and application servers is running or not in a particular machine.

if 40 sites are running or not running show in the html page.

These input_file have the 40 sites name


var=`cat input_file`

if [ -f ./out.htm ]
then
rm ./out.htm
fi

for list_of_proc in $var
do
echo $list_of_proc
ps -eaf | grep $list_of_proc >> ./out.htm >&1
done
for list_of_proc in $var
do
flag=`grep "NETXcppServer -name $list_of_proc" out.htm|wc -l`
startTag="<html><head></head><body><h1>"
endTag="</h1></body></html>"
echo "$startTag" "$list_of_proc" "=" "$flag" "$endTag" >> /u/mkulluru/dev/html/abc.htm
done



i am getting out

Republic = 1
TRP = 0
(here 1 mean is running)
(here 0 mean is not running)


but i want put like

Republic = running
TRP = Not running

how can write the in the script , please on this it is very use full to me.

Thanks & Regards
Madhusudhana gupta kulluru

Reply
madhu3437's Avatar
madhu3437
Newbie
12 Posts
May 16th, 2008
09:01 AM
#10

Re: using shell script
i giving full details,

we have machines a,b,c,d
in each machine application servers are running
but each machine have 40 application server are running
result of 40 application servers are running or not shown in the web page.


but i am in z machine
when run the script (http://172.30.98.91:/cgi/health.ksh) in web page
Result will show in the web page .

Resultant in web page


Server a machine b machine c machine d machine
--------------------------------------------------------------------------------------------
trowe running running running running
republic running not running running running
universal not running
. ,
.
. ,
.
.
acs running running running running


it very help full greate becaues i am trouble .please help

Thanks & Regards,
Madhusudhana gutpa kulluru









Quote:
Originally Posted by madhu3437
we have 40 sites each site haveing webserver setup and application servers.
and also we need to find out the each site webserver and application servers is running or not in a particular machine.

if 40 sites are running or not running show in the html page.

These input_file have the 40 sites name


var=`cat input_file`

if [ -f ./out.htm ]
then
rm ./out.htm
fi

for list_of_proc in $var
do
echo $list_of_proc
ps -eaf | grep $list_of_proc >> ./out.htm >&1
done
for list_of_proc in $var
do
flag=`grep "NETXcppServer -name $list_of_proc" out.htm|wc -l`
startTag="<html><head></head><body><h1>"
endTag="</h1></body></html>"
echo "$startTag" "$list_of_proc" "=" "$flag" "$endTag" >> /u/mkulluru/dev/html/abc.htm
done



i am getting out

Republic = 1
TRP = 0
(here 1 mean is running)
(here 0 mean is not running)


but i want put like

Republic = running
TRP = Not running

how can write the in the script , please on this it is very use full to me.

Thanks & Regards
Madhusudhana gupta kulluru

Reply
Reply
Not the answer you were looking for? Post your question . . .
170,099 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Linux / Unix / BSD Forum Contributors