Cannot set process title in a PHP command line script using cli_set_process_title() OS X?

南楼画角 提交于 2019-12-11 10:05:25

问题


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!


回答1:


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 chmoding the process (or more exactly the file that is being executed via php file.php), chowning 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.



来源:https://stackoverflow.com/questions/27803120/cannot-set-process-title-in-a-php-command-line-script-using-cli-set-process-titl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!