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:
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
- 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:
- if [ `ps -ef | grep -v grep | grep -c httpd` -gt 0 ] ; then
-
echo "<P>apache is running</P>"
-
exit
-
else
-
echo "<P>apache is not running</P>"
-
exit
-
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:
so that only root can execute it and then include the lines:
- # User privilege specification
-
root ALL=(ALL) ALL
-
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