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 &");
Related
I'm running a php script which is not working properly in my windows OS but this is supposed to work in linux.
I figured it out that nohup is not an associated tool with windows.
$ffmpeg = 'C:\ffmpeg\bin\ffmpeg';
$command = "nohup >/dev/null 2>&1 ".$ffmpeg." -i {$input_path} {$ffmpeg_string} -stats -y {$output_path} 2> {$log_path} >/dev/null &";
exec( $command );
So what could be my best alternatives if I want to run this code on windows.
Detailed explanation will be greatly appreciated as I don't know much about background process.
nohup on windows, exec without waiting for finish
nohup >/dev/null 2>&1
The above command redirects everything from console to the null location, including the error log (2>&1).
on Windows this is done using echo off in batch.
Your question should probably be changed to "What is the equallent of nohup on Windows", it gives clarity.
i think this link gives you more information on your issue: What's the nohup on Windows?
I am trying to start a ffmpeg process from a php script and I know it has been asked a lot of times but I tried many solutions and none of them seem to work, each time the php script never finishes unless I kill the ffmpeg process. At the moment I am using this script which indeed starts ffmpeg and writes info in the designated files but the php script is loading forever.
What am I missing?
$cmd = 'cd cache && ffmpeg -y -i "rtsp://stream" -r 20 -f image2 a%6d.jpg >/dev/null 2>/dev/null &';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, 'log.txt', 'error.txt' . '.pid'));
A little more info: I am running FFMpeg 0.6.5, PHP 5.3.3 on CentOS 6.5
Thank you for your time!
You can use > /dev/null & instead of 2>&1 &
This will execute $cmd in the background without PHP waiting for it to finish.
Ref: http://php.net/manual/en/function.exec.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 &
I use exec() to execute command, either linux or windows.
How do you execute a command, linux and windows, and log the output without waiting?
I know for linux, to not wait for the output: command* > /dev/null 2>/dev/null &
And to log output for linux: command* > /path/to/log.txt 2>/path/to/error.txt
How would you go about logging and setting it to background in one command? How would windows look like too?
On Linux you can do:
exec('command* > /dev/null 2>/dev/null &');
On Windows you can do:
pclose(popen('start /B cmd /C "command* >NUL 2>NUL"', 'r'));
Both examples disable output and errors, those go to /dev/null (linux) or NUL (windows) which means they are stored "nowhere".
You can replace these with valid paths on your system.
On Linux, a & at the end places it into background. On windows this is more complicated and needs start to invoke the process and cmd to allow redirection of the streams.
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.