I am about to run a machine learning program which might take a day or two to complete; I do not want to run it on my laptop, but on a remote server. Now I am thinking if I ssh into the machine and run the program there, and close the ssh session, how will I know next time I ssh into the machine if the program is still running or completed?
Use screen instead. Assuming you have it installed, just run screen from the ssh session. You will be given a new shell.
Once you start the program you can detach the session from your terminal by typing ^a d (CTRL+a followed by d).
Later, when you ssh back in, run screen -r to reattach the session to your current terminal.
(Note that just plain killing off the ssh session will in fact also detach the screen session, not kill it.)
man screen for further reading. screen is very powerful, and learning the ^a action sequences would be well worth your time. I use screen daily and love it.
Related
I have a deep learning vm setup on google cloud platform. I ssh into by clicking the ssh button in my list of vm instances. The problem I am having is that if I run a program and close the ssh window while it is still running, when I ssh back into the vm, the program has been interrupted and the whole vm essentially has restarted.
Is there a way to setup my vm so that I can run a program and then close the ssh connection without it restarting the vm? I want to be able to run programs without being required to leave the ssh window open and my computer on.
As #Rup pointed out, processes belonging to your SSH session's shell will get a hang-up signal when you close the connection. To bypass the hang-up signal and let a program continue running, use the nohup command. Here is an example
nohup python test.py &
That will run the program test.py and as well as ignore the hang-up signal. The programs output will be stored in a nohup.out file.
I have a home server running Ubuntu Server 14.04 and I've setup OpenSSH on it. But. I'm intending to run some terminal based stuff that takes long time to be finished.
So what I wanna be able to do is ssh into the server, start the process, log out of the computer i connected with and connect from another device and have that process I started still running.
Almost like a remote-desktop type SSH connection.
If this is not possible then I'll just install a desktop environment and vnc server.
Try nohup & command.
This way the shell should keep the process running, even if you log out.
One option is the program tmux or screen, which you can detach from and log out.
Instructions for tmux:
$ ssh me#server
$ tmux
run commands
Ctrl-B d (this
means push CTRL-B then push d to detach from tmux) logout
If you want to see the results, use tmux attach when you log back in. Your session will be exactly as you left it.
I am currently implementing a web server on a raspberry py using the web.py framework. For convenience, I am using ssh, so I can perform tests on the raspberry directly from my laptop.
My problem is that I can't close the ssh session without the web server stopping to work, because when the session closes, a logout is performed automatically.
Does anyone know a possibility to avoid the logout while closing the ssh session? I am using Linux on both my laptop and the raspberry. Thank you.
The best way to do this is by using screen. It lets you "multiplex" your command line, meaning you can run multiple commands at the same time. Also, when you exit your ssh session, the commands running in screen will continue to run, and it is very easy to install as well.
Here's a guide for installing and running it, and if you have any other questions, comment or google for more screen guides
Edit in 2022: These days many people prefer tmux to screen. My personal recommendation would be tmux, but both do the trick!
I have raspLite on the raspberry pi 3 model b and this is how i used screen in the bash enviroment.
ssh pi#hostname
apt-get install screen
screen
Then I press Enter to proceed through the License Agreement.
npm start
Then I close the terminal and the webserver continues untill i use the SIGINT kill command on the pid.
here is my problem: i would like to run a Mathematica script through ssh on a remote machine so that i can close the terminal on my computer and keep it running on the remote one.
My problem arises because the script acts in interacting mode, and so when i close the terminal the process is shut down too.
Thanks.
Use tmux or GNU screen.
Workflow:
ssh into remote machine
start tmux/screen, e.g. tmux or screen
start Mathematica script inside tmux/screen session
detach tmux/screen session, e.g. Ctrl+B d (tmux) or Ctrl+A d (screen)
close ssh connection
Then later:
ssh into remote machine
reattach to tmux/screen session, e.g. tmux attach or screen -d -R
view completed Mathematica script output
Several cases:
If you don't need to interact with it or need to visualize the notebook during evaluation
Log in to the machine with ssh
Then, to run a kernel in the background and detach it from the current session, use nohup tool (the standard output of the command will be dumped to myNotebook.out):
nohup math < myNotebook.nb > myNotebook.out &
At this point, the ssh session can be closed without killing Mathematica
Optionally you can monitor mathcommand output with the tail command (use CTRL-C to exit the tail monitoring)
tail -f myNotebook.out
If you need to see what's going on, visualize graphs during calculation or to be able to interact graphically, use remote desktop (vnc) and tunnel your communication with remote machine. Details depends a bit on the Linux distribution (vnc clients & servers may differ). You can even from Windows or Mac connect with remote desktop to your linux box and manipulate it. I suggest you to search the web for remote desktop ssh tunnel + your distro for tutorials.
background:
I use a linux box(call it host) everyday. But sometimes, the GUI/mouse/keyboard stop working and it(host) hangs. I was not able to do anything but restart it(host). So, now i always a ssh server running on it(host) so that I can connect to it and turn off things safely. I do this by using VNC client on my laptop(call it REMOTE). It replicates the whole screen from the host, so even if my mouse/keyboard on the host don't work, i could close things with REMOTE. But VNC is expensive on network usage and slow. I want to replace it with normal ssh connection.
problem redefined:
So, if I start a simple program to print "hello world" endlessly on terminal on the "host". How can i see the same terminal(with hello world printing) on "REMOTE" over SSH. Not just see, control this program say stop it, kill it etc.