Loop bash script until cURL response doesn't equal string - php

I'm trying to loop a bash script (cURL below) until my remote PHP script doesn't equal failure. I've tried the following, and it works when $fileName exists.. but doesn't loop when it returns failure.
My PHP:
<?php
$videoFile = $_GET["name"] . ".mp4";
if (file_exists($videoFile)) {
echo "success";
} else {
echo "failure";
}
?>
Bash command:
until $(curl --output /dev/null --silent --head --fail "my-remote-php-script.php"); do
printf '.'
sleep 1
done

This should work:
while [ "$(curl -s 'http://your-url.php')" == "failure" ]; do
echo -n '.'
sleep 1
done
The $() places the curl in a sub-shell. The resulting string, comming from your php script, is compared with failure.

Try this:
url="http://server.tld/foo.php"
until curl -sf "$url"; do echo -n "."; sleep 1; done

Related

Why exec() is not working in foreach loop?

See, for testing purpose if I am trying to run PHP exec() statically with only one video file, then its compressing video perfectly(please see first line line).
Later when I am compressing dynamically in the loop, then exec function is not working. Please tell me why it's not working in the loop?
// echo shell_exec("/usr/local/bin/ffmpeg -i /home4/machine/public_html/riyaz/check_video/video_1494453415.mp4 -strict -2 -crf 20 /home4/machine/public_html/riyaz/zcompress/video_1494453415.mp4");
#include_once("start.php");
$directory_path = FILEPATH.'/';
$video_mp4 = glob("/home4/machine/public_html/riyaz/check_video/*.mp4");
/*video size will show in mb*/
foreach ($video_mp4 as $video_mp4_list){
$video_size = filesize($video_mp4_list);
$video_size_in_mb = round(($video_size/1048576), 2);
$get_file_name = explode('/',$video_mp4_list);
$get_file_name_for_destination = $get_file_name[6];
$getSourceFileNamePath = '/home4/machine/public_html/riyaz/check_video/'.$get_file_name_for_destination;
$getDestFileNamePath = '/home4/machine/public_html/riyaz/zcompress/'.$get_file_name_for_destination;
if ($video_size_in_mb >= 1000 ){
echo exec("/usr/local/bin/ffmpeg -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath);
}
}
shell_exec() returns the full output: PHP shell_exec() however
exec() returns only the last line from the outptut that might be empty.. So you have to provide second $output and third parameter $return_var to get more useful data: PHP exec()
if ($video_size_in_mb >= 1000 ){
$command = "/usr/local/bin/ffmpeg -y -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath . ' 2>&1';
echo PHP_EOL ."Executing command: ". $command;
if(file_exists($getDestFileNamePath)) echo "File already exists!";
$output = null;
$return_var = null;
exec($command, $output, $return_var);
print_r($command);
print_r($return_var);
print_r($output);
echo PHP_EOL;
} else {
echo "video too small to proceed';
}
php shell_exec() vs exec()
EDIT: The problem is that the destination file exists and ffmpeg exits without any action. One solution is to use attribute -y to overwrite the output file 5.4 Main options -y (global) Overwrite output files without asking.

sudo_exec returns nothing

I try to ping www.google.de with shell_exec and store the result into a variable but i get no result back from shell_exec.
<?php
$ping = 'sudo ping -c 4 ';
$url = 'www.google.de';
$command = $ping . $url;
$ping_result = shell_exec($command);
$datei = fopen("/var/www/myProject/result_ping","w") or die ("Could not open file!");
sleep(10);
if ($datei == false)
{
$ping_result = "Cannot open file!";
}
else
{
fwrite ($datei , $ping_result);
fclose ($datei);
}
echo $command; //Output: sudo ping -c 4 www.google.de
echo $ping_result; //Output: nothing
?>
The file result_ping has all rights (chmod 777).
Maybe the webserver is not allowed to execute ping?
Add 2>&1 to your command to ensure you're not getting an error message that shell_exec would filter off:
$command = $ping . $url . ' 2>&1';
shell_exec will return NULL in case of error. With that modification you redirect any error message to normal output, thus forcing shell_exec show every message you would normally get on a console session.

How to get pid of the process that run in background exec by shell in php

I'm working on a shell in php, and I want to display the same output as bash. When in bash you execute sleep 10 & you'll get [1] <PID>. How can I do the same in php, when I call shell using:
if (preg_match("/&\s*$/", $command)) {
$this->$shell_fn($token, '/bin/bash -c ' . escapeshellarg($command) .
" > /dev/null");
return array(
'output' => '',
'cwd' => $path
);
}
$shell_fn is variable that point to wrapper over shell_exec, exec or cgi script called by curl. Is it even possible to get the pid from php or using a shell?
If you want the pid in bash, you can do use the ! special parameter to get the pid of the most recently backgrounded process:
bash -c 'sleep 10 & echo $!'
I don't know exactly how php spawns external processes, but I imagine you'd be able to capture the echo output here, just by running the above shell command.
I use this functions to manage process:
function ProcessStart($cmdline) // return pid
{
exec( "nohup $cmdline >/dev/null 2>&1 & echo $!", $output) ;
// print_r ($output);
return (int)$output[0];
}
function ProcessStatus($pid) // return TRUE (live) o FALSE (dead)
{
exec("ps -p $pid",$output);
// print_r($output);
return ( isset($output[1]) ? TRUE : FALSE ) ;
}
function ProcessStop($pid)
{
exec("kill $pid",$output); // kill -9 ??
}

can't download file using wget with authentication while being called from php

I've created a small remote download script which downloads remote files to my server using wget.
Now here's how it works:
I have a html form, which the user can enter the URL, destination file name, and optionally username and password to authenticate, in case needed.
The form calls a php script, using AJAX, and the php script passes the information to a shell script.
Now the problem is, the shell script works flawlessly on my local linux machine, but on my server, it doesn't work with authentication(without authentication, it works just fine.)
#!/bin/bash
if test $# -eq 2
then
wget -O "$2" $1
elif test $# -eq 4
then
wget -O "$2" --http-user="$3" --http-password="$4" $1
else
wget $1
fi
it might be worth mentioning that, I own a shared server.
Thanks
EDIT:
Is it possible that the version of wget installed on the server, doesn't support http authentications???
EDIT 2:
I piped the output of wget to a file like this:
wget -O "$2" --http-user="$3" --http-password="$4" $1 | echo >> output
but the output is empty, i.e. no messages is being printed from wget!!!
And I did this too, to check if the credentials passed is ok:
echo "$3 | $4" >> credentials
And it was ok.
And this is the php code which runs the shell script:
if(isset($_POST["startdownload"]))
{
list($url, $dest, $user, $pass) = explode("|", urldecode($_POST["data"]));
$filelen = file_len($url);
$freespace = getRemainingSpace();
//die( "( Free: $freespace, File: $filelen )" );
if($filelen > $freespace - 10000)
{
$result = json_encode(array("error" => true,
"message" => "There is not enough space to download this file. ( Free: $freespace, File: $filelen )"));
die($result);
}
else
{
if($user != "NULL" && $pass != "NULL")
{
execInBackground("../dl.sh '{$url}' '../{$dest}' '{$user}' '{$pass}'| at now");
}
else
{
execInBackground("../dl.sh '{$url}' '../{$dest}' | at now");
}
$result = json_encode(array("error" => false, "message" => "Starting download..."));
die($result);
}
}
function execInBackground($cmd)
{
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
Well, running this script via system/exec/shell_exec or any others, probably going to "block" the rest of the PHP script anyway. But as an answer, check out this: http://www.zarafa.com/wiki/index.php/Using_wget_and_HTTP_authentication_for_supported_downloads
wget -O "$2" --user="$3" --ask-password="$4" $1

Escaping quotes in a PHP script using ssh2_exec

I need some help with escaping the quotes in this script, I have code above and below, but it does not pertain to this because if I just issue a screen -d -m -S minecraft, it creates one fine. Any help would be amazing because i've been trying for an hour now, Thanks!
if (!($stream = ssh2_exec($con, 'screen -d -m -S minecraft -p 0 -X stuff "\printf "\say This is a test.\r"\"\' ))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
echo $data;
fclose($stream);
}
Shouldn't the \ should go before the ". Also, from your description, it sounds like that you only want to run "screen -d -m -S minecraft" and it looks like you have an extra printf in there.
if (!($stream = ssh2_exec($con, "screen -d -m -S minecraft")))
http://php.net/manual/en/language.types.string.php
You only need to change "\ to \".

Categories