Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

system commands and returns

by perl_zen (Initiate)
on Nov 29, 2011 at 17:13 UTC ( #940661=perlquestion: print w/replies, xml ) Need Help??
perl_zen has asked for the wisdom of the Perl Monks concerning the following question:

I have the following perl issue:

I am running a script that is executed by my regular user. I need to “su” to other users throughout the execution of this script, which will require me to enter some information from the UNIX command line. When I “su”, I am prompted for the password, I enter the password, the password is accepted (& updates to the other user) but the scripts returns to the UNIX prompt rather than continuing with the remainder of the script.

Linux 4.1.2

Perl 5.8.8

What am I doing wrong here? Here is my code sample:

&Chg_User ($other_user); &Chg_Directory ($dir_location); … … … ## the sub_routines below sub Chg_User { my @chg_args = ("su”, “$_[0]"); my $chg_result = system(@chg_args); # updates to user but returns + to the UNIX command prompt print ("cOmMaNd LiNe \n"); #never gets here unless I type “exi +t” at the UNIX command prompt if ( $chg_result == -1) { print "wrong password: $!\n"; exit (-1); } } sub Chg_Directory { system (“cd”, “$_[0]”); }

Replies are listed 'Best First'.
Re: system commands and returns
by chromatic (Archbishop) on Nov 29, 2011 at 19:12 UTC

    system runs a separate process, so you're spawning a new process, running su in that, and then exiting. I think you need to stay in the process once you've switched users. Perhaps something like IPC::ShellCmd::Sudo (I haven't used it) would help?

    Improve your skills with Modern Perl: the free book.


      i think this would cause me to save the password in a file, correct? i would like to avoid this option, if at all possible

        I don't see why; it's not obvious reading the module's code that that's necessary, and in any case you could always prompt for it in your program, right?

        Improve your skills with Modern Perl: the free book.

Re: system commands and returns
by cdarke (Prior) on Nov 30, 2011 at 08:34 UTC
    Split your script into two. One part is the launcher and the other is the code to run under the second user. Use something like this:
    system("su -c ./");

    Update: By the way, your sub Chg_Directory suffers from a similar problem, it will only change the directory of the child process. Use chdir.


      I will test this out after a morning meeting and give an update on success or failure later today.

      BTW, I noticed that problem with  sub Chg_Directory last night while attempting more testing.


        This worked and thank you. I had to make two small changes (noted in the code below) but I have success.

        The first change was enclosing the script execution command in single quotes and the second was adding perl to the command call.

        The end result is two perl scripts.

        The orignal script (with updates)

        &Chg_User ($other_user); #&Chg_Directory ($dir_location); … … … ## the sub_routines below sub Chg_User { #system("su -c ./"); #from cdarke system("su -c 'perl'"); #with changes #if ( $chg_result == -1) { # print "wrong password: $!\n"; # exit (-1); #} } sub Chg_Directory { system (“cd”, “$_[0]”); }

        and (with the script for the "other_user" shell)

        print ("Not the cOmMaNd LiNe from script2 \n");

        I would like to get the IPC::ShellCmd package to see if this will eliminate the need of a second script file; however, this takes care of my needs for the day and moves me closer to my goal

        Thanks to all of you.

Re: system commands and returns
by JavaFan (Canon) on Nov 30, 2011 at 00:12 UTC
    It doesn't "return" to the UNIX command prompt. I does exactly what you ask it to do: start up a *new* shell, as the user passed in as $_[0]. Typing exit causes this new shell to exit, which causes the system to terminate, which causes the next command to run.

    If you want to run commands as the other user, pass them as an argument to su.


      Thank you for this explanation. Being new to Perl, it is very helpful.

      I have several commands that I would like to execute as  other_user , so I would like that particular shell to execute more than one command. As in the sample code, I would like to su to the user and print "cOmMaNd LiNe" without any external interaction. Needing to type "exit" from the command line (in order to get "cOmMaNd LiNe" to print) prohibits this ability.

      I think your explanation and the example given by cdarke both look promising.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://940661]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2017-02-24 16:13 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (356 votes). Check out past polls.