Jump to content

PHP Code to Execute Command on Linux Server


tom.park89

Recommended Posts

Hi Guys,

 

I'm trying to find a way to run a simple bit of code on My Debian Linux Server using PHP. I wan't to run the following Comannd '/etc/init.d/squid restart'.

 

I currently haven't been able to fingure out how to do this. I'm hoping that someone on this forum can help me with thsi.

 

Regards

Thomas

Link to comment
Share on other sites

 

I could use something like

shell_exec('/etc/init.d/squid restart'); or something like this

shell_exec('echo "password of root account" | -U root -S /etc/init.d/squid restart');

 

Also is there a way to catch any erros and display them to the screen. A user has to login to the page before they can run the command anyway.

 

Regards

 

 

Link to comment
Share on other sites

shell_exec returns the output of the command as a string.

 

That second command you posted makes no sense, and the first one, your server won't have permission to execute.

 

You would need to grant your servers process access via sudo.

Link to comment
Share on other sites

That second one I was given by someone else on another forum. I didn't think it looked right. I am a little new to Linux as in I have never granted access for server processes before. I know how to use sudo to install programs and change file permisions, restart services etc but never given access to processes before how would I go about doing this? or where could I find the info on how to do this?

 

also with the shell_exec command how do I output the responce to the screen?

 

Regards

Link to comment
Share on other sites

I know how to use sudo to install programs and change file permisions, restart services etc but never given access to processes before how would I go about doing this? or where could I find the info on how to do this?

 

Depending on your distro the /etc/sudoers file should be pretty well commented. You can give certain user access to sudo specific commands. You will also need to allow this user to execute sudo without being prompted for a password.

 

There is a good example here: http://www.gratisoft.us/sudo/sample.sudoers which is the first Google result I found.

 

also with the shell_exec command how do I output the responce to the screen?

echo shell_exec($cmd);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.