last day (15 days later) » 

13:46
0
Q: PHP CLI cross-platform ctrl-C

TuckbrosI am trying to rewrite a python script in php. The python script is handling the KeyboardInterrupt exception this way : except KeyboardInterrupt: pass I was starting with this, but can't figure out what to put for ??? } catch (Exception $e) { if ($e->getCode() == ??? ) { //I have n...

If you do a print_r or var_dump of $e you should see what the code is. The codes that may be returned depend on where the exception is being thrown from (PDO etc).
the whole code is embedded in the try block, it can happen anywhere. Maybe ̀exception` is not the right approach and signals or events should be handled instead (if better...)
Then it isn't clear what you are trying to catch. Normally a try/catch block is used around a very specific "thing" that you want to make sure either works or doesn't.
The thing I want to catch is the same as the one catched by the python KeyboardInterrupt
Then please edit your code to include a Minimal, Complete, and Verifiable example, as explained in stackoverflow.com's help center so others may better assist. What you have in the question now isn't something anyone could replicate and test with.
13:46
I don't know how to make it more understandable than how to translate the a python except KeyboardInterrupt: pass statement in php.
What is except KeyboardInterrupt: pass in Python? You've tagged this for PHP but assume everyone knows what that statement does. What is the code in the try portion of the block?
thanks for thet help :)
you can find the python script I am translating in php here : github.com/ttyridal/phpdlna/blob/master/tools/announce.py
it is not mine but I don't like the idea of having part of the application in php and another part in another language
Well that is a very common practice to be sure .... so is that line just looking for a Ctrl-C ??
I guess it can deal with any keyboard interruption (ie user input), but anyway ctrl-something
I am already having some hard time with all the socket things...
I am pretty sure there should be a trivial way of getting this... but couldn't find it
There isn't going to be, if PHP is in a loop using Ctrl-C does not trigger a catch or throw any type of error that could be caught. And you can't really use something like readline cause that would pause PHP waiting for input.
13:58
I also looked around ignore_user_abort function but could find nothing
Based on a description of what that Python is doing it looks like you really want to be looking for a threaded PHP solution. Or just use it in conjunction with PHP as its readme mentions :)
as "threaded", this script is intended to run in background to "announce" the upnp server
Ah then you probably want to run it as a service that starts automatically in which case you wouldn't need to worry about interrupts as there would be no "user" or command window
I think it is not enough cross-platform, I am going to start it with popen from the php main app
the user would reach the main page of the app which would start the script.
and the script itself would stop if it can find another instance itself running
well if it is supposed to be running in the background all of the time I would certainly make it standalone and start automatically on boot
14:16
the aim is to be as light as possible, even portable
Then what I said stands. If it is PHP it's portable.
but not I couldn't start it as a service
You can have it as a "service" on any of the OS platforms ... the only difference is what it's called
You can have it as a "service" on any of the OS platforms ... the only difference is what it's called
You can call it anything you want and run it any way you want to, from a command line, as a service, as a cron job, whatever

last day (15 days later) »