I want to write a php script for monitoring particular process using top command and write into the file.

I use top -p pid for monitoring a particular process and also i write a shell script for that.



echo 'counter value is :- '$counter
while [ $counter -lt 3 ]
        echo 'inside while'
        top -b -n 1 |grep $PROCNAME >> $FILE
        sleep $TIMEOUT
        echo $counter
echo 'while loop end'

This script will monitor init process after 5 sec for 3 times and write the it into file.

How can I write the same script in PHP?


Why not use

top -bd 5 -n 3 -p `pidof init` >> /home/test/nik/myscriptoutput2

instead of your whole script...

In PHP you can use

   $file  = "/home/test/nik/myscriptoutput2";
   $output = shell_exec('top -bd 5 -n 3 -p `pidof init`');
   file_put_contents($file, $output);
| improve this answer | |
  • Thank you it working successfully but using top -b -n 1 |grep init command. – Nikhil Patil May 22 '15 at 11:50
  • top -bd 5 -n 3 -p pidof init this command is not working it will show error (top: bad pid 'pidof init') – Nikhil Patil May 22 '15 at 11:51
  • You need to use backticks (`) instead of single quotes ('). – Lambert May 22 '15 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.