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

Check if a certain service is running on the server

P: n/a
Hi.

I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
Oct 22 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Shadlan schreef:
Hi.

I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
Hi Shadlan,

If you mean by a service a process: yes.
eg for *nix:
exec("ps -aux");

Have a look at the exec-zoo here:
http://nl3.php.net/manual/en/book.exec.php

If you are on W$ machine, you can pass commandline instructions the same
way.

So once you know WHAT you would do on the commandprompt to get an answer
to your question, you can pass that command to one of the exec function
in PHP and get the result.

http://nl3.php.net/manual/en/function.passthru.php

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Oct 22 '08 #2

P: n/a
On Oct 22, 11:34*am, Shadlan <shad...@arjoc.comwrote:
Hi.

I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
I can not tell if you mean 1. some php service(program) is installed
on your server and if it is enabled, or if 2. some program called for
in a web page is running at the moment to do something required for
the web page.

If you mean 1. there is a very simple php function that will tell you
much about what your php, if installed, will do. A simple one line web
page is all it takes. See how it works on my server at
http://www.cwdjr.net/php/phpInfo.php . All you need to do is to write
a simple .php page that contains "<?php phpinfo(); ?>" (without
quotes) on one line.

If you mean 2., the process program can be written with some value
such as test set to say 0. The very first step in the program for the
process would be to reset test to say 1. The process should end with
setting test back to 0. You can then check if the process is running
or not by examination of the value of test.
Oct 22 '08 #3

P: n/a
On Oct 22, 6:15*pm, cwdjrxyz <spamtr...@cwdjr.infowrote:
On Oct 22, 11:34*am, Shadlan <shad...@arjoc.comwrote:
Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?

I can not tell if you mean 1. some php service(program) is installed
on your server and if it is enabled, or if 2. some program called for
in a web page is running at the moment to do something required for
the web page.

If you mean 1. there is a very simple php function that will tell you
much about what your php, if installed, will do. A simple one line web
page is all it takes. See how it works on my server athttp://www.cwdjr.net/php/phpInfo.php. All you need to do is to write
a simple .php page that contains "<?php phpinfo(); ?>" (without
quotes) on one line.

If you mean 2., the process program can be written with some value
such as test set to say 0. The very first step in the program for the
process would be to reset test to say 1. The process should end with
setting test back to 0. You can then check if the process is running
or not by examination of the value of test.
Unfortunately it's neither.

I have a Windows Box running a Windows Service created by me that
collects data from several industrial machines and stores it in a
database. The same box is running a web page to allow users to run,
stop and check if the service is running.
Oct 23 '08 #4

P: n/a
Shadlan wrote:
On Oct 22, 6:15 pm, cwdjrxyz <spamtr...@cwdjr.infowrote:
>On Oct 22, 11:34 am, Shadlan <shad...@arjoc.comwrote:
>>Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
I can not tell if you mean 1. some php service(program) is installed
on your server and if it is enabled, or if 2. some program called for
in a web page is running at the moment to do something required for
the web page.

If you mean 1. there is a very simple php function that will tell you
much about what your php, if installed, will do. A simple one line web
page is all it takes. See how it works on my server athttp://www.cwdjr.net/php/phpInfo.php. All you need to do is to write
a simple .php page that contains "<?php phpinfo(); ?>" (without
quotes) on one line.

If you mean 2., the process program can be written with some value
such as test set to say 0. The very first step in the program for the
process would be to reset test to say 1. The process should end with
setting test back to 0. You can then check if the process is running
or not by examination of the value of test.

Unfortunately it's neither.

I have a Windows Box running a Windows Service created by me that
collects data from several industrial machines and stores it in a
database. The same box is running a web page to allow users to run,
stop and check if the service is running.
Maybe try:

$shell = `sc query myService`;

I'm not sure which privileges are needed to run this command, if any,
so you might want to research further.

--
Curtis
Oct 23 '08 #5

