Php exec ffmpeg not running in background - php

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

Related

how can i run ffmpeg command in background php

Hello guys i am building a web app with php, i want to add watermark to a video with ffmpeg but due the process do take time to finish, i want to run it in the background, here is the command that i used
$hidey=" 2>/dev/null >/dev/null &";
$cmd="ffmpeg -i $video_name -i watermark.jpg -filter_complex overlay=W-w-5:H-h-15[video];[video]drawtext=\"text=$text:fontcolor=white:fontsize=10:x=(w-text_w)-5: y=(h-text_h)-5\" $new_file
$hidey";
shell_exec("$cmd")
the above command does not work, i dont know why, what did i do wrong
I also tried var_dump(shell_exec("$cmd")), so that i can get the error, it gave me NULL

shell_exec() tcpdump from php

I am trying to write a script in php to run the following bash script using apache2+php7
#!/bin/bash
#cd /home/cwc/http/www/html/admin/web/
nowfile=$(date +"%Y%m%d-%H%M%S")
nohup tcpdump -w $nowfile.pcap -i enp2s0 >> /dev/null 2>&1 &
Now I understand I might have to use full paths
The above code works with a non sudo user using bash because I added a user the the pcap group.
I'm trying to figure out why this will not work with php
<?php
$command = "/pathtoscript/tcpdmp.sh
shell_exec($command); //not working?
?>

exec(); or shell_exec(); performance issue

I'm trying to execute by php exec(); a bash file which runs a process based on curl looping script. The problem is I've tried these methods:
exec();
shell_exec();
sudo -S nice -n -20
sudo -i
> /dev/null 2>/dev/null & (achro)
and started process by PHP exec(); are slow, response from curl is milliseconds slower than if I run it manually on SSH console directly as a command.
I have it this way right now:
start.php run this:
echo shell_exec('echo "password" | sudo -S nice -n -20 ./new.sh > /dev/null 2>/dev/null &');
new.sh run this
sudo -i /home/scripts/source/do.bat > /dev/null 2>/dev/null &
and do.bat run this
cd /home/scripts/a/2
echo "" > output.txt
./check.bat & echo $! > check.pid
cd /home/scripts/a/6
echo "" > output.txt
./check.bat & echo $! > check.pid
When I execute directly ./do.bat at console, script runs pretty fast, curl responses are as I need and everything goes fine, output.txt is written with a decent output speed.
But when I try to run it with PHP exec(); curl responses are slow down, and does not work as I need. Also you will ask why I run new.sh with start.php instead runs directly do.bat with start.php? Because if I do it, I get an output response very weird with an -e wrote on text.
Somebody can help me to make it run at same speed as if I run manually on console as command?

nohup >/dev/null alternative for windows

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?

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