GCP Virtual Machine and ssh task execution

by allenlu2007

Ref1: http://orcaman.blogspot.tw/2013/08/google-compute-engine-keeping-your.html

Ref2: https://www.tecmint.com/keep-remote-ssh-sessions-running-after-disconnection/amp/

使用 GCP 主要使用 ssh command line window.  好處是在各種平台都可以運行。壞處就是缺乏 GUI, 需要用 VNC + gnome desktop 協助。

另外一個很大的問題:一旦 ssh disconnect, 正在運行的 task 也自動被結束。即使是用 background task ( command &) 也不行。



原因可參考 Ref2.  

when we ssh on to other user on some other system and run commands on that machine, it actually creates a pseudo-terminal and attaches it to the login shell of the user logged in.

When we log out of the session or the session times out after being idle for quite some time, the SIGHUP signal is send to the pseudo-terminal and all the jobs that have been run on that terminal, even the jobs that have their parent jobs being initiated on the pseudo-terminal are also sent the SIGHUP signal and are forced to terminate.

Only the jobs that have been configured to ignore this signal are the ones that survive the session termination. On Linux systems, we can have many ways to make these jobs running on the remote server or any machine even after user logout and session termination.


本文是討論解法。參考 Ref1.

So after we’ve ssh’ed into our GCE VM, we will need to:
1. install GNU screen: 

apt-get update
apt-get upgrade
apt-get install screen
2. type “screen”. this will open up a new screen – kind of similar in look & feel to what “clear” would result in. 
3. run the process (e.g.: ./init-dev.sh to fire up a ChicagoBoss erlang server)
4. type: Ctrl + A, and then Ctrl + D. This will detach your screen session but leave your processes running!
5. feel free to close the SSH terminal. whenever you feel like it, ssh back into your GCE VM, and type screen -r to resume your previously detached session.
6. to kill all detached screens, run: 

screen -ls | grep pts | cut -d. -f1 | awk '{print $1}' | xargs kill