如何使用php运行ssh2_exec来运行nodejs proccess

时间:2016-03-07 20:38:52

标签: php node.js

我正在制作一个应该通过SSH2运行的PHP脚本。

我目前的代码:

<?php
$connection = ssh2_connect('localhost', 22);
    ssh2_auth_password($connection, 'root', '******');


$stream = ssh2_exec($connection, "killall nodejs");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);




$stream = ssh2_exec($connection, "cd /var/www/a/ && nohup nodejs chat.js &");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);

// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);

// Whichever of the two below commands is listed first will receive its appropriate output.  The second command receives nothing
echo "Output: <b>" . stream_get_contents($stream);
echo "</b><hr /><br />Error: <b>" . stream_get_contents($errorStream).'</b><br /> <hr/>';

// Close the streams        
fclose($errorStream);
fclose($stream);

我不知道为什么,但它不起作用。它只是不对chat.js文件做任何事情。

0 个答案:

没有答案