execute bash script from php without waiting - php

I execute a bash script from php with shell_exec.
But the php script waits until the shell script is finished.
Can I somehow call the bash script without waiting.
Both:
exec
shell_exec
are waiting until the bash script is finished.
I'm running linux btw.

This has to work:
exec('/your/command /dev/null 2>/dev/null &');

when calling your bash script append & so it will run in the background
that's the easiest way if you don't need any output
shell_exec("/bin/bash /path/to/script.sh &");

Related

shell_exec doesn't execute from PHP but works fine in shell

user/bin/inkscape -f sample.svg -e sample.png
Above command works when executes in shell, but with php shell_exec() its not executing.
Any help will appreciated.

How to stop PHP file execution with exec or shell_exec

I have triggered a PHP file which has an infinite loop, with:
shell_exec('php '.__DIR__.'/serv.php 2>&1 > /dev/null &');
and now I can't to stop it.
Is there any way to do it with 'shell_exec' or 'exec'?
PHP is a service. You can either run killall php or killall php-cgiin shell or, if you are using a program like MAMP or WAMP, you can simply close and reopen the program. Also, if you are using a later version of PHP there should be infinite loop failsafes. Check your php.ini for max_execution_time to see how long the script should be allowed to run. You can also set this in your file using the set_time_limit function.
just run the following on your shell
killall php

Execute shell command run php in background

I run a php script through shell using the following:
php script.php
How do I type this command in order to run it on the background and also log the output to a file?
I've tried
php script.php 2>&1 > out.log
But once I close putty, the script stopped.
you can use nohup (no hang up)
nohup php script.php 2>&1 > out.log
or you use cron or at to run your script in background
Add a & after the command.
Try call like this:
php script.php 2>&1 > out.log &

Run command from PHP, exit script, but keep command running

I am running a PHP script that uses the passthru function to run system commands in the background. By redirecting the output and putting the & at the end of the command, the script is able to continue executing without waiting for the system command to finish. However, the PHP script itself does not exit until all commands are complete. Is there a way that the PHP script can start a system command and then not have to wait for that command to finish before exiting?
Have you tried using nohup?
Instead of
my_command >/dev/null &
do
nohup mycommand >/dev/null &

How to execute remote SSH command in background using PHP exec()?

Is there a simple way to execute SSH commands in the background on remote machines from PHP without using ssh2_*? The PHP script is executed by the user from bash (no Apache involved), so it's not an issue of rights. I've tried doing this:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i {$keyFile} {$user}#{$ip} {$remoteCommand} 2>&1 >/dev/null </dev/null");
For example:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/data/id_rsa user#192.168.0.19 '/home/user/script.sh ; exit' 2>&1 >/dev/null </dev/null");
All PHP variables have been escaped with escapeshellarg() and $remoteCommand is a bash script on the remote machine that sleeps for a few minutes and then starts executing some commands.
My problem is that if I execute that SSH command from bash, it gives control back to bash immediately. If I execute it from php using exec() it waits until the remote command executes. I've tried adding 2>&1 >/dev/null </dev/null after /home/user/script.sh, but the execution still doesn't return control to the PHP script.
I think you are missing an & at the end of your command for sending the execution to the background.

Categories