Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending signals to process via php

Tags:

shell

php

I am trying to send a kill -10 (SIGUSER1) to a process ID by using a button in a webpage, I have tried different ways to send the signal while passing the PHP variable (which holds the PID) to shell. Below is what I have:

$next = posix_kill($pid3, 10);

Right now, this is giving me the below error:

PHP Warning:  posix_kill() expects parameter 1 to be long, 
              string given in /var/www/parse2.php on line 15

Please advise

like image 652
xxethanixx Avatar asked Dec 15 '22 15:12

xxethanixx


2 Answers

Just try this:

$next = posix_kill((long)$pid3, 10);

Intval returns an int so the warning won't go away, but it will probably work

So since you seem to have a problem still better would be like suggested elswhere:

$next = posix_kill(intval($pid3), 10);

If that doesn't work I'd suggest to show us the php version you use.

Unless you have a space that comes with it, then try:

$next = posix_kill(intval(trim($pid3)), 10);

update:

So now you got it working, you need to trap the signal and make it do something, you need to attach a callback function, since I don't know the rest of your code. You need something like this :

pcntl_signal(SIGUSR1, "sig_handler");

Then do something on that signal:

function sig_handler($sig) {

   switch($sig) {
      case SIGUSR1:
         // logtrace(1,"received USR1 signal.");
         break;
   ...

I just think about 1 thing, I assume that the process we are sending a signal is a PHP script. Otherwise I think you got it working but isn't doing a whole lot (yet)

like image 97
Glenn Plas Avatar answered Dec 30 '22 23:12

Glenn Plas


most php functions will try to coerce their arguments into the appropriate data type if its possible. its extremely common for php functions to automatically convert strings into numeric types like int or float, if possible.

But, some just aren't written that way, and you need to supply the data type specified in the manual.

"long" is a numeric type, so just cast to integer. This assumes $pid3 is a numeric string, so that the cast results in something sensible.

$next = posix_kill((int) $pid3, 10);
like image 27
goat Avatar answered Dec 30 '22 22:12

goat