Question

Show Running Processes in PHP

Asked by: vePortal

Hi all, Please see my attached code,
I am attempting to use "vztop -e -n 1" in a PHP Script, My customer has been extremely specific that they NEED the output of this command displayed in a PHP Script. I've tried exec(), system() and passthru() functions each to no avail they all show " 22" which obviously is not the desired result.

Anybody who can assist on this matter there are 500 points to be had :) Thanks!

I need this to be able to format also, at present using <pre>$output</pre> is fine! but i need to be able to minipulate the rows printed.

<?php
exec('vztop -e -n 1', $output);
print_r($output);
?>

                                  
1:
2:
3:
4:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-06 at 19:03:32ID24791105
Tags

top

,

vztop

,

php

,

processes

Topic

PHP and Databases

Participating Experts
2
Points
500
Comments
30

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. passthru problem
    i have linux servers and i want to execute commands in my box 1 using my box 2 using php. i read that passthru can do this but whenever i use it the command executes in box 2 and just outputs the result in box 1. for example i use passthru('ls'); the list of the current dir...
  2. How can I: Echo Command Error with Exec/System/Pas…
    Hi experts, I've been trying to return command error when using the exec(), system(), or passthru(). <?php $cmd = "invaluidcommand 2>&1"; exec($cmd, $output); foreach($output as $line) { echo $line . '<br />'; } ?> I expected this dis...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: yauhingPosted on 2009-10-06 at 19:18:51ID: 25511728

Did you check the access right of the user, which start the web server?

 

by: gr8gonzoPosted on 2009-10-06 at 19:58:51ID: 25511912

I haven't played with vztop before, but it sounds like it's just virtuozzo's version of the "top" command.

That said, "top" (and apparently vztop) are interactive programs. They don't just print data out and end, so trying to capture the output could be tricky or next to impossible. Sometimes PHP has trouble capturing unusual outputs, and a general rule of thumb is that if you pipe the output to a file, like this:

somecommand > output.txt

...then output.txt should contain whatever text PHP should be able to read. (there may be exceptions, but it's usually true)

I know you said that they want that exact command, but vztop/top is really just running the vzps/ps command every few seconds, so the vzps command should be able to dump all the same process data (with the correct parameters). You could probably take that dump and convert it into output that looks like vztop's screen.

If you don't want to do that, then the only other things I can think of are beyond normal PHP. There could be a program out there that hooks into the operating system's graphics level (or at least right above it), and dumps out the current screen view to a file. If such a program exists, you could start the program with PHP, run the program with PHP, wait a second or two and kill the vztop process, then pull in the logged screen view.

Also, depending on how similar vztop is to top, you could also try out this solution:
http://www.directadmin.com/forum/archive/index.php/t-6539.html

 

by: vePortalPosted on 2009-10-06 at 20:01:58ID: 25511931

I've tried using a shell script to output to .txt file, to no avail, and using the -n 1 tag stops it and makes it a "Static" output rather than an "Interactive" output.

 

by: gr8gonzoPosted on 2009-10-06 at 20:16:14ID: 25511973

Just because the output APPEARS to be static doesn't mean that it's being sent to the normal outputs. There are different ways to send data to a screen. It's sort of like a TV with different channels. While YOU can change the channels and see the different content, programs like PHP are kind of stuck on the "main" channel, which is STDOUT. (This is not a 100% accurate explanation - I'm just trying to create a simple analogy)

If vztop is using a particular channel for sending out the automatically-updating data, then it's probably just sending the same static content over the same channel. Same channel, different content. :/

 

by: gr8gonzoPosted on 2009-10-06 at 20:23:16ID: 25512005

Just for curiousity's sake, try the first script here:
http://www.directadmin.com/forum/archive/index.php/t-6539.html

Just change the top command to your vztop version, then save the script as /tmp/capturetop.sh and, set up the shell script to have 777 permissions. Then, manually run the PHP script from the command line that executes something like:

exec("/tmp/capturetop.sh > /tmp/capturetop.log");

See if it makes any difference (the main difference would be the strict setting of the TERM environment variable).

 

