How can I run a linux command from a PHP script?


I am trying to run this linux command from a PHP ,How I do this ?

//command 1
yum install wget

//command 2
cd /root/
chmod -R 777 /root/

//commande 3
echo "10 0   * rm -f /var/log/pmta/log-*" | tee -a /var/spool/cron/root 2>&1
echo "*/30     /usr/bin/php /var/www/exactarget/Send/received.php" | tee -a /var/spool/cron/root 2>&1
echo "*/30     /usr/bin/php /var/www/exactarget/Send/delivered.php" | tee -a /var/spool/cron/root 2>&1
echo "*/30     /usr/bin/php /var/www/exactarget/Send/bounce.php" | tee -a /var/spool/cron/root 2>&1
service crond restart

//command 4
service httpd restart
service pmtahttp restart
service pmta restart

Thank you so much

By using one of the sys exec functions:

Mind that PHP likely won’t have the required permissions to run commands that modify the OS.


Why do you want to run this in PHP? Why not just a bash script? That seems perfect for this.


THANk you

thank you so much for you respond but im just a beginner in PHP. and I have app Programmed on this language. but that don’t prevent me to trying The way That you suggested . could you help me ??

Did you try the example on the page Dormilich linked to?

Example #1 An exec() example

// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');

Please pay special attention to the Notes and Warnings on that page. This path has a lot of potentially major pitfalls and should not be taken lightly or rushed.

1 Like

First you need to modify the script so that you don’t need that chmod. Its a very bad idea from a security point of view.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.