PDA

View Full Version : Running a a system command as another user on BSD


nkrgupta
09-07-2006, 07:05 AM
Hi,
I am running a small program which executes the command
`ps -axme > /usr/local/apache/cgi-bin/process_list` and then i iterate through the generated file to show the output to the browser. Strangely, the output which is coming is being limited to a maximum of 80 charactres per line, no matter whatever i try! On a Linux box, its perfect, all's coming fine, but when i run this command on a BSD box, which is what i want, then the problem arises. After everything, i've come to the conclusion that its a permission thingy coz its only when apache tries to do the job, it happens. When i run the same program through shell or execute the command directly, everything is perfect. So i was wondering if i can call the program from browser, being some other user and not apache, so that it works around. Some kind of a sudo or something.. not sure.. Or any other workaround to this problem?? I've tried a host of command and options but to no avail :confused:

PS: I want to call the program directly from the browser, and all operations from executing the command to writing the file and then displaying it are done through it
Thanx a lot

FishMonger
09-07-2006, 09:09 AM
Why direct the output to a file then iterate over the file? Why not just use backticks to capture the output to an array?

Can you put your command into context for us by showing us your script?

nkrgupta
09-07-2006, 12:08 PM
Well ... tried that also..same output..


#!/usr/bin/perl

use strict;

print "content-type:text/html\n\n";

my @arr=`ps -axme`;

foreach (@arr) {
print $_."<br>";
}


Earlier i was trying the following:-

#!/usr/bin/perl

use strict;

print "content-type:text/html\n\n";

`ps -axme > /usr/local/apache/cgi-bin/process_list`;
open(W,"/usr/local/apache/cgi-bin/process_list");

while (<W>)
{
chomp;
$_=~s!<s!~s!g;
print $_."<br>";

}
close (W);


Thanks

FishMonger
09-07-2006, 06:08 PM
I doubt that the problem is related to permission settings for the apache user/group. It's more likely that there is a configuration directive in apache (or tomcat, or ?) that is limiting it to the 80 characters per line.

Do you have other BSD boxes that you can test to see if this issue occurs on each of them or just this 1 box?

Is the 80 characters per line problem only occurring when outputting to the browser? Do you see the same problem when you view the process_list file with cat or vim or emacs?

I don't have a BSD box, so I can't duplicate your problem, I can only suggest possible avenues to look into.

FishMonger
09-07-2006, 06:40 PM
Another thing you can try is to split the line into its seperate fields (I'd probably use an array slice to grab only the desired fields) and then ouput them to see if it has the same issue.

FishMonger
09-07-2006, 06:51 PM
Another test to try.

Use the backticks to put the ps output into the array, then when looping through the array, first output the length of each element instead of its contents then use Data::Dumper to output the contents of each element.

nkrgupta
09-07-2006, 08:11 PM
Hey FM,
Thanks a lot for you replies. As for your suggestions:
1) Yes i tried i on at least 5 different FreeBSD boxes versions ranging from 4.10 through 5.4! But the same result. 80 chars problem occurs whenever the file is created by the script called from a browser, whether or not displayed there. Even when i open the created file usin vim or cat, the result is th same, since the output has been limited, due to god knows what reason! When i use the shell to create the similar file using the same command, or for that matter, running the same perl program...bingo..all comes perfect!

2) Probably your second suggestion would not work for me great because i require all the fields to be shown.Nonetheless, the issue persists even after implementing it.

3) Though i doubt anything would change on doing what you said finally, i'll have a look into it first thing in the morning and get back with the results :)

Thanks again
Naveen

FishMonger
09-07-2006, 08:19 PM
A possible short term work-around. Create a cron job that runs the ps command every minute, dumping the results to a file, then have the web script read that file.

nkrgupta
09-07-2006, 08:48 PM
Hmm... that seems to be a good and probably the only solution right now. Will check out with the concerned guys whether it would suffice their requirements and/or not overburden the servers in any manner. I guess it (a cron job running evey minute) shouldn't.

But I need to know about the BSD stuff.... Not the first time it has sent me scratching my head!!:eek:

Thanks again FM:thumbsup:

FishMonger
09-07-2006, 09:08 PM
This may be a BSD specific issue. You might try posting a question at EE.

http://www.experts-exchange.com/Operating_Systems/Open_BSD/
http://www.experts-exchange.com/Operating_Systems/FreeBSD/