我正在制作一个应该通过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
文件做任何事情。