通过exec()从php运行服务器端js [英] Run server-side js from php through exec()

查看:447
本文介绍了通过exec()从php运行服务器端js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Apache/PHP上运行的网站,出于性能方面的考虑,我编写了JavaScript来执行特定任务.

I have a site running on Apache/PHP, and as a matter of performance, I wrote a javascript to do a specific task.

我已在服务器上安装了node.js,以便运行此javascript.当我从命令行调用脚本时,它可以正常工作.参见下面的命令:

I have installed node.js on server, in order to run this javascript. When I call the script from the command line, it works fine. See the command below:

> node myscript.js

但是我需要它从php页面运行,而我试图通过调用exec()PHP函数来做到这一点,就像这样:

But I need it to run from a php page, and I am trying to do this by calling the exec() PHP function, like this:

<?php exec('node myscript.js >/dev/null/ 2>&1 &'); ?>

...但是它不起作用.

...but it's not working.

我做错什么了吗?还有另一种方法可以做我想做的事吗?

Am I doing something wrong? Is there another way to do what I want?

推荐答案

我找到了一种使其工作的方法!我只是在exec()调用中写了安装node.js的完整目录.就这么简单:

I found a way to make it work! I just wrote the full directory where node.js is installed in the exec() call. Simple as that:

<?php exec('/home/bin/node myscript.js >/dev/null/ 2>&1 &'); ?>

这篇关于通过exec()从php运行服务器端js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