Help - Search - Members - Calendar
Full Version: Php Detirmine If Program Is Running
Linuxhelp > Support > Technical Support
gieltje
Hi all,

I had a php script for kernel 2.4 that printed up or down if a program was or was not running;

<?php
function checkk($daemon,$name) {
$ps ="ps ax | grep $daemon | wc -l";
$origps = exec($ps);
$minone = $origps-3;
if ($minone<1) {
$dataps = "<b><font color=red>DOWN</font></b>";
}
if ($minone==1) {
$dataps = "<b><font color=blue>Up</font></b>";
}
if ($minone>1) {
$dataps = "<b><font color=blue>Up</font></b>";
}
return $dataps;
}
echo checkk("q3ded","Q3ded server");
?>

But in kernel 2.6 it doesnt work, it just ad randum displays up or down wheter it is running or not.

Who has/can make a working script for 2.6?

Regards,

Michiel
hughesjr
What this script does is figure out how many processes are running that match the $daemon name .... (the number of lines) ...

Then for some reason it subtracts 3 from that number and if the result is < 1, it says down ... otherwise it says up.

(one result is going to be the grep command itself, that means that the logic is 3 other processes are named the $daemon 's name for everything that runs as a deamon).

It would seem to me that this logic is bogus reguardless of the kernel ... I have a redhat system with sendmail running ... and the command:

ps ax | grep sendmail | wc -l

equals 3

so :

3 - 3 = 0

which means sendmail is down ... but it is up.

What is your distro of linux?
gieltje
Debian Sid
Hemant
Yeah..there are two problems..in the code
1.Basically when you check for a service that is not running...let's say of name foo.

$ ps ax|grep foo|wc -l


this will return 1..becuase..for a process that has name foo ,there is only one process running..and that is..

grep foo


Another error in the program..is use of grep as we know grep by default searches..for all the matches..that contain..the pattern either as full word or as a substring.
so the output of this command will be

$ ps ax|grep init
...................init
..................xinit<all the xinit>
..................kdeinit<all the kdeinit processes>
..................grep init<just entered command>


but you intended to search for init only,.so only two line should be printed.With these modifications..your code will be....
CODE
<?php
function checkk($daemon,$name) {
$ps ="ps ax | grep -w $daemon | wc -l";
$origps = exec($ps);
$minone = $origps;
if ($minone>1) {
$dataps = "<b><font color=blue>Up</font></b>";
}
else
{
$dataps = "<b><font color=red>DOWN</font></b>";
}
return $dataps;
}
echo checkk("q3ded","Q3ded server");
?>

this should work...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.