概要
SSHが突然きれたときにその段階でしていた処理はSIGHUPシグナルで終了する
(SIGHUPシグナル:端末終了時に発生。元来はモデムの受話器をあげて通信を切ったことから)
試してみるとSIGHUPされていることがわかる
root@hostname:/home/shimizu# cat /usr/local/sbin/sshtest.sh #!/bin/bash # EXITシグナルをtrapして終了メッセージを指定する。 trap "echo '`basename $0`を終了します.'" EXIT # 他のシグナルもtrapしておく。 trap "`echo 'SIGHUPをtrapしました' > /var/log/sshtest.log`" 1 # Ctrl+Cで終了するテストのためにsleepしておく。 sleep 100 exit 0 root@hostname:/home/shimizu# /usr/local/sbin/sshtest.sh ### 実行中にSSHを切断して、再度接続する ### root@hostname:/home/shimizu# cat /var/log/sshtest.log SIGHUPをtrapしました
これを防ぐためにscreenコマンドを利用する
root@hostname:/home/shimizu# aptitude install screen 以下の新規パッケージがインストールされます: screen ... root@hostname:/home/shimizu# screen Screen version 4.01.00devel (GNU) 2-May-06 Copyright (c) 2010 Juergen Weigert, Sadrul Habib Chowdhury Copyright (c) 2008, 2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib Chowdhury Copyright (c) 1993-2002, 2003, 2005, 2006, 2007 Juergen Weigert, Michael Schroeder Copyright (c) 1987 Oliver Laumann This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, see http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA. Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to screen-devel@gnu.org Capabilities: +copy +remote-detach +power-detach +multi-attach +multi-user +font +color-256 +utf8 +rxvt +builtin-telnet root@hostname:/home/shimizu# /usr/local/sbin/sshtest.sh ### SSHを中断して、再度ログインする ### root@hostname:/home/shimizu# ps afx ... 6139 ? Ss 0:00 SCREEN 6140 pts/3 Ss 0:00 \_ /bin/bash 6141 pts/3 S+ 0:00 \_ /bin/bash /usr/local/sbin/sshtest.sh 6144 pts/3 S+ 0:00 \_ sleep 100 ### screenの状況を確認する ### root@hostname:/home/shimizu# screen -ls There is a screen on: 6139.pts-2.hostname (2015年04月09日 23時11分50秒) (Detached) 1 Socket in /var/run/screen/S-root. ### atachする。detachしている間も処理は継続されていた ### root@hostname:/home/shimizu# screen -r 6139.pts-2.hostname
[ctl]+[D]でdetachできる
また[ctl]+[A]+[K]でscreenを終了する
参考URL
シグナルと trap コマンド
http://shellscript.sunone.me/signal_and_trap.html
ssh切断後の挙動について
https://teratail.com/questions/5771