Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continue executing a program, after end of ssh [duplicate]

Tags:

linux

ssh

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.

like image 549
gsamaras Avatar asked Jan 21 '26 16:01

gsamaras


2 Answers

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

like image 74
gsamaras Avatar answered Jan 23 '26 14:01

gsamaras


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

like image 36
Peter Cordes Avatar answered Jan 23 '26 14:01

Peter Cordes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!