简体   繁体   English

如何使用exec()在PHP中执行随npm命令安装的命令?

[英]How to exec a command installed with `npm` command in PHP using `exec()`?

In PHP running on Ubuntu, I can run exec('npm -v') and the output is good, but I can't run exec('gitbook xxxx') . 在Ubuntu上运行的PHP中,我可以运行exec('npm -v') ,并且输出很好,但是我不能运行exec('gitbook xxxx')

gitbook is a npm package I installed by gitbook是我安装的npm软件包

npm install gitbook -g

I can run gitbook xxxx in the Ubuntu terminal, how can I run it from my PHP code? 我可以在Ubuntu终端中运行gitbook xxxx ,如何从我的PHP代码运行它?

I tried once like this on UNIX-based OS: 我曾经在基于UNIX的操作系统上尝试过这样的一次:

You can run shell commands via the exec() function: 您可以通过exec()函数运行shell命令:

// make an php file to execute shell script //制作一个php文件来执行shell脚本

exec("node yourscript.js &", $output);

Well output here become array of each line of output along with process id. 好的输出在这里成为每行输出以及进程ID的数组。 You can kill process by processid also. 您也可以通过processid杀死进程。

exec("kill " . $processid);

This how I was did. 这是我做的。 Other then this you can use node supervisor. 除此之外,您可以使用节点管理器。 Hope it will help you. 希望对您有帮助。 Try your also with node command. 也可以使用node命令尝试。

If you run php by nginx or apache (for example, visit url example.com/index.php), sometime you need to export the PATH 如果您是通过nginx或apache运行php(例如,访问url example.com/index.php),则有时需要导出PATH

exec("export PATH=/usr/local/bin && gitbook build);

after I added export PATH, everything works fine. 添加导出路径后,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM