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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    Pretty good explanation in [How to keep processes running after ending ssh session?](http://askubuntu.com/q/8653/143251). – fedorqui Jul 31 '15 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 Jul 31 '15 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 Jul 31 '15 at 15:41
  • Do you think I should post on meta about this @fedorqui? I also answered my question, but it you think it's should be deleted, please let me know. – gsamaras Jul 31 '15 at 15:47
  • 1
    I strongly advise you to do so. In fact, some time ago [I commented this](http://meta.askubuntu.com/questions/13807/are-bash-shell-scripting-questions-on-topic/13808#comment23874_13808) in meta askubuntu, but never went back to make it more visible. If you feel like, go ahead! – fedorqui Jul 31 '15 at 15:49
  • 1
    @fedorqui, I asked on meta: http://meta.stackoverflow.com/questions/300598/search-for-duplicates-in-relevant-sites – gsamaras Aug 01 '15 at 13:19
  • This belongs on Unix & Linux (http://linux.stackexchange.com/), or Superuser. I'll say more on your meta post. – Peter Cordes Aug 04 '15 at 08:24
  • @PeterCordes I see your point (and in meta) and I agree, thanks for the comment. – gsamaras Aug 04 '15 at 12:39
  • Asked and answered *ad nauseam*. Possible duplicate of [How to make a programme continue to run after log out from ssh?](https://stackoverflow.com/q/954302/608639), [How to prevent a background process from being stopped after closing SSH client in Linux](https://stackoverflow.com/q/285015/608639), [How to keep processes running after ending ssh session?](https://askubuntu.com/q/8653), etc. – jww Jul 31 '19 at 19:52

2 Answers2

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.".

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Yes, tmux needs installing. A simple `apt-get install tmux` as root is enough. – deviantfan Jul 31 '15 at 16:08
  • Thank you @deviantfan, I updated my answer. – gsamaras Aug 01 '15 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. – Peter Cordes Aug 04 '15 at 08: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.)

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847