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?
Related
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?
?>
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?
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 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.