Skip to content
Advertisement

Php executing a bash script with at command

Hello I’am trying to execute a shell command in my php script but it is not working. My php script :

           //I save the Order 
            $holdedOrder->save();
            $id = $holdedOrder->id;
            $old_path = getcwd();
            chdir(__DIR__.'/../');
            $scriptFile = 'anacron_job_unhold_order.sh';
            $bool = file_exists($scriptFile);
            //$bool is true !

            //this command works in shell but not in here do not know why
            $s = shell_exec("echo "/usr/bin/bash $scriptFile $id" | /usr/bin/at now +$when");
            chdir($old_path);
            return [$s,$bool];

$when has a valid value 4 hours or 4 days … The command will be :

echo bash anacron_job_unhold_order.sh 29 | at now +1 minutes

the output is null. Trying it with exec() is returning 127 code

Edit : I removed www-data from /etc/at.deny and still the same problem

Advertisement

Answer

The command shell_exec("echo "/usr/bin/bash $scriptFile $id" | /usr/bin/at now +$when"); is probably causing the issue, you should take a closer look on how at works.

The command should be something like

at [options] [job...]

To give a job to at from the command instead of STDIN, use heredoc syntax

at now + 1 minute <<< 'touch /tmp/test'

So the PHP code should be something like;

$s = shell_exec("/usr/bin/at now + {$when} <<< '/usr/bin/bash {$scriptFile} {$id}'");
exec($s, $output, $return_var);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement