无法使用cli_set_process_title()OS X在PHP命令行脚本中设置进程标题? [英] Cannot set process title in a PHP command line script using cli_set_process_title() OS X?

查看:64
本文介绍了无法使用cli_set_process_title()OS X在PHP命令行脚本中设置进程标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP构建一个简单的守护程序脚本,并且我想设置进程标题以在 ps top 输出。这段代码是:

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?

感谢您的关注!

推荐答案

这并不是一个真正的答案,本身,但我不确定您是否仍可以在OS X 10.10.x中使用cli-php来执行此操作,而无需以超级用户或其他不太安全的黑客方式运行进程。

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.

我已经尝试过 chmod 处理过程(或更确切地说是通过 php执行的文件) file.php ), chown 将GID分配到另一个组,并使用 sudo 。即使这些php进程是由属于管理组的授权用户运行的,所有这些选项对我来说都是失败的。

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.

我的猜测:Apple必须阻止此功能出于安全原因...我想我可以想象为什么。

My guess: Apple must be blocking this functionality for security reasons... and I suppose I can imagine why.

最后,因为我的目标是Linux运行时,所以我重构了代码,使得 cli_set_process_title()不是关键任务,并使用错误抑制运算符 @cli_set_proc_title()运行命令,并记录了以下事实:该命令失败了。

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.

希望这会有所帮助。

这篇关于无法使用cli_set_process_title()OS X在PHP命令行脚本中设置进程标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