How to exectute a Mathematica Script through ssh - ssh

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.

Related

When using ssh -X to connect to remote Ubuntu 18.04, couldn't it show server's local terminal GUI on my own PC?

I found a strange thing when using ssh -X to connect to two servers.
Ubuntu16.04 is installed on one server, while Ubuntu 18.04 is installed on the other. I upload a simple shell script as following to both servers:
#!/bin/bash
tab=" --tab"
options=()
cmds[1]="echo Banana"
cmds[2]="echo Cat"
for i in 1 2; do
options+=($tab -e "bash -c '${cmds[i]} ; bash'" )
done
gnome-terminal "${options[#]}"
exit 0
You know this script will open a new terminal window on server with two tabs. One tab will print "Banana", and the other tab will print "Cat".
Then I using ssh -X to remotely login into the first server (Ubuntu 16.04) and execute this script on server, I got a new terminal opened with two tabs on my pc. That is I saw the remote terminal GUI of server on my own computer. However, if I repeated these steps on the second server(Ubuntu18.04), I will got nothing to show on my computer.
The I execute commandgedit on the ssh login terminal when using ssh -X to login into both servers, I could see the GUI on my computer for both the cases.
I guess Ubuntu 18.04 cannot transport terminal's GUI through ssh -X to my pc. I was very confused about this. Could you please to explain the reason? And BTW, how can I see terminal's GUI of server in this case? Thanks a lot!
ssh -X doesn't transport a terminal. It tunnels a X11 connection.
X11 in its core is a network based display protocol. The programs (X clients) connect to a X server (a program running on your local machine) and instruct it to create windows and draw stuff to it. There is no graphical rendition of any kind anywhere else than the X server!
ssh -X does not operate like Windows Remote Desktop, where there is an actual graphics environment running on the remote machine. Hence if you create two separate ssh -X connections (maybe even from different machines), these connections are completely independent from each other. Hence you cannot use ssh -X to connect with a preexisting X session!
What you want is either Xvnc or Xpra, where the graphics environment is actually run on the remote machine, and only the output is transferred to your local machine.

Google Compute Engine instance restarts when I ssh

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.

SSH from one computer, continue on another?

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.

Script for connecting to remote server (via another desktop) from my laptop using cygwin

I need to connect to my university server (S) from home using my laptop (L). Since I am off campus (and my VPN does not work for some reason), I am required to first login (SSH) to my desktop (D) at the university, and then connect to S (since the server only accepts connections from computers on the campus network).
I am using Cygwin on Windows 8. I would like to know how I can create a script to auotmate this process - currently I have to manually SSH from L to D, and then again from D to S. I am new to unix.
Any help will be appreciated. Thanks.
--- Edit ---
Specifically, I would like to know how I can automate this process so that I don't have to enter my password every time.
ssh accepts a command to execute at the remote host after connecting. You can use that to launch a second ssh session:
ssh -t D ssh S
You'll be prompted for your desktop password first, then for your server password.
By the way, I recommend looking into GNU screen if you're not already using it. It prevents losing any work in case your SSH connection drops out.
To automate this even more, stick it in a bash file called "connect-university.sh":
#!/bin/bash
ssh -t D ssh S
You can then run that file from the Cygwin commandline via:
./connect-univiersity.sh
Note that the ./ part is essential, as Cygwin doesn't usually look for executable files in the current directory for security reasons.

How to run a CPU Hogging Program on Remote Server?

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.