1

I have a script that runs an executable with 1000 different parameters (one per execution). This can take many hours. I run this in the lab computer. Now, I do this via ssh, from my home computer. Can I do an ssh, start executing the script, logout, but somehow allow the script to continue executing?

I mean the lab pc will still be open and I do not care about the stdout of the program. The interesting results will be written into a file, which I can check when I login again.

I tried to do so, but the script will stop executing as soon as I logout.

9
  • 2
    Pretty good explanation in How to keep processes running after ending ssh session?.
    – fedorqui
    Commented Jul 31, 2015 at 15:33
  • 1
    Damn it, I checked for duplicates when I typed in the question. Maybe an improvement of this feature would be to check on other relevant sites too, like askubuntu. I am now reading the link @fedorqui
    – gsamaras
    Commented Jul 31, 2015 at 15:36
  • 2
    I completely agree on this. We have multiple duplicates over sites, that's why I always end up using google over SE search.
    – fedorqui
    Commented Jul 31, 2015 at 15:41
  • 1
    I strongly advise you to do so. In fact, some time ago I commented this in meta askubuntu, but never went back to make it more visible. If you feel like, go ahead!
    – fedorqui
    Commented Jul 31, 2015 at 15:49
  • 1
    @fedorqui, I asked on meta: meta.stackoverflow.com/questions/300598/…
    – gsamaras
    Commented Aug 1, 2015 at 13:19

2 Answers 2

6

Since I can't mark the question as a duplicate of this, since "The duplicate question must exist on Stack Overflow" and because I want to share my experience, I will answer my own question.

tmux and screen are the best answers in the duplicate. However, the first was not recognized in my Debian (probably need installing), while the later is ready to go and works fine, for this simple scenario:

  • ssh into your remote box. Type screen Then start the process you want.

  • Press Ctrl-A then Ctrl-D. This will "detach" your screen session but leave your processes running. You can now log out of the remote box.

  • If you want to come back later, log on again and type screen -r This will "resume" your screen session, and you can see the output of your process.

Also this comment seems good: "I will usually name my screen sessions using screen -S name to make it easier to connect to the correct one later. "


As deviantfan put it, " tmux needs installing. A simple apt-get install tmux as root is enough.".

3
  • Yes, tmux needs installing. A simple apt-get install tmux as root is enough.
    – deviantfan
    Commented Jul 31, 2015 at 16:08
  • Thank you @deviantfan, I updated my answer.
    – gsamaras
    Commented Aug 1, 2015 at 13:05
  • I never name my screen sessions. I only ever have one per machine (per account). So I just log in and screen -dR. Although I recommend spending your time learning/customizing tmux, since it's newer and better. Commented Aug 4, 2015 at 8:34
1

I assume when you tried, you put it in the background with myscript &, and then logged out. Some GNU/Linux distros default to sending a SIGHUP to all processes that have the session tty as their tty (or some other way of finding processes from a login sessions, like a tree of parent PIDs, I forget.)

So, a solution is:

nohup myscript &

It's really that easy. When run under nohup, you process will ignore the hang-up signal. nohup redirects stdin from /dev/null if it was from the terminal, and stdout/stderr to nohup.out. Then you read nohup.out, which will be created in the directory from which you called your script.

tmux is really nice, and I use it all the time, but it's not actually needed for this. (I actually use screen, but that's only because tmux didn't exist when I started using screen, and switching would incur some re-learning overhead.)

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.