2

I made a script in bash for getting pings into a file.

#!/bin/sh
echo "Starting script"
echo "Working.."
while true; do
    DATE=$(date)
    PING=$(ping -c 1 google.pl | tail -1| awk '{print $4}' | cut -d '/' -f 2)
    echo "$DATE Ping: $PING" >> logs/ping.txt
    sleep 5000
done

But due to lack of free space i changed echo "$DATE Ping: $PING" >> logs/ping.txt to just echo "$DATE Ping: $PING" to recive every line in cmd, and it worked

But still the main idea is to run the scipt through the web browser and display its output. (i can run it tho but i have no idea how to show echo output in a browser)

5
  • You tagged this as PHP; What do you have so far? Commented Feb 26, 2014 at 13:56
  • Also, what are you trying to accomplish? Commented Feb 26, 2014 at 13:57
  • Other *.sh scripts which i am using have normal php code inside so i thought that there is a way to do that in php too. Commented Feb 26, 2014 at 13:59
  • Just like I said i would like to see output of this script in web browser not just in console. Commented Feb 26, 2014 at 13:59
  • do you have a web server set up for php? Commented Feb 26, 2014 at 14:02

3 Answers 3

3

You can call the bash script from php using:

exec('myscript.sh');

And then open the ping.txt using:

$myFile = "ping.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

Without text file:

$ping = shell_exec('myscript.sh');
echo "<pre>$ping</pre>";
Sign up to request clarification or add additional context in comments.

5 Comments

Yup its good option but like I said i cant use script version with ping.txt due to lack of space, it must be something like echo in cmd but in web browser
Output will be shown only when script will stop and I need realtime preview
Does $ping contain the stdout as string? That means that shell_exec is synchronous, right?
@Nenotlep Yes, it's a string. I'm not a PHP expert but I believe the shell_exec is synchronous.
1

With a bit of ajax and using Net_Ping you could have a page that updates in near-realtime. Alternatively use shell_exec to run ping from inside your php and echo the output returned from it.

Comments

0

If you need to execute your bash script from PHP and display the output in a browser, then just use the shell_exec() PHP function

Example from php.net:

<?php
$output = shell_exec('/path/script-name-here');
echo "<pre>$output</pre>";
?>

2 Comments

Output will be shown only when script will stop and I need realtime preview
Typically a webserver will always server the content from start to finish. It isnt really designed to keep a connection open and keep sending your information. So, along the lines of what kguest wrote; you will need to make multiple asynchronous requests with javascript, and display the results in the browser as each response comes back in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.