Skip to content

Instantly share code, notes, and snippets.

@sagar290
Forked from codeZoner/run-command.php
Created January 24, 2020 17:36
Show Gist options
  • Save sagar290/e7d48aa04a1d34253a56d305ec017448 to your computer and use it in GitHub Desktop.
Save sagar290/e7d48aa04a1d34253a56d305ec017448 to your computer and use it in GitHub Desktop.
Execute Shell Script using PHP (Git Pull Example)
<?php
// chdir to the correct directory before calling the script
//Ref: https://stackoverflow.com/questions/11052162/run-bash-command-from-php#answer-11052453
$old_path = getcwd();
chdir('/path/to/file');
//make sure to make the shell file executeable first before running the shell_exec function
$output = shell_exec('./shell-script.sh');
chdir($old_path);
echo $output;
?>
#!/bin/bash
cd /path/to/git/folder && git pull origin branch_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment