0

I want to run a php check for some specific running processes (I know their exe name, they are my little C++ programs and there can be more of them), detect which are not answering/responding and kill them. (PHP5.4+, Windows)

What I already know

1.To get list of processes I'm using win32ps extension => win32_ps_list_procs()
=> that gives me easy access to PID and EXE name
2.Killing the process (I'm not sure if this is the best solution on Windows, please tell me if there's a better way...) :

system('taskkill /f /pid '.$pid.' /im '.$exe_name);
//forces to kill process with given pid and image name mask

What I don't know

Is how to detect if process is not responding or not. This check is rather scheduled - so I have no problem with e.g. saving some current state data and comparing them with state data collected in the next run of this check.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
jave.web
  • 13,880
  • 12
  • 91
  • 125
  • I assume you have a specific type of processes you want to monitor? Certainly you do not want to "blindly" kill everything your script does not understand? If this is to monitor specific processes, then you should answer that question in a specific manner. That obviously depends on the type of processes you want to monitor, which you did not tell us... – arkascha Jul 17 '15 at 06:20
  • What are "C++ exes"? compiler runs? or executables implemented in c++? The first certainly won't ever get locked up, the second is nothing "specific", it is just a normal binary executable. – arkascha Jul 17 '15 at 06:23
  • There is nothing specific about a program that is implemented in c++. What you get is a normal binary. It is (nearly) impossible to say if a binary has been implemented in c++. But if those are _your_ programs, then maybe you can modify them? If so, just implement a "heartbeat" service where your monitoring script can check if the process is still alive. If you cannot modify the programs, then again this depends on _what_ programs... – arkascha Jul 17 '15 at 06:25
  • Things are getting a bit clearer. But I think I answered to that in my last comment... you have to answer yourself this question: what do you mean by _programs which are not answering/responding_? If you have the answer to that, then you have your answer to this question. We obviously cannot answer that question for the programs you implemented. Only you can. – arkascha Jul 17 '15 at 06:26
  • Logfiles are an approach. Very robust, but also very slow and expensive. What about listening on a socket instead and answering to "ping" requests to that socket? Much faster, less expensive and no annoying files generated you have to take care of later. – arkascha Jul 17 '15 at 06:35
  • About the PIDs: each new process gets a new PID, counted upwards. That works for a very long time. However after a very long period the system runs out of numbers and will "recycle" PIDs, only those not used any more. However I dare say that since you use MS-Windows you will never have your system running long enough without booting to hit that boundary. Such system are simply not designed to keep running for years like unix or linux systems are... – arkascha Jul 17 '15 at 06:37
  • I don't see a connection to a database here, what do you want to use it for? – arkascha Jul 17 '15 at 06:38
  • About my suggestion? What more can I say? Open a socket in your c++ program and listen to that. For every incoming request on that socket your program has to react and send out a reply. That way you can monitor your programs from "outside". The php monitor script can send requests to all known programs and react on the fact if it gets a reply or not. That's all. – arkascha Jul 17 '15 at 06:39
  • Well, 2 to the power of 64 is a pretty big number, a pretty lot of processes you have to start :-) Though indeed I am not certain if there really is a 64bit MS-Windows system already. I mean one that really uses 64bit internally, not just by the name like early 64bit MS-Windows systems did in a few locations, whilst the rest was still left in 32bit architecture. But you are indeed correct that thinking of robustness is a good thing! – arkascha Jul 17 '15 at 06:41
  • 1
    Ah I get you - you ment like in PHP going through filtered PIDs and sending them "questions" through sockets? - I think I get your solution in theory, but I haven't done anything like that in many years, could you provide an example or at least a link please? – jave.web Jul 17 '15 at 06:42
  • There are endless examples on the internet for such thing. It is a simple socket listener, a standard pattern in programming. I am certain you will easily find lots of examples. – arkascha Jul 17 '15 at 06:43
  • I can't find any suitable - especially not on how to send request from PHP using PID .... sry :/ BTW: you could just make it an answer :P – jave.web Jul 17 '15 at 06:48
  • No, that is not possible like that. You cannot somehow address the socket of a local process just like that. This has nothing to do with php, it is generally not possible. – arkascha Jul 17 '15 at 06:53
  • Alternatives: 1. use named sockets in the local file system (do such exist on MS-Windows? I do not know). In that case the endpoint of the socket is a node in the file system, so the php process would simply read and write that file. Works. 2. you need some sort of scheduler, to a "father" process where the started program registers and keeps a socket open. That central father process can act as a relay then. Or you bind your programs socket to a port on your system and note that port number down in a lock file per process. Then php can simply address that network socket. – arkascha Jul 17 '15 at 06:54
  • If in doubt go with the lock file approach, it is easy and works: each started program creates an own "lock file". That file is called like the programs PID and located in a general folder where all such lock files for programs are kept that should be monitored. The file itself is created as a file based named socket. So whatever is written to the file by php is forwarded by the OS to the program that created the socket. And vice versa. That way php knows exactly how to "talk" to each program. – arkascha Jul 17 '15 at 06:57
  • Yea I wanted to avoid using network attitude, but thanks anyway :) Would you mind deleting not-so-important comments or make a summary of them ? – jave.web Jul 17 '15 at 06:57
  • Named sockets in the local file system are not networking. They are file system based. – arkascha Jul 17 '15 at 06:58
  • I know, I was reffering the network sockets – jave.web Jul 17 '15 at 06:58
  • Also, there is nothing bad about networking. You can bind it to the loopback device, so that it is not exposed outside the system. Also typically the OS takes a shortcut for the loopback device so that no networking overhead occurs. But indeed, the file system based sockets are more efficient, especially since you do not need an additional registry style thing. You can use a general folder where all sockets are kept in whilst in use. – arkascha Jul 17 '15 at 07:01
  • I don't like an idea of randomly hitting already occupied port - but I think there should be way of getting free port automaticly, shouldnt it? Anyway, do you have a clue where would Windows (8) store these socket files? – jave.web Jul 17 '15 at 07:06
  • Your attempt to bind to a port already in use would fail, so it is easy to detect ports already in use. you just try in a loop incrementing the port number until you succeed. The system will store file system based sockets wherever you create them. It is like creating a file, you specify a path. It is just another type of file system node you create. So you can define a specific folder for that, like /var/spool/monitor/ or similar. – arkascha Jul 17 '15 at 07:09
  • Then I really don't see a difference between this and custom log files ? O.o – jave.web Jul 17 '15 at 07:22
  • 1
    A log file would have to be opened each time, read, the seek pointer placed close to the end and the content would have to be parsed and interpreted. That is a huge overhead compared to just keeping a socket open, sending 5 bytes in and receiving 5 bytes back or not every minute or so! – arkascha Jul 17 '15 at 07:30

0 Answers0