Print on php page what runs on background (Terminal) php_exec - php

I run a soft like ffmpeg with exec function. What I want is to see what is happening on the terminal the same time the command runs, I don't know if I can do it with echo or not. So, I need your help.
Thank you !

If you want to show the output of a command before it has finished, you should use a combination of popen() and flush(). There is a good example in the php.net docs, see http://www.php.net/manual/en/function.popen.php#56604

Related

how to use refresh html header through php through shell

I wrote a php script that refresh automatically to do a long process.
To do that, I use:
header('refresh: '.$refresh_time);
The time between every refresh is changed depending of what happened
It works well on a classic browser but I need to execute this script from shell now.
To do that, I tried:
php my_php_script.php
But this don't works as it execute the script only 1 time (yep, no refresh here).
So, is it possible to use the headers sent in php by calling the script in a command shell?
If yes, please enlighten my knowledge.
If no, BE DOOMED!!!! Damn, I'll have change my code!
Thanks for the help!
You can achieve that by using shell command in php file You can use the following code it will do the exact same thing in shell.
$refresh_time = rand(2,10);
sleep($refresh_time);
$command = "php my_php_script.php > /dev/null &";
shell_exec($command);
On Linux System, you can use cron. And on Windows, you can use windows schedule task.
Basically you can tell them when to execute your script. Every minute, day, week, etc.
Search on Google for more details.
Tricky part would be to change the time interval, can't help you much with that. Will surely let you know in case I find a solution for the same.
I answer my question but I would like to thanks you all for the helpfull tips.
As HTML headers can't be used by calling php though shell, I wrote a script that get the refresh header sent and sleep the time needed before calling again the php script.
#!/bin/bash
my_bash_function()
{
response="$(curl -vs http://localhost/my_php_script.php) 2>&1"
refresh_regex="refresh:[[:space:]]([0-9]+)\s*"
numeric_regex="^[0-9]+$"
if [[ "$response" =~ $refresh_regex ]] ; then
refresh="${BASH_REMATCH[1]}"
if [[ "$refresh" =~ $numeric_regex ]] ; then
sleep $refresh
my_bash_function
fi
fi
}
my_bash_function
That way, it allow me to not change the php script and simply use it as the curl or php command could respond to the refresh header.
Am I wrong ?
Edit: I was wrong! curl -I don't seams to execute properly the php code but only what the headers need. curl -vs seams to works but to get the header output, I had to add 2>&1
Credits: Jigar, Pavan Jiwnani

php exec() is not executing the command

I have tried to use exec() with 'whoami' to check if it works and I got the result of
nt authority\system
Now I need to run a .exe file with parameters from php via exec() function.
I tried this in command prompt and it actually runs the program with given parameters. This is the example command.
NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae)
> ..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml
But when I run this command from php file:
exec('..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml');
nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?
I tried using:
\\ instead of \
escapeshellarg() on the directory
added "" around directory folder names
No luck
Addendum:
echo exec($command) // echos < .... why?
or
exec($command, $output);
print_r($output); // Array()
I even changed the permission on the file to full control to all users.
If I call the program from command prompt, I can see the icon appearing next to clock for a second.
But the same call from php will not even call the program.
Edit
Even exec('notepad.exe'); is not working. Something has to be done with php configurations maybe?
I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().
Thanks #mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.
So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.
For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.
exec('some_command 2>&1', $output);
print_r($output); // to see the response to your command
Thanks for all the help guys, I appreciate it ;)
You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick.

exec causes perpetual load

I noticed exec and shell_exec is causing perpetual loading.
Basically, I'm trying to do something as simple as loading a PHP script in the background. When I try to do that, it just loads and loads.
My code is as follows
exec('php test.php -- '.escapeshellarg($param1).' > /dev/null ');
I first thought it was my other script, so I pointed it to a file with just:
echo $agrv[1];
But it still loads perpetually.
Don't wait for the process to exit
exec() waits for the process to give an exit code. The link I provided above may help you.
Oh, and since you tagged Linux for whatever reason, I assume you're on a Linux distro.
You could consider this, aswell:
http://ca1.php.net/pcntl_fork

How to print the output of a shell command in php?

I want to print the output of a command named jnettop into an html file.
I am very familiar with shell_exec() and sort.
But it doesnt work as the command that Im using doesnt have a bash mode like top that is described here.
The end goal is have a site that I go to and I see the information that jnettop would normally display in terminal.
Here is the link to jnettops wiki page.
You might try passthru() or exec() with the output argument peresent.
the command that Im using doesnt have a bash mode like top
Sure it has: jnettop --display text.

Run shell commands with PHP?

Occasionally my media server goes down and I'm wondering if it's possible to start it remotely using php to check the port and if it's not running invoke cron (or some other way) to run a shell command. Is this possible because this is not a strong area for me. Here's the process I use with PuTTy.
login to shell
cd to source/red5/dist
screen
./red5.sh
CTRL-A then D to detach
logout
Simplest thing is to write a shell script. And then login to remote console via PHP.
shell_exec: execute a shell command and returns the output as string.
exec: just executes an external program
A simple way to achieve what you want is to run this in screen:
while /bin/true ; do ./red5.sh ; done
If you can write a shell script that does what you need, then PHP's has exec(), system() and passthru() for you.
PHP actually has a special operator for executing shell commands, the backtick:
`cd source/red5/dist`
will go to the specified directory. (But I don't know much about shell, so I can't implement you the whole thing.)
If you need much control over the execution (I don't know whether you need here) use proc_open.
you can use corn job on php and put all command on .sh file and run like this
59 11 * * 1,2,3,4,5 root command file.sh?token
something like this ,it will be save
There is more than one good answer here, but you should opt for executing the init script for red5 instead of the .sh or .bat. There are pre-made init scripts here: http://code.google.com/p/bigbluebutton/downloads/detail?name=red5&can=2&q= and here: http://www.videowhisper.com/forum.php?ftid=48&t=init-file-red5-linux-installations-red5-linux-init.d-chkconfig

Categories