473,246 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Check if a certain service is running on the server

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
12 12698
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: aurora | last post by:
I am looking for a way for reloading updated modules in a long running server. I'm not too concerned about cascaded reload or objects already created. Just need to reload module xxx if the...
3
by: Andrew Weaver | last post by:
How do you change the port in which CDONTS will look for Microsoft SMTP server? I have Imail running on port 25, and I have SMTP service running on port 26. Any help you can provide me would be...
5
by: Iwan Petrow | last post by:
Hi, I have Win application -> Web service -> Sql Server. The user input date and time in format dd/MM/yy hh:mm. I convert it to datetime and send it to a web service which save it to the db. But...
1
by: Stephen Corey | last post by:
I've got a windows service running as NetworkService on a WinXP Pro machine. Is there a way for it to find the Active Directory account of the logged on user? I don't mind switching the service to...
3
by: jliusolar | last post by:
Hi I am trying to figure out why an application get this error when I am trying to open the application's asmx file from localhost. I don't have indexing service running(it set as manual and not...
0
by: Jennyfer J Barco | last post by:
Hello, I have a .NET service running and sometimes, not very often I receive this error "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not...
0
by: Computer geek | last post by:
I am a beginner programmer and would like a little help with a piece of code. Does anyone know how write code that will check the status of a service running on a remote server? Or is it even...
2
by: Bill Fuller | last post by:
I am trying to attach to a WCF Web Service running in virtual directory in my local dev box, but not sure how to do it. I tried setting a breakpoint on the .svc and attaching to the Inetinfo...
0
by: Gordon | last post by:
On Oct 23, 9:43 am, Curtis <dye...@gmail.comwrote: if ($handle = fsockopen ('localhost', 80, $errnum, $errmsg, 1)) { echo ('A web server is running on this system<br>'); fclose ($handle); }...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.