简体   繁体   English

如何从Tomcat Web应用程序运行PhantomJS

[英]How to run PhantomJS from Tomcat web app

I'm trying to run PhantomJS from a Grails application running on a Tomcat 7 instance on Linux (Ubuntu 13.04). 我正在尝试从在Linux(Ubuntu 13.04)上的Tomcat 7实例上运行的Grails应用程序中运行PhantomJS。

I'm sure PhantomJS is installed correctly because I can execute it from the command line. 我确定PhantomJS已正确安装,因为我可以从命令行执行它。 I'm using it to make a screen capture of a web page ( http://phantomjs.org/screen-capture.html ). 我正在使用它来制作网页的屏幕截图( http://phantomjs.org/screen-capture.html )。

When I run the command from the command line, it works great, using user root: 当我从命令行运行命令时,使用root用户可以很好地工作:

phantomjs /home/user/captureScreen.js "http://xx.xx.xx.xx/chart" "/home/user/07012014050636114.png"

I believe there is no need to share the JS code since it's working fine from the command line. 我相信没有必要共享JS代码,因为它可以从命令行正常运行。

The problem is that when I run the same command from the web-app (Grails), it just returns with a 0 value, which tells me everything ran fine, but it's not creating the PNG file neither returning an error. 问题是,当我从Web应用程序(Grails)运行相同的命令时,它仅返回0值,这告诉我一切运行正常,但它没有创建PNG文件,也没有返回错误。

I'm calling the command from Groovy, this way: 我是这样从Groovy调用命令的:

String path = "phantomjs pathToJs.... etc"
def process = path.execute()
process.waitForOrKil(5000) // This runs in 1 to 2 seconds in the command line
println process.exitValue()

I tried adding write permissions to the tomcat7 user to the folder where the JS file and where the image is written but still it didn't work but I still believe it's something related to permissions 我尝试将tomcat7用户的写许可权添加到JS文件和写图像的文件夹中,但仍然无法正常工作,但我仍然认为这与许可权有关

Any thoughts? 有什么想法吗?

Thanks! 谢谢!

As for command execution, we experienced problem in Grails applications concerning pure string notation. 至于命令执行,我们在Grails应用程序中遇到了有关纯字符串表示法的问题。 Using the array syntax 使用数组语法

   ["/usr/binphantom/js","/home/user/captureScreen.js","http://xx.xx.xx.xx/chart","‌​/home/user/07012014050636114.png"].execute()

made the thing work. 使事情起作用。 I think it has to do with shell expansion which differs on how strings are handled in Groovy. 我认为这与外壳扩展有关,外壳扩展在Groovy中如何处理字符串不同。

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

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