How to run program on Unix server and then close SSH?

0

I need to run a long operation in MATLAB on a remote Unix server. But I know I won't be able to stay logged in via SSH the whole time. I am root/admin if that helps.

How can I do this without losing my work?

What I'm currently doing is opening MATLAB and then doing something like the following:

t=cputime; x=A\b; time=cputime-t; save('workspace.mat')

While this is running (x=A\b is the operation taking all the time) I close my SSH shell which was running MATLAB, and the operation doesn't continue to run. So I don't get the results.

How can I accomplish running this even when I logout of SSH midway? Thanks.

JDS

Posted 2014-01-17T17:34:53.217

Reputation: 37

Question was closed 2014-01-21T17:11:35.790

No that's not what I asked. I'm not interested in reloading a session state, I'm interested in having my program run while I'm logged off. – JDS – 2014-01-17T17:57:27.530

Answers

4

You either need to push the command to the background using a trailing &

Or, better still, install SCREEN and maybe BYOBU. Then you can have a persistent session that you can go back and check without having to stay logged in permanently.

Install SCREEN and BYOBU on the server and configure as desired. After you have started your first session, you will be able to reconnect to it should you end and restart your SSH connection. There are all sorts of clever things you can do to automate sessions, have multiple windows, multiple terminals and more. This article on StackOverflow has some ideas on automating connections.

Julian Knight

Posted 2014-01-17T17:34:53.217

Reputation: 13 389

If I can fork it it'll still run even when I'm logged off SSH? – JDS – 2014-01-17T17:41:47.743

SCREEN basically allows the terminal to continue running without your SSH connection so it should indeed keep going quite happily just as though you were logged in. BYOBU makes SCREEN much nicer to work with as SCREEN is not the most intuitive of user interfaces. – Julian Knight – 2014-01-17T17:43:54.140

+1 for screen. It brings tabbing and persistence to remote sessions. EDIT: actually, byobu looks really slick too! – thirtythreeforty – 2014-01-17T17:45:07.817

Cheers @thirtythreeforty. Yes BYOBU is great, stumbled on it recently when messing with the Raspberry Pi and now have it on several machines and servers. – Julian Knight – 2014-01-17T17:50:55.200

I'm confused - do I install SCREEN on the server machine? Then what? – JDS – 2014-01-17T17:56:54.030

2

Hi, yes, install screen on the server. When you SSH in, your first command would simply be screen to start a new session. Future sessions can be reconnected to. There is a more comprehensive answer here as there are lots of options for making life easier with remote sessions.

– Julian Knight – 2014-01-17T18:02:06.060

I couldn't install SCREEN, any other alternatives? – JDS – 2014-01-17T18:33:57.767

What version of UNIX is it? Are you able to compile for the server? The main alternatives are tmux and Dvtm with Dtach – Julian Knight – 2014-01-17T18:37:57.883