TekBytz

Closing telnet session without terminating the foreground process

by on Nov.04, 2008, under Unix

Finally have found out a way to terminate the telnet session with out killing the foreground process which is running. Anyone struck with the above situation can follow this 🙂

Assume a script “script1” is running in the foreground for a long time and this script needs to be retained even if the telnet session is closed. Here are the steps which needs to be followed.

  1. On the telnet screen press <CTRL + Z>. This will temporarily stop the script or process to run.
    $ ./script1.ksh
    [1] + Stopped (SIGTSTP)        ./script1.ksh
  2. Type in the command “bg” to run the process in background
    $ bg
    [1]     ./script1.ksh&
  3. Identify the session process ID. This can be done by giving the “ps” command in the prompt.
    $  ps
    PID      TTY   TIME  CMD
    4882522  pts/7  0:00 ps
    4984988  pts/7  0:00 -sh
  4. Identify the PID of the script which is is shifted to background. In our case “script1” This can be done by ps command piped with grep as below.
    $ ps -ef | grep script1.ksh
    user1 4399240 4984988   0 08:57:49  pts/7  0:00 grep script1.ksh
    user1 5029960 4829226   0 07:57:23      -  0:00 /usr/lpp/ars/bin/script1.ksh
  5. Now with all the PID’s collected, use “nohup” command
    $ nohup -p 4984988
    $ nohup -p 5029960

This will make the script or process not to be terminated even if the telnet session is closed.

NB: The telnet session process 4984988, will be running at the background unless it is killed.

Courtesy: Santhosh Fabian

:,

6 Comments for this entry

  • Julien CROUZET

    God man … We’re in 2008 who use telnet ???
    And you can do nohup command &

  • James

    The nohup with fedora does not support -p.

  • uygar

    If you use ‘ssh’ instead of telnet, you can use the ‘screen’ command for that. In order to start a script in the background use:

    $ screen
    //A new screen is created

    $ ./script.sh
    //Press Ctrl+A+D in order to detach the session, script still continues

    In order to monitor the script later on, use screen command with -ls and -r options, such that:

    To list all the screens:
    $ screen -ls

    To re-attach a previously started screen use:

    $ screen -r #screen-id

  • Frederick Tybalt

    @Julien,
    This holds good with SSH too..

    @uygar,
    Good… This too works…

  • karlberg74

    Hi,

    I know it’s been a long time since the last reply,

    Is there a way to put a script automatically in the background from the get go?

    Start the script (it is put automatically in the background) and then close the telnet session with nothing else to do?

    Thanks for your time.

  • Frederick Tybalt

    @karlberg74,
    This can be done directly, here we go

    $ nohup script1.ksh &

    This will directly run the script at background and the script will be alive even if the session is closed

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!