G'day ox1d0,

Welcome to the monastery.

You need some basic debugging here.

If

system ("su -l - $user -c \'$action > DBlist.txt && cat DBlist.txt\'") +;

doesn't work, but

system ("su -l - MyRealuser -c \'$action > DBlist.txt && cat DBlist.tx +t\'");

does work, then the only difference between them is $user and MyRealuser.

Use a print statement like this:

print ">>>$user<<<\n";

Do you get:

>>>some_username<<<

or:

>>>some_username <<<

If the former, replace MyRealuser with some_username (in the system() command that worked), and see if it still works. If not, see system to learn about checking for errors. Maybe there's a problem with some_username's account or privileges.

If the latter, the newline after $user may be the culprit. You can fix that with:

chomp(my $user = $line);

I also note that you're using the same $line variable when reading from the EXEC and $cmd filehandles. Unless there's some specific reason for doing this, I'd use different variables, e.g.

while(my $exec_line = <EXEC>) { ... while (my $cmd_line = <$cmd>) { ... } }

Even if using the same variable isn't causing any problems now, it could well turn into a hard-to-track-down bug in the future following some (seemingly innocuous) modification.

-- Ken


In reply to Re: run shell command as another user by kcott
in thread run shell command as another user by ox1d0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.