Execute multiple shell_exec at once? - php

How do I execute multiple shell_exec at once?
Also how do I switch each process to view output from test.php file? I need to know what each process (php file) is doing.
Take a look, example:
index.php
<?php
shell_exec("php test.php");
shell_exec("php test.php");
shell_exec("php test.php");
?>
That does not work how I wanted to be, first shell_exec (test.php) has to be completed first and then it will go to next shell_exec.
test.php
<?php
$section = rand(123,123);
$x = 1;
while($x <= 50) {
print $section . ": " . $x . "\n";
$x++;
sleep(1);
}
?>
It does not output when shell_exec is executed but when I type php test.php manually in shell, it does output.
Or is there a way to open multiple shells?
This is a quick example, however I will be executing multiple PHP file to run CURL

shell_exec("php test.php& php test.php");
Your best bet is to have those scripts log output to a file and actively parse it after running shell_exec().

Related

Php read a log content after executing a script

I have a php script that creates a shell script which is run after making it from the same php file, the shell script generates a registry file that I need to read after the script is executed, again from the same php. The php reads the file, but I think it does it before the file is filled or created, if I go back at the browser and execute the php again, then there is content at the textarea. I have tried to solve it adding sleep(), exit() functions and some other strategies but no success. Here are some of the things I've tried:
// Creation of the shell script: Corpus alignment target to origin
......
$cmd = "cwb-align-encode -r $REGDIR -D $CORPUSLOCATION$corpusname/$corpusname"._."$lang_tg.align\n\n";
file_put_contents($scriptfile, $cmd, FILE_APPEND | LOCK_EX);
// Run the corpus indexation script
$cmd = "/bin/bash $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);
Read the registry file from the same php:
// 1st try: no content at the textarea
echo "<textarea id='txtArea'>".htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>";
// 2nd try: no content at the textarea
echo "<textarea id='txtArea'>".sleep(10); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>";
// 3rd try: no content at the textarea
echo "<textarea id='txtArea'>".exit(); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ))."</textarea>";
// 4th try: no content at the textarea
echo "<textarea id='txtArea'>".if(filesize($REGDIR.$corpusname) != 0) { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); } else { exit(0); sleep(10); htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); }."</textarea>";
The command line you are using created a new thread that performs the task. PHP wont wait for it, as you do not refer the strout to php (but to /dev/null)
So, by changing the command, you can make PHP wait and thus get the result you expect.
Now I don't know for sure what the correct command is, but I would start with something like
$cmd = "/bin/bash $scriptfile"
Also have a look here. You want the opposite of what that guy wants. It does however give a bit more information about what the command actually does.
Even though the answer given by #Jeffrey is the right one, I realized that this answer is only good enough if the shell script takes short execution time, otherwise your php webpage can expire or hang up, so I gave another try with another php function: header('Refresh: x'), and that made it work right!
So here's what I get now:
// Run the corpus indexation script
$cmd = "/bin/bash $scriptfile > /dev/null 2>&1 &";
shell_exec($cmd);
<textarea id="txtArea" rows="28"><?php if (filesize($REGDIR.$corpusname) != 0) { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); }
else { header('Refresh: 0.5'); htmlspecialchars(file_get_contents( $REGDIR.$corpusname ));} ?></textarea>
UPDATE
Yet another solution:
do { echo htmlspecialchars(file_get_contents( $REGDIR.$corpusname )); } while (filesize($REGDIR.$corpusname) == 0);

How to execute shell from php script

I want to execute this command from a php script
scrapy crawl example -a siteid=100
i tried this :
<?php
$id = 100;
exec('scrapy crawl example -a siteid= $id' $output, $ret_code);
?>
try this:
<?php
$id = 100;
exec('scrapy crawl example -a siteid=$id 2>&1', $output);
return $output;
?>
You actually need to redirect output in order to get it.
If you don't need the output, and just to execute the command, you only need the first part, like this:
exec('scrapy crawl example -a siteid=' . $id);
because you don't put the parameter inside the ' ', you put it outside, read about text concat in PHP.
Depending on if you need the output of the script there are different approaches.
exec executes a command and return output to the caller.
passthru function should be used in place of exec when the output from the Unix command is binary data which needs to be passed directly back to the browser.
system executes an external program and displays the output, but only the last line.
popen — creates a new process that is unidirectional read/writable
proc_open — creates a new process that supports bi-directional read/writable
For your scrapy script I would use a combination of popen and pclose as I don't think you need the script output.
pclose(popen("scrapy crawl example -a siteid=$id > /dev/null &", 'r'));
From the PHP Manual - shell_exec()
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
So in short, there is a native PHP command to do what you want. You can also google for exec() which is a similiar function.
phpseclib - Download from http://phpseclib.sourceforge.net/ and include it in your project.
include('Net/SSH2.php');
$ssh = new Net_SSH2("Your IP Here");
if (!$ssh->login('Your User', 'Your Password')) {
exit('Login Failed');
}
$id = 100;
echo "<pre>";
print_r($ssh->exec('scrapy crawl example -a siteid= $id'));
Hope this helps.

