Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Forking an operative terminal

by perlgrim (Initiate)
on Dec 31, 2012 at 08:39 UTC ( #1011023=perlquestion: print w/ replies, xml ) Need Help??
perlgrim has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm struggling to fork/span a terminal from my script, a GTK2-perl single-window one, run there an OS command to get its output and then remain --the terminal-- alive and operative, accepting user input. The terminal/shell should become completely independent, no need to communicate with the parent process, that has to continue with its own life.

I'm working in Ubuntu/Debian boxes, and have tried tweaking xterm and gnome-terminal options, combined with exec, system, fork... but the best I get is a flashing process that goes away.

Help from their Wisdom would be appreciated.

Comment on Forking an operative terminal
Re: Forking an operative terminal
by Anonymous Monk on Dec 31, 2012 at 09:15 UTC

    I'm working in Ubuntu/Debian boxes, and have tried tweaking xterm and gnome-terminal options, combined with exec, system, fork... but the best I get is a flashing process that goes away.

    Then you need to find the correct invocation :) on win32 its  cmd /x /k the /k means remain open

      Yes, I've tried every combination, 'xterm -e' , 'gnome-terminal --execute' , 'gnome-terminal --command' ... and sequences including 'xterm -e somecommand; read -p' , "gnome-terminal --command 'somecommand; bash'"... , but can not find it out. Thanks.
Re: Forking an operative terminal
by zentara (Archbishop) on Dec 31, 2012 at 11:48 UTC
      xterm -e forAndExecXterm.sh
        Thanks both for the point; I had seen this thread before: system("xterm -e yourchatprogram") is OK for a program --like a chat client-- that remains running. But what I want is firing up the terminal, runnig a single command (like ls) that outputs to the terminal and ends, and then the shell/terminal staying alive and processing CLI input till the user close it explicitely. When the single command ends the terminal closes, comes it from system, fork/exec or backtics.
Re: Forking an operative terminal
by Anonymous Monk on Dec 31, 2012 at 15:42 UTC

    Maybe the -i switch (force interactive) to the shell helps (xterm -e "sh -i -c yourcommand"). If not, I'd put your command into a (temporary) .bashrc or similar. The third option might be starting a new shell after the command: fork() or exec("xterm", "-e", "sh -c 'somecommand; sh'");

    (Of course, you should find out the user's preferred shell instead of just using /bin/sh or similar)

      Hey! it works!

      The third option you suggested does the job, using as well sh or bash as xterm or gnome-terminal. I include the code:

      ... $tks->signal_connect( 'clicked' => sub { my $command = "tw pro:$currproj"; my $ret = fork(); if($ret == 0) { # this is the fork: become $command and go away. exec("gnome-terminal", "-e", "bash -c '$command; bash'"); } else { # this is the parent: do nothing and go ahead. } return FALSE; } );

      I detect zombie processes remaining in my ps -e:

      2504 ? 00:00:01 perl 2509 ? 00:00:01 gnome-terminal <defunct> 2573 ? 00:00:01 gnome-terminal <defunct> 2638 ? 00:00:01 gnome-terminal <defunct> 2702 ? 00:00:01 gnome-terminal <defunct> 2767 ? 00:00:00 gnome-terminal 2773 ? 00:00:00 gnome-pty-helpe
      that dissapear when the perl script comes to en end. Thank you very much, Anonymous Wisdom.

        You can get rid of the defunct processes by setting $SIG{CHLD} = 'IGNORE' in the parent. Also, you should use sh -c instead of bash -c in "bash -c '$command; bash'", since only sh is guaranteed to exist.

        And the user's shell can be found with this piece of code:

        my ($shell, $uid); while (($uid, $shell) = (getpwent)[2,8]) { last if $uid == $<; } say $shell;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-11-29 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (204 votes), past polls