Sometimes you want to run a command on your server through PHP. There are couple of ways to do it, system()、exec()、shell_exec(), passthru(). All of them pretty much works alright with some slight differences. I would like to share my method which I think is more “programmatic”.
This is an example, it execute a backup of the whole mysql database. (1234 is the password)
1 2 3 4 5 6 7 8 9 10 | $output = array(); $return_var = -1; $command = 'mysqldump -u root -p1234 --all-databases > /home/mysql/all-database.sql'; $last_line = exec($command, $output, $return_var); if ($return_var === 0) { // success }else{ // fail or other exceptions } |
Linux/Unix conventions
You may ask how do I know what’s the return code from the shell command means. In fact, there is a set of convention among most Unix commands. Please reference to this Stackoverflow post. Although you may not sure what convention the command is using, you can be safe to assume code 0 means success.
Exception propagation
Since the error happen when you invoke the command will not be thrown in PHP environment, You have to throw it manually.
1 2 3 4 5 6 | if ($return_var === 0) { // success }else{ // fail or other exceptions throw new \Exception(implode("\n", $output)); } |
Full Example
The code in in Laravel, but the idea can be applied everywhere.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class AdminController extends BaseController{ public function executeBackup(){ try{ $this->runBackupMySQL(); echo "Success"; }catch(\Exception $e){ echo "Error : " . $e->getMessage(); } } private function runBackupMySQL(){ $output = array(); $return_var = -1; $command = 'mysqldump -u root -p1234 --all-databases > /home/mysql/all-database.sql '; $last_line = exec($command, $output, $return_var); if ($return_var === 0) { // success }else{ // fail or other exceptions throw new \Exception(implode("\n", $output)); } } } |