简体   繁体   中英

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).

I'm sure PhantomJS is installed correctly because I can execute it from the command line. I'm using it to make a screen capture of a web page ( http://phantomjs.org/screen-capture.html ).

When I run the command from the command line, it works great, using user 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.

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.

I'm calling the command from Groovy, this way:

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

Any thoughts?

Thanks!

As for command execution, we experienced problem in Grails applications concerning pure string notation. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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