0

I'm connected to a server through ssh and want to run a process that will take a long time. I connect to the server by using ssh in my laptops terminal, but I want to be able to turn my laptop off but still have the progress running on the server. Since they're two separate computers, it seems like I should be able to do this, but I'm not sure if it's possible through ssh.

2 Answers2

1

It is quite simple, add a & after your command say for example

bash script.sh &

It will continue its execution even if you quit the session or close the terminal.
Another way is use nohup command and execute your command. For example

nohup bash script.sh

Find more using man nohup and follow this question which has similar direction.

  • See also gnu screen, tmux, and vnc. They allow you to to keep a session open while disconnected. – ctrl-alt-delor Apr 04 '20 at 19:44
  • @ctrl-alt-delor I will look into those, thank you! – James Ronald Apr 04 '20 at 19:46
  • @iamhrishikesh Thank you!! So I tried what you recommended and I think it's working, but I can't see the output of the process I ran. After I add the & and run the command, the process seems to just disappear. I know it ran successfully, but the process also prints output to the terminal which I'd like to see - is this possible? – James Ronald Apr 04 '20 at 19:47
  • 1
    @iamhrishikesh's first command "bash script.sh &" will place script.sh in the background but will terminate if the user logs out or disconnects. The second command "nohup bash script.sh" will survive the user disconnecting, but should have the '&' at the end to place it in the background: "nohup bash script.sh &" – L.Ray Apr 04 '20 at 23:53
1

Background jobs

To run in background

program &

To run in background and continue after disconnecting. Also redirects stdout to ~/nohup.out.

nohup program &

Same but redirect to another file

nohup program > "another file" &

Note for above commands, you can see the background jobs with the jobs command.

Same but remove from jobs list

nohup program > "another file" & disown

Persistent sessions

See gnu screen, tmux, and vnc. They allow you to to keep a session open while disconnected.

Gnu screen

#create a screen session (do this once)
screen -d -m

#attach to the session screen -x

You can also have more than one (named sessions)

#create a screen session (do this once)
screen -d -m -S a_name

#attach to the session screen -x -S a_name

Beware that ctrl_a is used to control screen, when inside. ctrl_a ctrl_a to pass ctrl_a to the shell.

VNC

#create a vnc session (do this once)
vncserver -from 127.1.0.1 -geometry 1920x1000 :10

#attach to the session (from a remote machine) ssvnc Vnc+ssh://«user»@«machine-name»:10

Change the geometry to fix your display, maybe change the display number.

Notes:

Anyone on the local machine can connect to the VNC session.

You can add -SecurityTypes None to turn off VNC security, if you have enough security via ssh.

127.1.0.1 is in the loopback range. Only processes on the local machine can connect. You can connect remotely via ssh (ssvnc). You can use nftables to restrict which users can connect to 127.1.0.1. There are many addresses in the loopback range 127.0.0.1 to 127.255.255.254 (about 16 million), so you can allocate one to each user, and still have spare.