Thursday, March 24, 2011

Can I execute a shell script from a web page?

My web page uploads a file. Currently it places that file in a directory which is polled by a cron job. If the file is there, a bash script acts on the file. I hate the fact that I am polling. The upload happens perhaps once a week, but the user wants to see results within minutes from their file, so I end up polling every 5 minutes.

So rather than polling, I'd like to evoke the script once the upload is complete. How can I get my web page to fire up the bash script. The web page is coded with JSP.

From stackoverflow
  • So long as you haven't got a SecurityManager set, you can use Runtime.exec to call an executable program. In particular you can run /bin/bash (or whatever path it is to your shell). Note the -c option for shells.

    As LFSR points out, this is the sort of thing likely to be full of security flaws.

    dacracot : SecurityManager?
    Tom Hawtin - tackline : java.lang.SecurityManager. Not very fashionable. Mostly used for applets and WebStart.
  • Why don't you do this from your app? It won't be very hard to implement an FTP client in Java. Surely there are open/free libraries to accomplish this somewhere. I don't believe this script does anything that you can't do in Java. A message driven bean with the FTP code should be enough. Your server is not FTP? Well, installing FTP isn't that hard either.

    dacracot : The script does more than we are willing to write in Java. Not because Java can't, but one line in bash to do a rsync using a dsa/rsa link would take considerable effort to replicate in Java.

0 comments:

Post a Comment