从PHP脚本启动FOREVER或PM2作为WWW-DATA

Dam*_*ian 19 php linux ubuntu node.js

我有一个nodejs名为的脚本script.js.

var util = require('util'); 
var net = require("net"); 

process.on("uncaughtException", function(e) {
console.log(e);
});

var proxyPort = "40000"; 
var serviceHost = "1.2.3.4"; 
var servicePort = "50000"; 

net.createServer(function (proxySocket) {
    var connected = false;
    var buffers = new Array();
    var serviceSocket = new net.Socket();
    serviceSocket.connect(parseInt(servicePort), serviceHost);
    serviceSocket.pipe(proxySocket).pipe(serviceSocket);
    proxySocket.on("error", function (e) {
        serviceSocket.end();
    });
    serviceSocket.on("error", function (e) {
        console.log("Could not connect to service at host "
            + serviceHost + ', port ' + servicePort);
        proxySocket.end();
    });
    proxySocket.on("close", function(had_error) {
        serviceSocket.end();
    });
    serviceSocket.on("close", function(had_error) {
        proxySocket.end();
    });
}).listen(proxyPort);
Run Code Online (Sandbox Code Playgroud)

我正常运行它nodejs script.js,但现在我想包括foreverpm2功能.当我root一切顺利时:

chmod -R 777 /home/nodejs/forever/;
-- give rights

watch -n 0.1 'ps ax | grep forever | grep -v grep'
-- watch forwarders (where i see if a forever is opened)

/usr/local/bin/forever -d -v --pidFile "/home/nodejs/forever/file.pid" --uid 'file' -p '/home/nodejs/forever/' -l '/home/nodejs/forever/file.log' -o '/home/nodejs/forever/file.log' -e '/home/nodejs/forever/file.log' -a start /etc/dynamic_ip/nodejs/proxy.js 41789 1.2.3.4:44481 414 file
-- open with forever

forever list
-- it is there, i can see it

forever stopall
-- kill them all
Run Code Online (Sandbox Code Playgroud)

问题是当我想从PHP带有system或者exec函数的脚本运行脚本时:

sudo -u www-data /usr/local/bin/forever -d -v --pidFile "/home/nodejs/forever/file.pid" --uid 'file' -p '/home/nodejs/forever/' -l '/home/nodejs/forever/file.log' -o '/home/nodejs/forever/file.log' -e '/home/nodejs/forever/file.log' -a start /etc/dynamic_ip/nodejs/proxy.js 41789 1.2.3.4:44481 414 file
-- open as www-data (or i can do this just by accessing `http://1.2.3.4/test.php`, it is the same thing)

forever list
-- see if it is there, and it is not (i see it in watch)

forever stopall
-- says no forever is opened

kill PID_ID
-- the only way is to kill it by pid ... and on another server all of this works very well, can create and kill forevers from a php script when accessing it from web ... not know why
-- everything is in /etc/sudoers including /usr/local/bin/forever 
Run Code Online (Sandbox Code Playgroud)

这是为什么?我怎么解决这个问题?

我也做了一些技巧,创建了一个用户'forever2',我script.sh用这个内容创建了一个:

sudo su forever2 user123; /usr/local/bin/forever -d -v --pidFile "/home/nodejs/forever/file.pid" --uid 'file' -p '/home/nodejs/forever/' -l '/home/nodejs/forever/file.log' -o '/home/nodejs/forever/file.log' -e '/home/nodejs/forever/file.log' -a start /etc/dynamic_ip/nodejs/proxy.js 41789 1.2.3.4:44481 414 file;
Run Code Online (Sandbox Code Playgroud)

哪里user123不存在,只是退出shell执行后的技巧.该脚本,运行forever,我可以关闭所有forevers用命令forever stopallroot.当我尝试运行相同的东西http://1.2.3.4/test.php或作为www-data用户我不能从root或关闭它www-data,所以甚至这不起作用.

我从试过Ubuntu 14.04.3 LTS,Ubuntu 14.04 LTS,Debian GNU/Linux 8...还是同样的事情.

任何想法?

谢谢.

edo*_*ceo 1

如果您从 Apache 或 Web 服务器中启动该过程,那么您已经是用户www-data,因此sudo su可能没有必要对您已有的用户上下文执行 a 操作。

当您开始此forever任务时,您可能还需要关闭终端/输入并直接发送到后台。像这样的东西:

// Assemble command
$cmd = '/usr/bin/forever';
$cmd.= ' -d -v --pidfile /tmp/my.pid'; // add other options
$cmd.= ' start';
$cmd.= ' /etc/dynamic_ip/nodejs/proxy.js';
// "magic" to get details
$cmd.= ' 2>&1 1>/tmp/output.log'; // Route STDERR to STDOUT; STDOUT to file
$cmd.= ' &'; // Send whole task to background.
system($cmd);
Run Code Online (Sandbox Code Playgroud)

现在,这里不会有任何输出,但您应该有一些内容/tmp/output.log可以显示forever失败的原因,或者脚本崩溃的原因。

如果您有时以 root 身份运行脚本,然后尝试与 www-data 相同的命令,您也可能会遇到以 root 身份执行时创建的一个或多个文件/目录的权限,这些权限现在在以 www- 身份运行时发生冲突数据。