by: vePortalPosted on 2009-10-07 at 07:41:23ID: 25516017

Hi,
none of the above methods work, they are all outputting the exact same thing, if i run "vztopcapture.sh" via command line it outputs "vztop.log" and the file is perfect, however if i execute vztopcapture.sh from PHP using various commands it always gives me the <2HR or something like that output.

 

by: vePortalPosted on 2009-10-07 at 07:41:45ID: 25516023

I've tried running it using a CRON also but the cron fails and doesn't capture the screen dump either.

 

by: gr8gonzoPosted on 2009-10-07 at 08:08:58ID: 25516339

How is the PHP script being executed? From the command line or from a web server? (Are you running it by going to the command line and running:
php /path/to/my/script.php
or are you going to a web browser and hitting a URL for that PHP script?)

Just so you know, the " 22 stuff is ANSI code. ANSI can be used by programs to draw on the screen. A server program sends ANSI codes like that, and the client program (if it is capable of ANSI), converts those codes into instructions that say things like, "Put a blue block at this location".

The reason the TERM environment variable is important is because it can potentially tell the script that the script should dump regular old terminal data (VT100) without any of the ANSI codes/formatting. If you're running the script from the web, then the web browser has different environment variables, and the script will still use the ANSI stuff by default.

 

by: vePortalPosted on 2009-10-07 at 08:32:25ID: 25516619

Now fixed, using the following code:

<?php
exec('/usr/bin/vztop -e -b -n1', $uptime);
foreach($uptime as $uptime2)
{
     $uptimeX .= "<pre>".$uptime2."</pre>";
}
print $uptimeX;
?>

NOW... this is working but nothings lined up correctly, Can anybody split this up so that i can print it into a nice table with rows & collums?

 

by: gr8gonzoPosted on 2009-10-07 at 09:00:38ID: 25516901

Wasn't that the original command you tried? :) Or was the -b parameter something that was needed to make it output correctly?

Do you have an example URL where we can see what it looks like (with it not lining up correctly) ?

 

by: vePortalPosted on 2009-10-07 at 09:10:09ID: 25516983

the "-b" removes the colour & highlighting from the script, which in turn allows PHP to post it.

This is what it does now, this is by adding the row code into each instance of $uptimeX
http://www.veportal.com/try/bad.jpg

And i want something like this:
http://www.veportal.com/try/good.jpg

So far ive done this by exploding $uptimeX['9']; by spaces for the header which im happy todo for every row BUT when for example one with PID #1 is sent it goes to the PHP with 5/6 white spaces before the actual number meaning my explode doesnt give the same array numbers every time, so each row would need its own code and a foreach wouldnt suffice.

this is how it posts to the PHP:
http://www.veportal.com/try/pre.jpg



 

by: vePortalPosted on 2009-10-07 at 09:22:06ID: 25517111

ok need this done ASAP now, first to it gets the points.

I have attached the PHP i have so far, i need to make it ignore Additional spaces AND i also need to make it ignore $uptime['0'] to $uptime['8'] as these dont fit into the table!!!

exec('/usr/bin/vztop -e -b -n1', $uptime);
 
	foreach($uptime as $uptime2) 
		{
			$output2 = preg_split('/\s+/', $uptime2);
			
				$uptimeX .= '<tr>
					<td>'.$output2['0'].'</td>
					<td>'.$output2['1'].'</td>
					<td>'.$output2['2'].'</td>
					<td>'.$output2['3'].'</td>
					<td>'.$output2['4'].'</td>
					<td>'.$output2['5'].'</td>
					<td>'.$output2['6'].'</td>
					<td>'.$output2['7'].'</td>
					<td>'.$output2['8'].'</td>
					<td>'.$output2['9'].'</td>
					<td>'.$output2['10'].'</td>
					<td>'.$output2['11'].'</td>
					<td>'.$output2['12'].'</td>
					<td>'.$output2['13'].'</td>
				</tr>';
		}
		
