Run Bash Command from PHP
You probably need to chdir to the correct directory before calling the script. This way you can ensure what directory your script is "in" before calling the shell command.
$old_path = getcwd();
chdir('/my/path/');
$output = shell_exec('./script.sh var1 var2');
chdir($old_path);
Running php script (php function) in linux bash
From the command line, enter this:
php -f filename.php
Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.
Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).
Reference: https://secure.php.net/manual/en/features.commandline.usage.php
Run bash script from PHP script
I found what the issue was. It was because of Linux SELinux feature. This feature applies a least-privilege policy and denies any unnecessary command from running on Linux. The bash script is running successfully after disabling this feature. To do so, edit the file /etc/selinux/config and change SELINUX=enforcing to SELINUX=disabled and reboot the system. THIS IS NOT RECOMMENDED FOR SECURITY REASONS, however. You may check the link below to only create some exceptions rather than completely disabling SELinux.
https://wiki.centos.org/HowTos/SELinux
PHP how to run multi bash command in background
I think you also need to handle sleep
's stdout/stderr.
( sleep 5 > /dev/null 2>&1; ...; ) &
Or you can put the redirection after ( ... )
:
( sleep 5; ffmpeg ...no redir here...; ) < /dev/null > /dev/null 2>&1 &
Making Command in bash script to run php script
You need to look at the shebang line.
Placing #!/usr/bin/env php
at the top of your file should work as long as you have execute permissions:
#!/usr/bin/env php
<?php
echo "Hello World";
You can then utilize the native function getopt() to get options, or parse argv yourself.
exec() hangs when I run bash command in background
I've managed to fix it, but I have no idea why the new solution works while the old one doesn't. Maybe it has something to do with exec() build-it parser? Both lines work identically in bash so I'm blaming PHP on this.
So, I've replaced
$exec_result = exec('./myapp option1 option2 &> /dev/null &');
with
$exec_result = exec('./myapp option1 option2 > /dev/null 2>&1 &');
and that did it. I've checked it back and forth multiple times and the second line works consistently while the first one fails every time.
Related Topics
Pinging an Ip Address Using PHP and Echoing the Result
Parse an Xml With Simplexml Which Has Multiple Namespaces
Creating Jquery Ajax Requests to a PHP Function
How to Replace MySQL Functions With Pdo
Is the Leading Colon For Parameter Names Passed to Pdostatement::Bindparam() Optional
How to Create Helper Methods on Laravel Not a Facade
How to Write a File in Utf-8 Format
Connect PHP to Mssql Via Pdo Odbc
Format Bytes to Kilobytes, Megabytes, Gigabytes
Which $_Server Variables Are Safe
Notice: Trying to Get Property of Non-Object Error
What's the Deal With a Leading Underscore in PHP Class Methods
Youtube API - Extract Video ID
Sqlstate[Hy000] [2002] Connection Refused Within Laravel Homestead