run shell command from php ,

I need to run a python program with command line argument from my php script
This is my code
<?php
$output=shell_exec('python /path/to/python_from_php.py input1 input2');
echo $output;
?>
I am getting the contents inside shell_exec function as the output.
I tried using only
$output=exec('python /path/to/python_from_php.py input1 input2');
I am getting only the second input as my output.
But what I want is I should be able to print the input contents through my python script. If I remove the "echo $output" line I am not getting any output.
My python code
import sys
a=(sys.argv)
for i in (sys.argv):
print i
this code gives you the output from shell
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>
from this reference php also ensure php is not running in safe mode
but you could try just call the python script from php
<?php
$my_command = escapeshellcmd('python path/to/python_file.py');
$command_output = shell_exec($my_command);
echo $command_output;
?>
As OP wants to:
But what I want is I should be able to print the input contents
through my python script. If I remove the "echo $output" line I am not
getting any output.
should be used passthru, description
<?php
passthru('python /path/to/python_from_php.py input1 input2');
?>
Check, that you php user have access to run python and your python script.
Live demo is here
One option is to use passthru(), but if you don't want to manage the output buffer, and having the output in an array is desirable, you can add an output parameter to the exec command as so:
<?php
error_reporting(E_ALL);
$command = escapeshellcmd('/usr/bin/python /path/to/python_from_php.py arg1 arg2');
exec($command, $output);
for ($l=0; $l < count($output); ++$l) {
echo $output[$l]."\n";
};

shell_exec - logging & outputting

I am running multiple shell_exec, process.php's run in the background
On the shell/ssh, I execute the code like this: username [~/public_html/curl]# php index.php
Example....
index.php
<?php
shell_exec("php process.php > /dev/null 2>&1 &");
shell_exec("php process.php > /dev/null 2>&1 &");
shell_exec("php process.php > /dev/null 2>&1 &");
shell_exec("php process.php > /dev/null 2>&1 &");
?>
process.php
<?php
$section = rand(999,999999);
$z = 1;
print "STARTED .... \n";
while($z <= 10) {
print "---------------------------------\n";
print $section . ": " . $z . "\n";
$z++;
sleep(2);
}
print "LOOP FINISH at " . time();
?>
when process.php's is running, I am having two problems with this:
I cant not see the output from process.php's (I need to know what they are doing)
I need to know which process is finish and which have started.
What the best way logging the output in real time? saving into text file? or how can it be done to mysql database (logs table)?
If your processes will be alive only within the lifetime of another php script, you could use popen instead of shell_exec:
http://us.php.net/popen
This gives you a very convenient way to get data from the other processes into your php script, using the same interface as file handles. To know then the process is done, you could make sure the process sends an EOF (end-of-file) when it's done, and use the feof php function to detect it.
On the other hand, if your processes may live longer than any other php scripts that talk to it, then a text file may be a very practical solution. Keep in mind, though, that disk access is always much much slower than memory access, so if you use text files for communication, it will not be optimally fast.

Problems executing Perl scripts from PHP

Trying to figure this out. I am trying to execute a perl script within php, using shell_exec() like so:
<?php
$output=shell_exec("./tst.pl > test.txt");
//$output=shell_exec("./tst.pl");
echo $output;
?>
It will not write output to a file using ">" filename.txt.
It will work if I execute without directing it to a filename as I can confirm this with echo.
Does this have to do with using ">"?
Permissions should be fine as I am able to run the same perl script on command line and direct to file. Any suggestions for executing this?
The output of "test.txt" will be used as input:
<?php
$data = array();
$InputFile = file("test.txt");
...
?>
It was definitely a permissions problem. Wrote the file out to /tmp and it worked fine.
<?php
$output=shell_exec("./tst.pl > /tmp/test.txt");
echo $output;
?>

Categories