How to start an app within a SSH session and have it continue to run if I leave?

Discussion in '*nix Software' started by Red Squirrel, Jan 21, 2013.

  1. Red Squirrel

    Red Squirrel Lifer

    Joined:
    May 24, 2003
    Messages:
    36,971
    Likes Received:
    636
    If I do something like:

    ./appname &

    It runs in the background. yet, if I close the SSH console I ran that from, the app or script terminates. How do I make it so it does not terminate?

    I had the impression it already worked that way, but it just so happened that the console I've been doing this stuff from always stayed open on my server. I accidentally closed it, and all hell broke loose. I can't seem to get everything started again without having to keep a SSH session active somewhere or restarting the whole server (all that stuff is in the rc.local file). Is there a way to do this?
     
  2. mundane

    mundane Diamond Member

    Joined:
    Jun 7, 2002
    Messages:
    5,550
    Likes Received:
    0
    ..
     
    #2 mundane, Jan 22, 2013
    Last edited: Jan 31, 2013
  3. Jodell88

    Jodell88 Diamond Member

    Joined:
    Jan 29, 2007
    Messages:
    9,451
    Likes Received:
    27
    I would go with tmux over screen, but the same principle is behind the software.
     
  4. Ken g6

    Ken g6 Programming Moderator, Elite Member
    Moderator

    Joined:
    Dec 11, 1999
    Messages:
    11,914
    Likes Received:
    75
    I think you guys might be over-thinking it. It sounds like he just needs "nohup".
     
  5. Jodell88

    Jodell88 Diamond Member

    Joined:
    Jan 29, 2007
    Messages:
    9,451
    Likes Received:
    27
    I never heard of this command. :thumbsup:
     
  6. mv2devnull

    mv2devnull Senior member

    Joined:
    Apr 13, 2010
    Messages:
    954
    Likes Received:
    1
    "at now" is an alternative with more control on redirects.
     
  7. Red Squirrel

    Red Squirrel Lifer

    Joined:
    May 24, 2003
    Messages:
    36,971
    Likes Received:
    636
    Interesting, I will give nohup that a try, thanks! Also forgot about the at command, that would work too.

    Also through further experimenting I noticed if I do it with & and exit the console gracefully by typing exit, I'm ok. It's if I just X out of it.
     
  8. Nothinman

    Nothinman Elite Member

    Joined:
    Sep 14, 2001
    Messages:
    30,672
    Likes Received:
    0
    With that you lose control of the process, with screen or tmux you can resume the session and still interact with it if necessary.
     
  9. _Rick_

    _Rick_ Diamond Member

    Joined:
    Apr 20, 2012
    Messages:
    3,647
    Likes Received:
    1
    If you're an admin, you can also use the service/daemon frame work of your distribution.
     
  10. TheRyuu

    TheRyuu Diamond Member

    Joined:
    Dec 3, 2005
    Messages:
    5,406
    Likes Received:
    6
    I like tmux. Switched from screen, you can do a little more than screen but the main thing is the configuration is likely going to be far easier.

    Code:
    [SIZE="1"]set-option -g prefix C-a
    bind-key C-a last-window
    bind-key a send-prefix
    
    set -g default-terminal "screen-256color"
    
    set -g status-bg black
    set -g status-fg white
    set -g status-left-length 25
    set -g status-right-length 50
    set -g status-left '#[fg=green,dim][ #[fg=green,bold,nodim]#(whoami)#[fg=green,dim,nobold]@#[fg=green,bold,nodim]#H #[fg=green,dim,nobold]] #[default]'
    set -g status-right '#[fg=green,dim][ #[fg=green,bold,nodim]#(cut -d " " -f 1-3 /proc/loadavg) #[fg=green,dim,nobold]] [ #[fg=green,bold,nodim]%Y-%m-%d %I:%M %p #[fg=green,dim,nobold]]'
    set -g base-index 1
    
    set-window-option -g utf8 on
    set-window-option -g window-status-current-fg white
    set-window-option -g window-status-current-format '#[fg=red,bold](#[fg=white,bold]#I:#W#F#[fg=red,bold])'
    set-window-option -g window-status-fg green
    set-window-option -g window-status-format '#I:#W#F'[/SIZE]
    Code:
    tmux -u new
    Gets you a new tmux session.
    Ctrl-A + D detaches you, then you can leave the ssh session.
    Code:
    tmux attach
    Should be self explanatory :p
     
    #10 TheRyuu, Jan 24, 2013
    Last edited: Jan 24, 2013
Loading...