Execute shell command run php in background - php

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 &

Related

Running PHP Script with Windows Task Scheduler

So I found some posts on here about running PHP CLI scripts in the Task Scheduler, and it works like a charm! However, when I normally run the script in command line I output the results to a .log file:
ScriptName.php -arg1 -arg2 > log.log 2>&1
This doesn't seem to work in the "Add arguments" section of the Task.
I have it where Program/script is: C:\PHP\php.exe my argument is ScriptName.php -arg1 -arg2 > log.log 2>&1
Is there something I should be doing different?
Programs run from Scheduled Tasks don't generate any standard output or standard error. You need to launch a new cmd instance:
cmd /C C:\PHP\php.exe ScriptName.php -arg1 -arg2 > log.log 2>&1
So as a workaround I will just run the script from a .bat file
PHP ScriptName.php -arg1 -arg2 > log.log
and go from there.

execute linux command background in php

I need to execute a command in linux by using php and it should meet following requirement.
should create log file
should run as background
I know how to do it separately,
with log file :
shell_exec("command 1>log 2>&1");
as background:
shell_exec("command /dev/null 2>&1 &");
My question is how to do it together ?
shell_exec("command 1>log 2>&1 &");

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.

execute bash script from php without waiting

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 &");

Categories