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

    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    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

    Kris
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,959
    Rep Power
    4035
    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:
    Code:
    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:

    Code:
    exec("sudo /root/bin/startServer.sh");
    Last edited by kicken; February 19th, 2013 at 02:19 AM.
    Recycle your old CD's, don't just trash them



    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