How to run a shell script file from php?

by dalton_moen , in category: PHP General , 9 months ago

How to run a shell script file from php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by aniya.jaskolski , 9 months ago

@dalton_moen 

You can run a shell script file from PHP using the exec() or shell_exec() functions. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
// Path to the shell script file
$scriptPath = '/path/to/your/script.sh';

// Run the shell script
$output = shell_exec($scriptPath);

// Print the output
echo $output;
?>


Make sure the shell script file has executable permissions (chmod +x script.sh) for the user running the PHP script. You can also pass arguments to the shell script by appending them to the $scriptPath variable.


Note: Running shell scripts from PHP can be a security risk if you're not careful. Make sure to validate and sanitize any user input before passing it to the script.