bash - PHP: cannot run nested shell scripts

Solution:

In the inner shell, the arguments are not called like that (because it is invoked by the shell, not by PHP).

./secondScript.sh "$1" "$2" "$3"

or just

./secondScript.sh $*

At this stage, just remember that spaces, quotes, and dollar signs in the arguments you pass must be avoided at all costs (the solution is escaping them, but how to do it exactly is tricky).

You also might want to do instead:

$ret = shell_exec("./randomScript.sh 'arg1' 'arg2' 'arg3' 2>&1");

so that in $ret you might find error messages and output from the inner shell.

Answer

Solution:

You should escape your string command before passing it in exec function, i think it may help you

...
$escaped_command = escapeshellcmd("./randomScript.sh arg1 arg2 arg3");
exec($escaped_command);
...

For system command, i recommand using system

...
$escaped_command = escapeshellarg("./randomScript.sh arg1 arg2 arg3");
system($escaped_command);
...

Answer

Solution:

You also need to make sure that your PHP code does not change working dir, and both shell scripts have execute permissions, and filesystems allows to exec files on it.

I would avoid exec("./script $arg ...");, I would rather specify the interpreter to use and full path to the script like exec("sh /home/user/project/script.sh $arg ...");

Source