Hello, I need to write a cgi script which will print the output from ps(1) in a table (html), so the average-operator can click on a KILL link and the cgi will send the selected signal. I need to add one ps information per column in a table (html), however, I found ps(1) output to be too hard to parse. There is no separator. I believed \t was the separator but its not. The ps(1) command I need to use is: ps -ax -o pid -o user -o emul -o lstart -o lockname -o stat -o command Since many of those args use [:space:] in the output, I can not use [:space:] as a separator. Sadly, `-o fiend='value'` will only format the HEADER output, not the values. Ive got no clue what to do, can someone enlight me? Thank you all in advance. -- ==========Eduardo Meyer pessoal: dudu.meyer@gmail.com profissional: ddm.farmaciap@saude.gov.br
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Eduardo Meyer wrote:> Hello, > > I need to write a cgi script which will print the output from ps(1) in > a table (html), so the average-operator can click on a KILL link and > the cgi will send the selected signal. > > I need to add one ps information per column in a table (html), > however, I found ps(1) output to be too hard to parse. There is no > separator. I believed \t was the separator but its not. > > The ps(1) command I need to use is: > > ps -ax -o pid -o user -o emul -o lstart -o lockname -o stat -o command > > Since many of those args use [:space:] in the output, I can not use > [:space:] as a separator. > Sadly, `-o fiend='value'` will only format the HEADER output, not the values. > > Ive got no clue what to do, can someone enlight me?Perhaps use cut(1) with -c or something similar in other scripting language? It looks like that the output is aligned. Cheers, - -- Xin LI <delphij@delphij.net> http://www.delphij.net/ FreeBSD - The Power to Serve! -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (FreeBSD) iEYEARECAAYFAkkI7pEACgkQi+vbBBjt66Bi3wCgmk9chU/FIZjuBpm/57Yl7jBY D6kAoI6ZmQRdxDm7mzjale84p4uXmlmz =4FMM -----END PGP SIGNATURE-----
On Wed, 29 Oct 2008 20:02:43 -0200, "Eduardo Meyer" <dudu.meyer@gmail.com> wrote:> I need to write a cgi script which will print the output from ps(1) in > a table (html), so the average-operator can click on a KILL link and > the cgi will send the selected signal.If you can use awk, it's quite simple: ps | awk -F " " 'NR > 1 {printf("<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n", $1, $2, $3, $4, $5);}' The only problem I see is that $5, the COMMAND field, is truncated after the first space character, so command line arguments will be missing. -- Polytropon>From Magdeburg, GermanyHappy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...
| By Eduardo Meyer <dudu.meyer@gmail.com> | [ 2008-10-30 00:04 +0200 ]> Hello, > > I need to write a cgi script which will print the output from ps(1) in > a table (html), so the average-operator can click on a KILL link and > the cgi will send the selected signal. > > I need to add one ps information per column in a table (html), > however, I found ps(1) output to be too hard to parse. There is no > separator. I believed \t was the separator but its not.Another option might be to mount /proc and use that instead. See procfs(5). Regards, Aragon
Eduardo Meyer wrote: > I need to write a cgi script which will print the output from ps(1) in > a table (html), so the average-operator can click on a KILL link and > the cgi will send the selected signal. > > I need to add one ps information per column in a table (html), > however, I found ps(1) output to be too hard to parse. There is no > separator. I believed \t was the separator but its not. > > The ps(1) command I need to use is: > > ps -ax -o pid -o user -o emul -o lstart -o lockname -o stat -o command You should use -axww so the command is not truncated. > Since many of those args use [:space:] in the output, I can not use > [:space:] as a separator. Yup, that's a problem. That's why a simple awk line won't do it. You should use fixed character positions instead of field separaters for splitting fields. I think you can look at the headers (first line) to determine the field widths. All fields are left-aligned except for the PID. So your script should simply look at the first line and store the starting position of every word in an array. Using those values you can split all the data lines according to the character positions. I could provide a 3-liner in Python, but I assume you're not writing that script in Python. :-) Best regards Oliver -- Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M. Handelsregister: Registergericht Muenchen, HRA 74606, Gesch?ftsfuehrung: secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht M?n- chen, HRB 125758, Gesch?ftsf?hrer: Maik Bachmann, Olaf Erb, Ralf Gebhart FreeBSD-Dienstleistungen, -Produkte und mehr: http://www.secnetix.de/bsd C++: "an octopus made by nailing extra legs onto a dog" -- Steve Taylor, 1998
>Hello, > >I need to write a cgi script which will print the output from ps(1) in >a table (html), so the average-operator can click on a KILL link and >the cgi will send the selected signal. > >I need to add one ps information per column in a table (html), >however, I found ps(1) output to be too hard to parse. There is no >separator. I believed \t was the separator but its not. > >The ps(1) command I need to use is: > >ps -ax -o pid -o user -o emul -o lstart -o lockname -o stat -o command > >Since many of those args use [:space:] in the output, I can not use >[:space:] as a separator. >Sadly, `-o fiend='value'` will only format the HEADER output, not the >values.>Ive got no clue what to do, can someone enlight me?>Thank you all in advance.-->==========>Eduardo Meyer >pessoal: dudu.meyer@gmail.com >profissional: ddm.farmaciap@saude.gov.brHere is something simple, and you can wrap the HTML around it...; poshta:$ps axuww | while read USER PID CPU MEM VSZ RSS TT STAT STARTED TIME COMMAND; do echo $PID $CPU $USER $COMMAND;done |head -3 PID %CPU USER COMMAND 11 89.6 root [idle] 5127 2.9 qscand spamd child (perl5.8.8) the read ignores all white space...the last variable in that 'while read' will hold everything beyond it... ie; poshta:$ps axuww| while read USER PID CPU MEM VSZ RSS TT STAT STARTED TIME; do echo $PID $CPU $USER $TIME;done |head -3 PID %CPU USER TIME COMMAND 11 77.9 root 138080:11.91 [idle] 13607 5.0 qscand 0:09.12 spamd child (perl5.8.8) etc.etc... ]Peter[