Reputation: 6939
I am trying to build a simple daemon script with PHP and I would like to set the process title to see it in ps
and top
output. This code is:
#!/usr/bin/php
<?php
// Daemonize
cli_set_process_title('daemonized');
$pid = pcntl_fork(); // parent gets the child PID, child gets 0
if($pid){ // 0 is false in PHP
// Only the parent will know the PID. Kids aren't self-aware
// Parent says goodbye!
print "Parent : " . getmypid() . " exiting\n";
exit();
}
print "Child : " . getmypid() . "\n";
while (true) {
// daemon stuff...
sleep(2);
}
But I cannot set the title, when I launch the script I get the following warning:
$ ./daemonize.php
PHP Warning: cli_set_process_title(): cli_set_process_title had an error: Not initialized correctly in /Applications/MAMP/htdocs/daemonize.php on line 3
Warning: cli_set_process_title(): cli_set_process_title had an error: Not initialized correctly in /Applications/MAMP/htdocs/daemonize.php on line 3
Where is the issue, what should I do in order to make it work?
Thanks for the attention!
Upvotes: 1
Views: 1826
Reputation: 293
This is not really an answer, per se, but I am not sure that you can still do this with cli-php in OS X 10.10.x without running the process as the superuser or other not-so-secure hacks.
I have tried chmod
ing the process (or more exactly the file that is being executed via php file.php
), chown
ing the GID to another group and also running the script using sudo
. All of these options failed for me even though the php process was being run by an authorized user who belonged to the admin group.
My guess: Apple must be blocking this functionality for security reasons... and I suppose I can imagine why.
In the end, because I was targeting a Linux runtime, I refactored my code so that cli_set_process_title()
was not mission critical and ran the command with the error suppression operator, @cli_set_proc_title()
and just logged the fact that the command failed.
I would welcome more complete answers or other secure work arounds but after much wasted time this was the best I could come up with and I was unable to find any other answers.
Hope this helps.
Upvotes: 4