tom.park89 Posted August 3, 2011 Share Posted August 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243732-php-code-to-execute-command-on-linux-server/ Share on other sites More sharing options...
Cagecrawler Posted August 3, 2011 Share Posted August 3, 2011 exec should serve your needs. Quote Link to comment https://forums.phpfreaks.com/topic/243732-php-code-to-execute-command-on-linux-server/#findComment-1251435 Share on other sites More sharing options...
Maq Posted August 3, 2011 Share Posted August 3, 2011 Specifically you want, shell_exec although, you might run into user permission issues. Quote Link to comment https://forums.phpfreaks.com/topic/243732-php-code-to-execute-command-on-linux-server/#findComment-1251443 Share on other sites More sharing options...
tom.park89 Posted August 4, 2011 Author Share Posted August 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243732-php-code-to-execute-command-on-linux-server/#findComment-1251864 Share on other sites More sharing options...
trq Posted August 4, 2011 Share Posted August 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243732-php-code-to-execute-command-on-linux-server/#findComment-1251867 Share on other sites More sharing options...
tom.park89 Posted August 4, 2011 Author Share Posted August 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243732-php-code-to-execute-command-on-linux-server/#findComment-1251977 Share on other sites More sharing options...
trq Posted August 4, 2011 Share Posted August 4, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/243732-php-code-to-execute-command-on-linux-server/#findComment-1252206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.