By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,705 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,705 IT Pros & Developers. It's quick & easy.

using shell script

P: 13
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.
Apr 4 '08 #1
Share this Question
Share on Google+
9 Replies

prn
Expert 100+
P: 254
prn
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:
Expand|Select|Wrap|Line Numbers
  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
Expand|Select|Wrap|Line Numbers
  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:
Expand|Select|Wrap|Line Numbers
  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:
Expand|Select|Wrap|Line Numbers
  1. chmod 0744 foo.cgi
so that only root can execute it and then include the lines:
Expand|Select|Wrap|Line Numbers
  1. # User privilege specification
  2. root    ALL=(ALL) ALL
  3. www     desdemona=NOPASSWD:/usr/local/apache/share/cgi-bin/foo.cgi
  4.  
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
Apr 4 '08 #2

P: 13
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















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:
Expand|Select|Wrap|Line Numbers
  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
Expand|Select|Wrap|Line Numbers
  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:
Expand|Select|Wrap|Line Numbers
  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:
Expand|Select|Wrap|Line Numbers
  1. chmod 0744 foo.cgi
so that only root can execute it and then include the lines:
Expand|Select|Wrap|Line Numbers
  1. # User privilege specification
  2. root    ALL=(ALL) ALL
  3. www     desdemona=NOPASSWD:/usr/local/apache/share/cgi-bin/foo.cgi
  4.  
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
Apr 5 '08 #3

Expert 100+
P: 511

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
Expand|Select|Wrap|Line Numbers
  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 :)
Expand|Select|Wrap|Line Numbers
  1. ps -ef| awk '/httpd/'
  2.  
Apr 6 '08 #4

prn
Expert 100+
P: 254
prn
don't have to use that many greps, when awk does the job :)
Expand|Select|Wrap|Line Numbers
  1. ps -ef| awk '/httpd/'
  2.  
Eh?

Expand|Select|Wrap|Line Numbers
  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:
Expand|Select|Wrap|Line Numbers
  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/
  8.  
but that is simply equivalent to the case of "ps -ef | grep bash" so I'm not clear on what you mean there.

Paul
Apr 7 '08 #5

prn
Expert 100+
P: 254
prn
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
Apr 7 '08 #6

Expert 100+
P: 511
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
Expand|Select|Wrap|Line Numbers
  1. # ps -ef | awk '/bash/ && !/awk/'
  2.  
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. :)
Apr 8 '08 #7

prn
Expert 100+
P: 254
prn
Ah! I see now. Yes, that is a bit more elegant. :)

Paul
Apr 8 '08 #8

P: 13
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
May 13 '08 #9

P: 13
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









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
May 16 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.