【问题标题】:Run gulp "npm start" from a php document?从 php 文档运行 gulp“npm start”?
【发布时间】:2023-03-25 16:16:02
【问题描述】:

我正在尝试运行一个 shell 命令来编译 gulp。

我经常跑:

cd
cd /var/www/html/
npm uninstall gulp --save
npm install gulp --save

npm start

gulp 文件编译良好。

我需要知道是否有编译 gulp 文件的方法(本质上是从 php 文档运行 npm start 命令。

$output = shell_exec('./compileNpmStart.sh');

// display $output
echo $output;

这似乎可以编译但是:

编译后我得到一个日志文件而不是正确编译。

0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'start' ]
2 info using npm@4.1.2
3 info using node@v7.5.0
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle base-prep@1.0.0~prestart: base-prep@1.0.0
6 warn lifecycle npm is using /usr/bin/nodejs but there is no node binary in the current PATH. Use the `--scripts-prepend-node-path` option to include the path for the node binary npm was executed with.
7 silly lifecycle base-prep@1.0.0~prestart: no script for prestart, continuing
8 info lifecycle base-prep@1.0.0~start: base-prep@1.0.0
9 verbose lifecycle base-prep@1.0.0~start: unsafe-perm in lifecycle true
10 verbose lifecycle base-prep@1.0.0~start: PATH: /usr/lib/node_modules/npm/bin/node-gyp-bin:/var/www/html/node_modules/.bin
11 verbose lifecycle base-prep@1.0.0~start: CWD: /var/www/html
12 silly lifecycle base-prep@1.0.0~start: Args: [ '-c', 'gulp serve' ]
13 info lifecycle base-prep@1.0.0~start: Failed to exec start script
14 verbose stack Error: base-prep@1.0.0 start: `gulp serve`
14 verbose stack spawn sh ENOENT
14 verbose stack     at exports._errnoException (util.js:1023:11)
14 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
14 verbose stack     at onErrorNT (internal/child_process.js:359:16)
14 verbose stack     at _combinedTickCallback (internal/process/next_tick.js:74:11)
14 verbose stack     at process._tickCallback (internal/process/next_tick.js:98:9)
15 verbose pkgid base-prep@1.0.0
16 verbose cwd /var/www/html
17 error Linux 4.8.6-x86_64-linode78
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "start"
19 error node v7.5.0
20 error npm  v4.1.2
21 error file sh
22 error path sh
23 error code ELIFECYCLE
24 error errno ENOENT
25 error syscall spawn sh
26 error base-prep@1.0.0 start: `gulp serve`
26 error spawn sh ENOENT
27 error Failed at the base-prep@1.0.0 start script 'gulp serve'.
27 error Make sure you have the latest version of node.js and npm installed.
27 error If you do, this is most likely a problem with the base-prep package,
27 error not with npm itself.
27 error Tell the author that this fails on your system:
27 error     gulp serve
27 error You can get information on how to open an issue for this project with:
27 error     npm bugs base-prep
27 error Or if that isn't available, you can get their info via:
27 error     npm owner ls base-prep
27 error There is likely additional logging output above.
28 verbose exit [ 1, true ]

【问题讨论】:

  • 查找 shell_exec
  • 我尝试了 shell_exec、System()、exec() 和其他一些。它们似乎都不起作用。我认为可能存在某种权限错误。尝试安装 webconsole.php 并从基于 Web 的 web-console.org 终端运行命令。出现权限错误。
  • 你试过用 SUID 创建一个 shell 脚本并执行它吗?
  • 我没有。我现在试试看它是否适合我。
  • 无法正常工作

标签: php node.js shell npm gulp


【解决方案1】:

首先将要运行的脚本保存在文件中,例如compile.sh。给脚本正确的运行权限,例如chmod 755 compile.sh.

然后,在您的 PHP 脚本中,添加以下内容:

<?php
// allow script to run for up to 10 minutes
set_time_limit(600); // seconds

// output of script will be saved in $output
$output = shell_exec('./compile.sh');

// display $output
echo $output;

这应该足以让您使用 PHP 运行脚本。

您可能需要引用npm 和任何使用绝对路径的目录。通过命令行运行which npm 以获取要使用的完整路径。通过命令行运行脚本以确保其正常工作。

【讨论】:

  • 试过了。它似乎确实运行了脚本,但对服务器文件没有实际影响。
  • 具有 sh 脚本的 compile.sh 文件的文件权限为 777
  • 通过命令行手动运行./compile.sh,并确保脚本本身没有错误。您可能还需要将npm 引用为绝对路径。
  • 似乎工作正常。虽然我确实注意到了——这很正常——编译过程大约需要 8 分钟。我知道运行脚本的 php 不会显示谎言进度,但是运行 command.sh 的后台 php 是否有一个服务器端 cuttof,它可以运行 shell 进程多长时间?
  • 您可以通过在代码中添加以下行来修改允许脚本运行的时间:set_time_limit ( 600 )(10 分钟限制)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-25
  • 2015-09-08
  • 1970-01-01
  • 2016-08-05
  • 2021-01-28
  • 2019-11-28
相关资源
最近更新 更多