1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Rep Power

    PHP exec java command as root

    Hi All,

    I am running php on CentOS, I need to execute a java command which will start a server, this command needs to be executed as a root user. PHP is running as apache user. The java command is like "java -Xmx256M -jar boot.jar --server 1985 > /dev/null 2>&1 &".

    I been trying to research for the last 2 days, and am not that good with linux, I did read about sudoer, i tried to add an entry in the etc/sudoers "apache ALL=(ALL) ALL" . It doesnt work. Please somebody help me out. Thank you

  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Bonita Springs, FL
    Rep Power
    Once you've added the appropriate settings to /etc/sudoers you will be able to run it as root. You need to run it via the 'sudo' executable though, as in:
    exec("sudo java -Xmx256M -jar boot.jar --server 1985 > /dev/null 2>&1 &");
    What I would do is wrap that command into a bash script and save it somewhere, say /root/bin/startServer.sh. Then you can easily add a specific entry to /etc/sudoers allowing apache to run only that script file. Then in PHP all you'd need is:

    exec("sudo /root/bin/startServer.sh");
    Last edited by kicken; February 19th, 2013 at 01:19 AM.
    Recycle your old CD's

    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo