Basically I'd like to know of a very simple method for making a php script check if a specific program is running on my nix box.
Something like :
<?php
#some detection function here.
if($detectprogram == 1){
echo("It's running.");
}
else{
echo("It's not running.");
}
?>
Or even if I had to execute the program via a .sh (as I already do) and an -outputphp /path/to/htdocs/. Or similar. Thanks.
Maybe you need this?
exec("ps auxwww|grep sample.php|grep -v grep", $output);
Source: How to get list of running php scripts using PHP exec()?
Related
I need to compile the php code on HTML Button click and display the compilation output on browser.I am using the following code to display the output on browser,it works well in terminal but doesn't give anything on browser.I am using XAMPP server on windows machine.
<?php
if(isset($_POST['compile'])){
$cmd="php php_part.php";
$var=system($cmd);
echo $var;
}
?>
<input type="submit" class="button" name="compile" value="Compile Code" />
First you are not compiling the php_part.php script, you are running it. Yes I realise that running a php script requires that it first be compiled or rather interpreted but that statement will execute the php_part.php script.
There are 2 likely reasons why it is not running.
When run from the browser i.e via Apache it does not know where to find php.exe
Depending on where the php_part.php script is kept it may not be able to find that either.
Try changing this statement
$cmd="php php_part.php";
to
$cmd="C:/path/to/php/php.exe C:/path/to/script/php_part.php";
If that works then that above was the problem.
I have to also say that there must be a better way of running this script. As you are in a PHP section of code would not a simple include or require not be a better solution. Something like this
<?php
if(isset($_POST['compile'])){
require "php php_part.php";
echo $var;
}
?>
Of course this depend on how you have written that php_part.php script. If you wrote it as a function then this would be a better solution
The php_part.php script
function xxx() {
// do whatever coding
return $the_result;
}
The main code
<?php
if(isset($_POST['compile'])){
require "php php_part.php";
$var = xxx();
}
?>
I am having a script which can be launched with either cron, HTTP request (browser) or from another script. How can I determine from where a script was launched?
You can check a few pieces of information to find out:
The constant PHP_SAPI will tell you which PHP interpreter/interface is running - the names of common SAPI's are documented on the page for php_sapi_name
For command line scripts, you should be able to achieve what you want my using a combination of the posix_isatty function and checking for the existence of $_SERVER['TERM']
The most simple way to solve this is to pass an argument to your script. This will give you high control over the format of the information and it is (reasonably) safe.
Not sure, is this what you wanted ..
Cron:
php -q /dir/my_script.php cron
HTTP:
http://dir/my_script.php?arg=web
PHP:
if (!empty($argv[0]))
{
echo "local";
}
elseif(isset($_GET['arg']))
{
echo "web";
}
else
{
echo "Others"
}
I'm looking to run a program, and for every output line it generates, execute a PHP script and pass the line content to it.
I know, pretty hard to understand. Here's an example:
Execute script -> script outputs 'Initializing script on 127.0.0.1'. Now it needs to execute a command like php5 input.php 'Initializing script on 127.0.0.1'.
Is this doable? If so, how would I go about doing this?
Edit: to clarify; I basically want command > log.txt but in stead of writing the output to that file, writing it to a PHP script as an argument
PHP is an interpreter much like Bash, Python, etc, so you can do "normal" scripting with it. For example:
#!/usr/bin/php5
<?php
echo "Hello, world!\n";
while($line = fgets(STDIN)) {
echo "> " . $line;
}
?>
Mark the file as executable, then run:
$ /program/that/generates/lines | /path/to/your/php/script
However, contrary to your original question, it sounds to me like you actually want to use JavaScript and possibly AJAX for web purposes. Sane web applications will have the said script run in the background and safely write the results to a file or stream, using AJAX to read it and update the information on the current page.
This question already has answers here:
How to run shell script with live feedback from PHP?
(2 answers)
Closed 9 years ago.
I want to know how PHP can run bash scripts.
I mean, I want to pass some variables from a PHP script to a bash script which will run when the submit button is clicked.
Also how can I take output from terminal to php page?
The purpose of this question is to have understanding of making front end in PHP and back end with bash, I want to run command of terminal from PHP page by giving some variables.
Please put me in right direction, I haven't slept for 4 days now, just reading stuff for it, so far I am confused.
You might want to take a look at the Symfony2 Console Component. It is designed to facilitate the execution of shell commands and returning their output.
Running shell script from php code can be done as follows:
shell_exec("script_path -with arguments");
The command shell_exec returning whatever the script prints out.
here is an example:
<?php
$dir_listing=shell_exec("/bin/ls -1 /path");
?>
Try this:
$file = "/etc/hosts";
echo `cat $file`;
Added:
If you want to use it like this:
$f = $_POST['f'];
$s = $_POST['s'];
$path = $basepath.'/'.$_POST['path'];
$res = `dvblast -f $f -s $s -c $path`;
be sure that your input is sanitized. May be shell injection here.
I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.
I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen() from PHP to call the Python script.
Any better ideas?
Depending on what you are doing, system() or popen() may be perfect. Use system() if the Python script has no output, or if you want the Python script's output to go directly to the browser. Use popen() if you want to write data to the Python script's standard input, or read data from the Python script's standard output in php. popen() will only let you read or write, but not both. If you want both, check out proc_open(), but with two way communication between programs you need to be careful to avoid deadlocks, where each program is waiting for the other to do something.
If you want to pass user supplied data to the Python script, then the big thing to be careful about is command injection. If you aren't careful, your user could send you data like "; evilcommand ;" and make your program execute arbitrary commands against your will.
escapeshellarg() and escapeshellcmd() can help with this, but personally I like to remove everything that isn't a known good character, using something like
preg_replace('/[^a-zA-Z0-9]/', '', $str)
The shell_exec() operator will also allow you to run python scripts using similar syntax to above
In a python file called python.py:
hello = "hello"
world = "world"
print hello + " " + world
In a php file called python.php:
$python = shell_exec(python python.py);
echo $python;
You can run a python script via php, and outputs on browser.
Basically you have to call the python script this way:
$command = "python /path/to/python_script.py 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
echo fread($pid, 256);
flush();
ob_flush();
usleep(100000);
}
pclose($pid);
Note: if you run any time.sleep() in you python code, it will not outputs the results on browser.
For full codes working, visit How to execute python script from php and show output on browser
I do this kind of thing all the time for quick-and-dirty scripts. It's quite common to have a CGI or PHP script that just uses system/popen to call some external program.
Just be extra careful if your web server is open to the internet at large. Be sure to sanitize your GET/POST input in this case so as to not allow attackers to run arbitrary commands on your machine.
Your call_python_file.php should look like this:
<?php
$item='Everything is awesome!!';
$tmp = exec("py.py $item");
echo $tmp;
?>
This executes the python script and outputs the result to the browser.
While in your python script the (sys.argv[1:]) variable will bring in all your arguments. To display the argv as a string for wherever your php is pulling from so if you want to do a text area:
import sys
list1 = ' '.join(sys.argv[1:])
def main():
print list1
if __name__ == '__main__':
main()
The above methods seems to be complex. Use my method as a reference.
I have this two files
run.php
mkdir.py
Here, I've created a html page which contains GO button. Whenever you press this button a new folder will be created in directory whose path you have mentioned.
run.php
<html>
<body>
<head>
<title>
run
</title>
</head>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python /var/www/html/lab/mkdir.py");
echo"success";
}
?>
mkdir.py
#!/usr/bin/env python
import os
os.makedirs("thisfolder");
Note that if you are using a virtual environment (as in shared hosting) then you must adjust your path to python, e.g: /home/user/mypython/bin/python ./cgi-bin/test.py
is so easy 😁
You can use [phpy - library for php][1]
php file
<?php
require_once "vendor/autoload.php";
use app\core\App;
$app = new App();
$python = $app->python;
$output = $python->set(your python path)->send(data..)->gen();
var_dump($ouput);
python file:
import include.library.phpy as phpy
print(phpy.get_data(number of data , first = 1 , two =2 ...))
you can see also example in github page
[1]: https://github.com/Raeen123/phpy
If you want to execute your Python script in PHP, it's necessary to do this command in your php script:
exec('your script python.py')