長時間かかるスクリプトを実行する前に実行しておくといいコマンド

記事タイトルとURLをコピーする

はじめに

技術課の森です。 最近寒くなったり、暖かくなったりと身体を壊しやすい季節になりました。 そんなときに有益な情報です。 デプロイとかですごく時間がかかるコマンドを実行するときに、ひと手間加えるだけで、ターミナルが落ちてもいいコマンドがあります。 screen コマンドの紹介です。

手始め

まずは実行したいスクリプトを実行する前に screen コマンドを実行します。 今回の例はmac上でやっていますが、SSHした環境などでも利用することが出来ます。

コマンド実行

$ screen

実行すると以下のような出力になるので、スペースキーかEnterキーを押します。

Screen version 4.00.03 (FAU) 23-Oct-06

Copyright (c) 1993-2002 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 2, 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, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to screen@uni-erlangen.de


                                                                  [Press Space or Return to end.]

すると、渡しはbashを使ってるので、以下のような出力になります。

bash-3.2$ 

そして、コマンドを実行します。 実行した後、「Ctrl+A」を押した後に「d」を押します。

するとプロンプトが返ってきます。

$ screen
[detached]
$

これで、裏で実行されている状態になります。 復旧するには以下のコマンドを実行すると復旧に必要な情報が表示されます。

$ screen -ls
There is a screen on:
    65355.ttys001.mori-pc-mbp   (Detached)
1 Socket in /var/folders/rg/0tt06h395r7ck40dwh11671c0000gp/T/.screen.

今回は一つしか無いので、再度繋ぐ場合には、 screen -r でもOKなのですが、 screen -r 65355 でもOKです。

これを実行するだけで、リモートセッションが切れても簡単に再開出来ます。

最後に

今回はmacでしたが、もちろんAmazon Linuxでも同じことが出来ます。 なにかのデプロイで時間がかかっても、このコマンドを最初に実行しておけば、しばらく別のことをしていても安心ですね。

daiki mori(執筆記事の一覧)

アプリケーションサービス部 ディベロップメントサービス1課

お酒とLambdaとつまみとConnectが好きです。