$output = preg_split('/\s+/', $uptime['9']);
print '<table border="1" width="100%">';
print '<tr>
		<td>'.$output['1'].'</td>
		<td>'.$output['2'].'</td>
		<td>'.$output['3'].'</td>
		<td>'.$output['4'].'</td>
		<td>'.$output['5'].'</td>
		<td>'.$output['6'].'</td>
		<td>'.$output['7'].'</td>
		<td>'.$output['8'].'</td>
		<td>'.$output['9'].'</td>
		<td>'.$output['10'].'</td>
		<td>'.$output['11'].'</td>
		<td>'.$output['12'].'</td>
		<td>'.$output['13'].'</td>
		<td>'.$output['14'].'</td>
	</tr>';	
print $uptimeX;
print '</table>';

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:

Select allOpen in new window

 

by: gr8gonzoPosted on 2009-10-07 at 11:00:34ID: 25518073

You can pass another parameter to the split or explode functions to limit the number of items in the final array. For example:

$string = "The quick brown fox jumped";
$array = explode(" ",$string,3);

// Array will look like:
// $array[0] = "The";
// $array[1] = "quick";
// $array[2] = "brown fox jumped";

That might help with the additional spaces.

 

by: vePortalPosted on 2009-10-07 at 11:52:31ID: 25518585

Thats just showing how to use the explode function, it's already being used and no good for additional spaces as the total number of spaces varies.

 

by: gr8gonzoPosted on 2009-10-07 at 12:31:36ID: 25519032

By the way, the above example was using explode, but limit is also a valid parameter for preg_split. I should have clarified that.

 

by: gr8gonzoPosted on 2009-10-07 at 12:48:49ID: 25519238

