LoginSignup
10
8

More than 3 years have passed since last update.

ログアウトしたあともバックグラウンドでコマンドを実行し続ける

Last updated at Posted at 2016-08-03

コマンド実行時に指定する

nohup&(バックグラウンドで実行)を利用する。

$ nohup sh -c "cd /home/user/test && command.sh > result.txt" &
[1] 20515
nohup: 入力を無視し、出力を `nohup.out' に追記します  

コマンドを実行してしまったとき

disownを利用する。

$ cd /home/user/test && command.sh > result.txt
# Ctrl-Zでジョブを一時停止する
$ jobs # セッションに紐づくジョブ一覧を表示
[1]  + suspended command.sh > result.txt
$ bg %1 # 1のジョブをバックグラウンドで走らせる
$ disown %1 # ジョブテーブルから対象のジョブを外す
$ jobs
# 先ほどのコマンドがジョブテーブルから外れていることを確認

ちょっと補足

nohupを使う場合は、自分のジョブテーブルでコマンドを実行している。
ただ端末のセッションが破棄されるときに、SIGHUPシグナルを無視する。実行しているセッションは同じ。

disownを使う場合は、現在のセッションと切り離される。

10
8
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
10
8