dikidera
dikidera

Reputation: 2043

Problem with exec() in PHP

Well, i have this program i need to run via either functions however it is located on my dekstop (this ubuntu 11.04).

I moved it to /home/Username, but no dice.

I run

$blah = exec('sudo | echo mypassword | /home/server1/program commandhere', $test); 
var_dump($test); 
var_dump($blah); ?>

The output is nothing.

I was told if i wanted to run it via sudo i needed to add the Apache user which is www-data to the sudoers list, i added it, but no luck again. Basically, i've tried A LOT of things, it just wont run. Why?

EDIT: If i paste that into the terminal it works great, just not with exec,system nor passtrhu.

Upvotes: 2

Views: 783

Answers (2)

rid
rid

Reputation: 63442

Use echo mypassword | sudo -S instead.

It also depends on which user has sudo privileges. If you want to run this from the apache process, you need to give the apache user sudo privileges as well.

Also, just to clarify, the command should be:

echo mypassword | sudo -S /home/server1/program commandhere

Upvotes: 2

Michael Berkowski
Michael Berkowski

Reputation: 270617

Look into your security log. Not sure where this is on Ubuntu, possibly /var/log/secure or /var/log/messages. I'm betting that you find a message there similar to sudo requires a TTY, or sorry, you must have a TTY to run sudo indicating that sudo is configured not to work without a real interactive shell. That is, sudo won't permit you to use it in a script or to be called by an external program.

I recently dealt with this issue myself while trying to bind a Gnome keyboard shortcut to a sudo command.

If this is the case, you'll need to comment out the following line in /etc/sudoers

#Defaults requiretty

Upvotes: 0

Related Questions