(I wrote that message on a cached page so I didn't see your latest response before writing). I had assumed that you ended up using preg_split and it worked fine except for the extra pieces at the end (which the limit parameter should address).

Alternatively, if you don't want to use preg_split, you could always just do pattern matching, like this:

<?
$uptime = array(
0 => "  PID USER     PRI NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME  CPU COMMAND",
1 => "12922 apache     18   0  2204 1008   740 R     0.9 0.0   0:00  0 vztop blahdeedoo"
);

$reCol = "([^\s]+)";
foreach($uptime as $uptime2)
{
      preg_match("/^\s*?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?{$reCol}\s+?(.*)/",$uptime2,$matches);
      print_r($matches);
}
?>

 

by: vePortalPosted on 2009-10-08 at 05:02:52ID: 25524354

No help :(

 

by: gr8gonzoPosted on 2009-10-08 at 05:51:33ID: 25524782

I'm not sure what that means. What have you tried and what's not working? My script above should work properly for parsing out the columns into arrays, regardless of spacing, so I'm unclear as to what you need if that does not solve it.

 

by: gr8gonzoPosted on 2009-11-02 at 05:16:42ID: 25718989

Just so you're aware, this is the second question that you've asked (that I've participated in, at least) where experts have spent multiple days trying to help and you've ended up just closing off the question when it seems like the proposed answers have helped you in at least one way. Per the last 3-4 comments on this question, if you found that my code did not help you, then saying, "No help" does not give us enough informaiton to help you further (which I then asked for so I COULD help you).

My last preg_match reponse should work on the output you showed in your example (I tested it against your sample first). i don't know if you're just trying to conserve your points, but it's rather unfair to close out a question if we've given you lots of information and proposed answers that may have helped you find an answer.

This is similar to hiring a consultant to help you pick the best choice among choices A, B, C, and D. The consultant spends 5 hours giving you advice and saying that you should pick B and C, and then you send the person away without paying them, saying that you picked B on your own. Except the experts here don't get paid - we just get points, and most of us work for a living, which means we take hours that we COULD use to get paid, but instead use them to try to help people here at EE.

 

by: gr8gonzoPosted on 2009-11-02 at 12:00:26ID: 25723107

Wow. Really?

 

by: gr8gonzoPosted on 2009-11-02 at 12:06:01ID: 25723165

Just for future reference (should anyone else read this), my last comment of "Wow. Really?" was in response to him simply accepting the first comment after I objected to him trying to close the question, saying that he had figured it out himself.

 

by: vePortalPosted on 2009-11-02 at 14:10:54ID: 25724336

I did figure this out for myself, Your response was not an answer to the question asked, And therfor is not acceptable.

I accepted that solution as someone kept complaining about me trying to close the question.
You tell me how i resolved this and i will accept your answer... until then... Why do you keep re-opening the discussion on this?

 

by: gr8gonzoPosted on 2009-11-02 at 15:11:59ID: 25724759

You asked two questions in this thread. The first was about getting the output. I provided a fair amount of information on the first question (regarding ANSI coding, it being a variation of top, etc), after which you seemed to get the answer. Then you stated:

"ok need this done ASAP now, first to it gets the points.

I have attached the PHP i have so far, i need to make it ignore Additional spaces AND i also need to make it ignore $uptime['0'] to $uptime['8'] as these dont fit into the table!!!
"

You even attached an example where you were using preg_split to split the lines into columns.

I gave you EXACTLY what you asked for, tested using YOUR data. That alone should have provided me with the points, according to your comment.

One day later, in response you said "no help", which seemed strange, because I DID test it with your data. I then I asked for more details (because "no help" gives me no information to further help you) and you ignored me and later tried to close the question.

Once I objected, you immediately accepted a comment that wasn't even an answer to your first question (much less the second), which has all the appearances of trying to "punish" me for objecting.

There is no way you can assign me points now. Normally, I don't object if someone figures something out on their own and closes the question, especially if they are new to EE. The reason I am continuing this conversation is because this is the second thread / question that you've asked (and I've seen) where experts have spent quite a bit of time and effort trying to assist you, providing you with information and suggested answers, which seem to at least lead you in the right direction, and afterwards you simply try to close the question and "stiff" them. It is simply unprofessional.

Like I said, many of us help out only for points. If you're going to put a question out there, asking for immediate help with 500 points and get a lot of information out of it, the least you can do is award points to the people who gave you information.

 

by: vePortalPosted on 2009-11-02 at 15:24:49ID: 25724845

First of all, i tried to close the questions as in this instance especially... i STILL havnt got it displaying correctly, but i have some form of output which i DID get for myself!

And i tried to close the question because im sick of EE Spamming me 5-6 emails a day telling me its abandoned... it's not i was waiting for an answer!

 

by: gr8gonzoPosted on 2009-11-02 at 16:39:36ID: 25725235

1. I can't do anything about EE spamming you (I'm just a user like you). However, in my experience, you should only get 1 email per abandoned question, so if you're getting 5-6 emails a day, then maybe you have 5-6 questions that you simply left open.

2. I explained how I answered the question. You still have not given any information that indicates that my last code snippet did not do what you asked for. I do not understand why you are so resistant to giving credit for answers and informatoin. (Or if it did not work, as you claim, why you are so resistant to giving details about how it did not work.)

Just so you know, EE is not some random forum where you just ask a question and then leave. When you ask a question, you are the OWNER of that question. As long as the question is not closed and has not been answered, then it is still an open question. So if you were not waiting for an answer, then at THAT time, you need to post a comment saying that you want to close the question and why.

Otherwise, you have experts occasionally checking into your question for updates, using their time to try to get an answer for you. If you do not care about anyone else's time except your own, then go ahead and keep abandoning questions, but don't complain when we post objections.

Again, if you're a newcomer and don't understand the common practices and rules, it's fine if you make a mistake or don't follow them, but I have little sympathy for the few people who do it multiple times and seem to be intentionally less-than-truthful. Some experts might let it slide, but I know that you're probably going to pull this type of stunt again and stiff someone else out of their time and effort.

 

by: gr8gonzoPosted on 2009-11-04 at 06:15:26ID: 25739451

Unless vePortal can explain otherwise, post 25519238 should have answered his question.

 

by: gr8gonzoPosted on 2009-11-09 at 09:08:34ID: 25777814

Per my last comment, post 25519238 should be an appropriate response.

 

by: vePortalPosted on 2009-11-09 at 10:05:03ID: 31638022

Bravo,

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...