Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

In Linux how to kill the Prompt

by karthik.raju (Acolyte)
on Feb 19, 2015 at 05:17 UTC ( [id://1117184]=perlquestion: print w/replies, xml ) Need Help??

karthik.raju has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

In Linux once the Perl script get complete of its execution, then how to close that current command prompt ??

and also with the below command, i'm not able to print "Server started" even after the server get started.

system("ssh buildteam\@xx.xx.xx.xx sh /a/bbb/JBoss7/jboss-as-7.1.1.Fin +al/bin/standalone.sh -b 0.0.0.0 &"); print "Server Started";
Please help.

Thanks, Karthik

Replies are listed 'Best First'.
Re: In Linux how to kill the Prompt
by jmacloue (Beadle) on Feb 19, 2015 at 12:10 UTC

    Okay, it does not seem that you actually need to kill anything here, and it's not about any prompts. Prior to installing any modules let's try to understand what do you want first, and then why your code does not work as you want it to.

    The task seems to be to launch a JBoss server on a remote machine via SSH, isn't it? As far as I understand, you took sh /a/bbb/JBoss7/jboss-as-7.1.1.Final/bin/standalone.sh -b 0.0.0.0 & from a manual. When you, logged in to xx.xx.xx.xx as buildteam user, run it interactively — the JBoss server is started in the background, and you get your command prompt back.

    What happens if we try to run ssh buildteam@xx.xx.xx.xx sh ... & interactively from your workstation? Yep, JBoss server is started in the background, but the devil is in the details — as & sends to the background not only a standalone.sh script but your interactive SSH session instead. It is hardly what you want because if the SSH session gets terminated (e.g., you turned off your workstation or there is a network disruption) so will your server, and you want your JBoss to run independently.

    The magic & is a shell instruction. So, when shell parses your command and encounters an ampersand it launches the process in the background (showing something like [1] 9529 among other things, where 1 is a shell job ID and 9529 is the actual PID). The problem is, what shell gets to interpret this &? You need to escape it, with a backslash or shell quotes, if you want to pass it as it is to the remote shell. In this case I'd prefer quotes to improve readability:

    ssh buildteam@xx.xx.xx.xx "sh .../standalone.sh ... &"

    Then, back to our beloved Perl and its system() call. What happens if you put & in its argument? It's a shell metacharacter so system("... &") will invoke a shell to run the command. As it happens when you run the command interactively, the SSH process is sent to background, and you need the same quoting mechanism to send standalone.sh to background instead. So, to make our code pretty let's use cool q{} Perl operator (beware: it does not interpolate $vars in the string, just like '' operator so use qq{} if you need any substitutions, but qq{} will need \@remote again):

    system(q{ssh buildteam@xx.xx.xx.xx "sh .../standalone.sh ... &"});

    And, as a finishing touch, let's add &>/dev/null shell instruction which will get rid of all the output from standalone.sh (we don't care so why litter our terminal?):

    system(q{ssh buildteam@xx.xx.xx.xx "sh .../standalone.sh ... &>/dev/nu +ll &"});

    So, to sum up: system() launches shell, shell parses the command in q{...}, doesn't see & there as instruction, and launches ssh, which launches sh on the remote server xx.xx.xx.xx as a buildteam user, which parses the command in "...", sees & there and, finally, sends .../standalone.sh -b 0.0.0.0 to background on xx.xx.xx.xx machine. Then the control gets back to Perl. Just UNIX things, eh?

    PS However the thing which baffles me is why you don't get your Server started message. As you didn't check system() call result the execution must continue regardless any errors, where is the message then? I don't have JBoss around but checked similar code on my workstation — I get the message every time. Is it use autodie;, or maybe you just loose Server started in the standalone.sh output? Nevertheless the rest of my babble still stands.

Re: In Linux how to kill the Prompt
by vinoth.ree (Monsignor) on Feb 19, 2015 at 05:26 UTC

    Hi karthik.raju,

    In Linux once the Perl script get complete of its execution, then how to close that current command prompt

    Which command prompt? can you explain your question in detail so that we can help you better!

    Update:

    For your system command issue try below

    Append this string to your command:  >&- 2>&- <&-
    >&- means close stdout
    2>&- means close stderr
    <&- means close stdin

    system("ssh buildteam\@xx.xx.xx.xx 'sh /a/bbb/JBoss7/jboss-as-7.1.1.Final/bin/standalone.sh -b 0.0.0.0 > &- 2>&- <&- &'");

    SSH connects stdin, stdout and stderr of the remote shell to your local terminal, so you can interact with the command that's running on the remote side. As a side effect, it will keep running until these connections have been closed, that's why your main program does not print the string after system() call.


    All is well
      Hi Vinoth,

      I want close the Prompt on which the perl is running,

      my intention is, my perl script has to start and run the Jboss server in the background.

      once it get started i need to close the command prompt that is opened for this perl script.

        Then use exec when calling your perl script like this: exec your_script.perl as suggested here.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

      In Linux once the Perl script get complete of its execution, then how to close that current command prompt

        In Linux once the Perl script get complete of its execution, then how to close that current command prompt

        No use repeating your question, please read my answer here or there.

        You may understand that Perl, when it has already finished, cannot do any more for you, how unsatisfying that is.

        That is why you add the unix/linux exec command to your call to the perl script, when you start it.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

        In Linux once the Perl script get complete of its execution, then how to close that current command prompt(a paragraph)

Re: In Linux how to kill the Prompt
by Happy-the-monk (Canon) on Feb 19, 2015 at 07:54 UTC

    ... how to close that current command prompt?

    Prepend the call to your Perl programme with exec, and perl will take over the linux process that was your shell.
    After completion, there'll be no shell prompt to return to.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: In Linux how to kill the Prompt
by atcroft (Abbot) on Feb 19, 2015 at 06:25 UTC

    It sounds like what you are wanting may be to use Expect or Net::SSH::Expect to log in via ssh into the other machine and kick off the process. Both of the modules mentioned can be used to write code to log into a remote system and execute commands.

    Hope that helps.

      Is it possible to run command as background job on remote machine using this module?

      I can see its possible using Net::OpenSSH which is documented as, Running detached remote processes

        $ssh->system("nohup $long_running_command &");

      All is well. I learn by answering your questions...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1117184]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-24 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found