LoginSignup
175
148

More than 5 years have passed since last update.

SSHからログアウトした後もプロセスを起動しておく方法

Last updated at Posted at 2016-12-15

SSHでリモートに接続して,SSHの接続を切った後もプロセスを回し続けたいという場面があります。
私の場合は,リモートのGPUマシンに接続して機械学習のモデルを学習させたいときがこれにあたります。

nohup

nohupコマンドを使うと,stdout/stderrへの出力をファイルに書き出すことができます。

$nohup <command> &

<command>の部分には実行したいコマンドを入れます。例えばpython3 train.pyを実行し続けたい場合,

$nohup python3 train.py &

とするとSSH接続を切ったあとも学習を続けられます。

screen

screenコマンドは実行画面をセッションとして残しておくことができます。
nohupと同じようにコマンドを引数として渡すと実行が開始され, Ctrl-A → D で画面から抜け出すことができます。
この状態でSSH接続を切ってもプロセスは実行され続けます。

$screen python3 example.py
[detached from 12689.pts-4.user]
$exit
Connection to localhost closed.

SSHに再接続したら,screen -lsで復帰したい画面を選ぶことができます。

$ssh remote
$screen -ls
There are several suitable screens on:
    12762.pts-4.user    (12/15/2016 07:05:42 PM)    (Detached)

画面を選んで再接続すれば実行画面にもどれます。

$screen -r 12762
[detached from 12762.pts-4.user]
175
148
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
175
148