You are not logged in.

#1 2016-05-04 14:59:46

raingloom
Member
Registered: 2015-06-17
Posts: 43

Start a long running command though SSH then leave

I have read at several places that tmux/screen can be used for this:
- connect to server through SSH
- start tmux/screen session
- execute some command that will run indefinitely or for a long time
- detach the session
- log out from SSH

I have tried detaching/attaching and it works, but as soon as I log out, my session is lost. According to reports this should work, so what am I doing wrong?

I'm using openssh on both the client and server and it is started by the sshd.socket systend unit on the server.

Alternatively, I tried Mosh, as it is advertised to be resistant to disconnections, but I'm getting a "nothing recieved from server".
I'm on a local home network with a simple router. As I understand, I don't need forwarding for local connections, right?

thx and love
-Rain

Offline

#2 2016-05-04 15:07:01

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,531
Website

Re: Start a long running command though SSH then leave

I do this all the time on many different servers.

There are two key points: 1.  How you logout, and 2. how you reconnect.

To log out, obviously you can't actually log out of your screen session.  The "cleanest" thing to do would be `tmux detach` or whatever the syntax is, then you can logoff the ssh connection if you want.  But normally, I just `killall ssh` from another terminal, or just shut my computer off which has the same effect.  You could also just close the local terminal window if this is a gui pseudoterminal at the client end.

When you reconnect you need to call tmux with a reattach flag.

Here's what I use in the bashrc on a couple servers I work on on a daily basis:

[[ $- != *i* ]] && return
[[ -z "$TMUX" ]] && exec tmux new-session -AD -s thehostname_tmux_myusername

Note I also force a detach from any other connections when I log in.  This isn't really necessary and might not always be wanted.  But it allows be to be a little careless without every having to worry about abandonded tmux sessions needing to be cleaned up: I'll only ever have at most one tmux session on each server.  If I connect from a new device, it will disconnect from any previous device I logged in on.

The name after "-s" is completely arbitrary, but I like to be able to identify what is where.  On any given day I'm running tmux on my local machine with several tabs open at least 2 or 3 of which are shelled into different remote system which are each in turn running tmux in which I'll have several tabs open on that server ... so informative session names help keep logs and error messages a lot easier to interpret.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-05-04 15:09:06

wudu
Member
Registered: 2010-03-08
Posts: 83

Re: Start a long running command though SSH then leave

Hi,

perhaps theres some deeper problem but let's sort out the easy ones (I had these when I was using screen the first time...): What key combination(s) do you use to detach? What command/options do you use to reattach?

Last edited by wudu (2016-05-04 15:09:46)

Offline

#4 2016-05-04 16:19:05

bstaletic
Member
Registered: 2014-02-02
Posts: 658

Re: Start a long running command though SSH then leave

Can't you detach using ^A d? That should be the default binding. Check the wiki for more details.

Offline

#5 2016-05-04 23:39:29

raingloom
Member
Registered: 2015-06-17
Posts: 43

Re: Start a long running command though SSH then leave

Edit because it was too long:

I use ctrl-b d or tmux detach and ctrld-d or logout. I've noticed that SSH sometimes hangs when I try to log out, but I can kill it with ctrl-c.

Last edited by raingloom (2016-05-06 01:48:24)

Offline

#6 2016-05-07 10:57:23

Elizine
Member
From: United Kingdom
Registered: 2015-10-07
Posts: 39
Website

Re: Start a long running command though SSH then leave

You should look for modern alternatives like tmux.

tmux is superior to screen for many reasons, here are just some examples:

Windows can be moved between session and even linked to multiple sessions
Windows can be split horizontally and vertically into panes
Support for UTF-8 and 256 colour terminals
Sessions can be controlled from the shell without the need to enter a session

Basic Functionality
To get the same functionality as explained in the answer recommending screen, you would need to do the following:

ssh into the remote machine
start tmux by typing tmux into the shell
start the process you want inside the started tmux session
leave/detach the tmux session by typing Ctrl+B and then D
You can now safely log off from the remote machine, your process will keep running inside tmux. When you come back again and want to check the status of your process you can use tmux attach to attach to your tmux session.

If you want to have multiple sessions running side-by-side, you should name each session using Ctrl-B and $. You can get a list of the currently running sessions using tmux list-sessions.

tmux can do much more advanced things than handle a single window in a single session.

Offline

#7 2016-05-07 11:34:24

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,531
Website

Re: Start a long running command though SSH then leave

Elizine, most of the discussion in this thread has been about tmux so I don't know why you feel the need to post what looks like an advertisement for tmux.  And while I tend to also prefer tmux, screen would also do this just fine too.  It sounds more like you are focused on tmux prostyletizing rather than helping the OP with their question.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2016-05-07 13:29:13

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Start a long running command though SSH then leave

Unlike it's competitors, tmux also alleviates several common ailments such as arthritis and psoriasis and may even cure some cancers.*

Get your bottles here! Experience a new level of remote server session satisfaction! 1 liter bottles of tmux for just $5 each! But 2 get 1 free! Limited time offer while supplies last!

*Disclaimer: no warranties apply. Possible side-effects include arthritis, psoriasis, cancer, dysentery, leprosy and death. All claims are false. Tmux sold separately. Dissatisfied customers should blame Allan.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#9 2016-05-07 13:34:46

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Start a long running command though SSH then leave

Xyne - ha!
@op - if you want to try tmux, I have a minimal config on my github link I my sig.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2016-05-09 20:18:17

raingloom
Member
Registered: 2015-06-17
Posts: 43

Re: Start a long running command though SSH then leave

@graysky thanks but I am already using tmux, I guess I didn't make that clear :3
I can't do further testing as I successfully messed up my laptop's bootloaded, but that's a different issue. I'll report back when I can get it running again.

Offline

#11 2016-05-11 22:44:10

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Start a long running command though SSH then leave

Have you read/tried this: http://unix.stackexchange.com/questions … g-from-ssh

First search hit and so similar that I had to check if it was a cross-post.

Btw, if you have physical access to the server, monitor processes and the systemd log via the terminal to see what happens when you connect and disconnect from ssh.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB