Skip to content

Instantly share code, notes, and snippets.

@codeZoner
Last active April 12, 2022 19:48
Show Gist options
  • Save codeZoner/6545d76cc0b5b80d9d8e76fb3a60cd1d to your computer and use it in GitHub Desktop.
Save codeZoner/6545d76cc0b5b80d9d8e76fb3a60cd1d 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