Demiro-FE-Architect
Demiro-FE-Architect

Reputation: 3740

PHP: executing a service start (with sudo)

I am doing a system maintenance script that is checking the status of various services (memcached, sphinx, etc)

I would like to start the service from php script if it is down...

I managed to do it for memcache like this:

$command = 'memcached -d -m 800 -l 127.0.0.1 -p 11211 -u root start';
$dummy = system($command, $retval);

it is working GREAT...

now I'd like to do the same thing for SphinxSearch service

I tried

$command = 'service searchd start';

but got no result... and I read that I need sudo for this, so I did

$command = 'sudo service searchd start';

and even

$command = '/usr/bin/sudo /usr/bin/searchd';

but nothing worked.

Any suggestions?

Upvotes: 3

Views: 1743

Answers (2)

Searock
Searock

Reputation: 6498

Try using

echo 'yourpassword' | sudo -S command

Upvotes: 2

John Watson
John Watson

Reputation: 2583

Users need permission to run sudo. Specifically, the user your PHP script is running as needs to be in the sudoers file. See man sudoers for information about the file. Use visudo to edit the sudoers file. Be very careful about giving your php/apache user permission to run sudo. It is a potential security issue that can allow your PHP scripts to do anything.

Upvotes: 1

Related Questions