P: n/a
On Oct 23, 8:51*am, Curtis <dye...@gmail.comwrote:
Shadlan wrote:
On Oct 22, 6:15 pm, cwdjrxyz <spamtr...@cwdjr.infowrote:
On Oct 22, 11:34 am, Shadlan <shad...@arjoc.comwrote:
>Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
I can not tell if you mean 1. some php service(program) is installed
on your server and if it is enabled, or if 2. some program called for
in a web page is running at the moment to do something required for
the web page.
If you mean 1. there is a very simple php function that will tell you
much about what your php, if installed, will do. A simple one line web
page is all it takes. See how it works on my server athttp://www.cwdjr..net/php/phpInfo.php. All you need to do is to write
a simple .php page that contains "<?php phpinfo(); ?>" (without
quotes) on one line.
If you mean 2., the process program can be written with some value
such as test set to say 0. The very first step in the program for the
process would be to reset test to say 1. The process should end with
setting test back to 0. You can then check if the process is running
or not by examination of the value of test.
Unfortunately it's neither.
I have a Windows Box running a Windows Service created by me that
collects data from several industrial machines and stores it in a
database. The same box is running a web page to allow users to run,
stop and check if the service is running.

Maybe try:

$shell = `sc query myService`;

I'm not sure which privileges are needed to run this command, if any,
so you might want to research further.

--
Curtis
That does show me if the service is running, but how do I process the
response?

SERVICE_NAME: myService
TYPE : 10 WIN32_OWN_PROCESS
STATE : 1 STOPPED

(NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0

Is there a way to use sc just to read the state?
Oct 23 '08 #6

P: n/a
Shadlan wrote:
On Oct 23, 8:51 am, Curtis <dye...@gmail.comwrote:
>Shadlan wrote:
>>On Oct 22, 6:15 pm, cwdjrxyz <spamtr...@cwdjr.infowrote:
On Oct 22, 11:34 am, Shadlan <shad...@arjoc.comwrote:
Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
I can not tell if you mean 1. some php service(program) is installed
on your server and if it is enabled, or if 2. some program called for
in a web page is running at the moment to do something required for
the web page.
If you mean 1. there is a very simple php function that will tell you
much about what your php, if installed, will do. A simple one line web
page is all it takes. See how it works on my server athttp://www.cwdjr.net/php/phpInfo.php. All you need to do is to write
a simple .php page that contains "<?php phpinfo(); ?>" (without
quotes) on one line.
If you mean 2., the process program can be written with some value
such as test set to say 0. The very first step in the program for the
process would be to reset test to say 1. The process should end with
setting test back to 0. You can then check if the process is running
or not by examination of the value of test.
Unfortunately it's neither.
I have a Windows Box running a Windows Service created by me that
collects data from several industrial machines and stores it in a
database. The same box is running a web page to allow users to run,
stop and check if the service is running.
Maybe try:

$shell = `sc query myService`;

I'm not sure which privileges are needed to run this command, if any,
so you might want to research further.

--
Curtis

That does show me if the service is running, but how do I process the
response?

SERVICE_NAME: myService
TYPE : 10 WIN32_OWN_PROCESS
STATE : 1 STOPPED

(NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0

Is there a way to use sc just to read the state?
Yes, use regex or string manipulation functions to parse out the exact
data you need from the entire response.

For more information on sc, you'll have to ask google, or find an
appropriate Microsoft NG.

--
Curtis
Oct 23 '08 #7

P: n/a
On Oct 22, 5:34*pm, Shadlan <shad...@arjoc.comwrote:
Hi.

I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
If the service accepts incoming connections then you might be able to
use curl to try and connect to the machine on the port associated with
the service in question. If the service doesn't accept connections
then some other approach would be needed. I've not looked closely at
the responses already made but at first glance they look like they
could work.
Oct 23 '08 #8

P: n/a
Gordon wrote:
On Oct 22, 5:34 pm, Shadlan <shad...@arjoc.comwrote:
>Hi.

I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?

If the service accepts incoming connections then you might be able to
use curl to try and connect to the machine on the port associated with
the service in question. If the service doesn't accept connections
then some other approach would be needed. I've not looked closely at
the responses already made but at first glance they look like they
could work.
Seems like fsockopen() would probably be sufficient.

--
Curtis
Oct 23 '08 #9

P: n/a
On Oct 23, 9:43*am, Curtis <dye...@gmail.comwrote:
Gordon wrote:
On Oct 22, 5:34 pm, Shadlan <shad...@arjoc.comwrote:
Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
If the service accepts incoming connections then you might be able to
use curl to try and connect to the machine on the port associated with
the service in question. *If the service doesn't accept connections
then some other approach would be needed. *I've not looked closely at
the responses already made but at first glance they look like they
could work.

Seems like fsockopen() would probably be sufficient.

--
Curtis
Thanks for all your help. It's working now.

Here's the function that checks if a service is running. It doesn't
include any kind of verification.

function isRunning($serviceName)
{
exec("sc query $serviceName",$output); //$output is an array
containing all the lines from the instruction
output=implode($output); //$output is now a string
return (!strpos($output,'STOPPED'));
}
Oct 23 '08 #10

P: n/a
Shadlan wrote:
On Oct 23, 9:43 am, Curtis <dye...@gmail.comwrote:
>Gordon wrote:
>>On Oct 22, 5:34 pm, Shadlan <shad...@arjoc.comwrote:
Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
If the service accepts incoming connections then you might be able to
use curl to try and connect to the machine on the port associated with
the service in question. If the service doesn't accept connections
then some other approach would be needed. I've not looked closely at
the responses already made but at first glance they look like they
could work.
Seems like fsockopen() would probably be sufficient.

--
Curtis

Thanks for all your help. It's working now.

Here's the function that checks if a service is running. It doesn't
include any kind of verification.

function isRunning($serviceName)
{
exec("sc query $serviceName",$output); //$output is an array
containing all the lines from the instruction
output=implode($output); //$output is now a string
return (!strpos($output,'STOPPED'));
}
Glad to hear it's working; nice solution. :-)

--
Curtis
Oct 23 '08 #11

P: n/a
On Oct 23, 9:43*am, Curtis <dye...@gmail.comwrote:
Gordon wrote:
On Oct 22, 5:34 pm, Shadlan <shad...@arjoc.comwrote:
Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
If the service accepts incoming connections then you might be able to
use curl to try and connect to the machine on the port associated with
the service in question. *If the service doesn't accept connections
then some other approach would be needed. *I've not looked closely at
the responses already made but at first glance they look like they
could work.

Seems like fsockopen() would probably be sufficient.

--
Curtis
Yeah, good point. That just occurred to me a couple of minutes after
making the post.
Oct 23 '08 #12

P: n/a
Shadlan wrote:
On Oct 23, 9:43 am, Curtis <dye...@gmail.comwrote:
>Gordon wrote:
>>On Oct 22, 5:34 pm, Shadlan <shad...@arjoc.comwrote:
Hi.
I need to know if a service is running on my server. Is there any PHP
instruction that I can use to do this?
If the service accepts incoming connections then you might be able to
use curl to try and connect to the machine on the port associated with
the service in question. If the service doesn't accept connections
then some other approach would be needed. I've not looked closely at
the responses already made but at first glance they look like they
could work.
Seems like fsockopen() would probably be sufficient.

--
Curtis

Thanks for all your help. It's working now.

Here's the function that checks if a service is running. It doesn't
include any kind of verification.

function isRunning($serviceName)
{
exec("sc query $serviceName",$output); //$output is an array
containing all the lines from the instruction
output=implode($output); //$output is now a string
return (!strpos($output,'STOPPED'));
}
I just realized a couple improvements:

1.) strpos() will not work if "STOPPED" were to start on index 0. You
should test against FALSE with ===. Easier would be to use
strstr() or stristr() for case insensitivity.

2.) You can eliminate the need for imploding if you use the execution
operator (``). You can also simply store the return value of
exec(), like below.

function isRunning($serviceName)
{
$output = exec("sc query $serviceName");
// the same thing:
// $output = `sc query $serviceName`;

return !stristr($output, 'STOPPED');
}

--
Curtis
$eMail = str_replace('sig.invalid', 'gmail.com', $from);
Oct 30 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.