読了時間 : 10 minutes

あなたは始めます`./gradlew build`, ターミナルは3分間ブロックされており、待っている間に他のことをする必要がありますか?新しいタブを開く必要はありません。`Ctrl+Z`プロセスを一時停止する`fg`それはあなたが残した場所に正確に戻します。これは開発者の日常において最も過小評価されているUnixのヒントです。

問題:端末がビジー

すべての開発者はこの状況を経験したことがある:

$ ./gradlew build
> Building...

ターミナルがフリーズしています。もうコマンドを入力することはできません。そして、ファイルを確認して、起動する必要があります`git status`または、使用中のポートを確認する。

@startuml
skinparam backgroundColor #FEFEFE

actor Développeur as dev
participant "ターミナル" as term
participant "Gradle\n( プロセス )" as gradle

dev -> term : ./gradlew build
term -> gradle : Lance le build
gradle -> term : Occupe le terminal\n(pas de prompt)
dev -> term : veut taper une commande
term --> dev : ❌ Terminal bloqué

note right of dev
開発者は
ビルドの全期間中
身動きが取れなくなります
end note
@enduml

ほとんどの開発者は新しいターミナルを開きます。しかし、もっとエレガントで速く、ネイティブなソリューションがあります:ジョブ管理です。

必須の3つのコマンド

Ctrl+Z — 一時停止 (SIGTSTP)

`Ctrl+Z`信号を送る`SIGTSTP`フォアグラウンド プロセスに。 プロセスは中断された— 死んでいない — ターミナルはプロンプトを取り戻す。

$ ./gradlew build
> Building...
^Z
[1]+  Stoppé    ./gradlew build
$

括弧の中の数`[1]`ですジョブ番号. Le `+`デフォルトのジョブを示します。

fg — フォアグラウンドで復帰する

`fg`サスペンドされたジョブをフォアグラウンドに戻す。プロセスは、停止した場所から正確に再開される。

$ fg
./gradlew build
> Building... (reprend où il en était)

特定のジョブ番号:

$ fg %2

bg — バックグラウンドで再開

`bg`端末をブロックせずにプロセスを再開します。長いビルドに最適です。

$ bg
[1]+ ./gradlew build &
$

Le `&`終わりにあることは、プロセスがバックグラウンドで実行されていることを意味します。

jobs — シェルのジョブを一覧表示

$ jobs
[1]-  Stoppé    vim README.md
[2]+  En cours   ./gradlew build
@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

state "フォアグラウンド\n(ターミナル停止中)" as fg #LightCoral
state "停止中\n(Ctrl+Z)" as stopped #LightYellow
state "バックグラウンド\n(bg)" as bg_ #LightGreen

[*] --> fg : Lance une commande
fg --> stopped : Ctrl+Z\n(SIGTSTP)
stopped --> fg : fg\nreprend en avant-plan
stopped --> bg_ : bg\nreprend en arrière-plan
bg_ --> fg : fg\nramène en avant-plan
fg --> [*] : Commande terminée

note right of stopped
プロセスは停止していますが、
_終了してはいません_
end note

note left of bg_
ターミナルは空いています
ビルドを続行します
end note
@enduml

シナリオ 1: 長いビルド

これは最も一般的な使用例です。Gradleのビルドを実行し、ターミナルがブロックされ、他のことをする必要があります。

@startuml
skinparam backgroundColor #FEFEFE
autonumber

actor Développeur as dev
participant "ターミナル" as term
participant "Gradle" as gradle
database "プロセスの状態" as state

dev -> term : ./gradlew build
term -> gradle : Lance le build
gradle -> state : En cours (FG)
dev -> term : Ctrl+Z
term -> state : Suspendu (job [1])
term --> dev : Prompt libre $

dev -> term : git status
term --> dev : working tree clean

dev -> term : fg
term -> state : Reprend (FG)
gradle --> dev : Build réussi ✓

note right of state
Gradleプロセスは
終了されていません
中断した箇所から
正確に再開します
end note
@enduml

ステップバイステップ

$ ./gradlew build
> Building...
^Z
[1]+  Stoppé    ./gradlew build

$ git status
On branch main
nothing to commit, working tree clean

$ fg
./gradlew build
> Building...
BUILD SUCCESSFUL in 2m 34s

シナリオ2 : 忘れられた編集者

あなたは…の中にいます`vim`, 他のファイルを確認する必要があります sans 終了する`vim`.

# Dans vim, en mode commande :
:shell
$ cat /path/to/other/file.txt
$ exit
# Retour dans vim

しかし、それは重い。それに`Ctrl+Z`, それは即時です :

# Dans vim, n'importe quel moment :
Ctrl+Z
[1]+  Stoppé    vim README.md

$ cat /path/to/other/file.txt
# ... lire le fichier ...

$ fg
vim README.md
# Retour exact là où on en était, curseur en place
@startuml
skinparam backgroundColor #FEFEFE
autonumber

actor Développeur as dev
participant "vim" as vim
participant "シェル" as shell

dev -> vim : édition en cours
dev -> vim : Ctrl+Z
vim -> shell : Suspendu (job [1])
dev -> shell : cat autre_fichier.txt
shell --> dev : contenu du fichier
dev -> shell : fg
shell -> vim : Reprend (job [1])
vim --> dev : Curseur exactement\nà la même position

note right of vim
vim は以下の状態を
完全に復元します:
カーソル位置、
バッファ、undo スタック
end note
@enduml

(No output?) Actually we need to output something? The instruction: "Translate from fr to ja. …​ Output only the translated text". If there is no source text, then there is nothing to translate. So output nothing. We’ll output an empty line.

</think>

(Empty)vim`その後、完全にその状態を復元します`fg: カーソル位置, バッファ内容, 元に戻す履歴 — すべて保持されます。

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif
skinparam componentStyle rectangle

actor Développeur as dev
participant "opencode\n(LLMエージェント)" as agent
participant "シェル" as shell
participant "git" as git

dev -> agent : Session active
agent -> agent : Tour de parole\ndu LLM en cours

note over agent
Ctrl+Zが
コード解析の
真っ最中に発生します
end note

dev -> agent : Ctrl+Z
agent -> shell : Suspendu (job [1])
dev -> git : git diff
git --> dev : changements visibles
dev -> shell : fg
shell -> agent : Reprend (job [1])
agent --> dev : Reprend exactement\nl'analyse en cours

note right of agent
コンテキストの損失なし
LLMは回答を
正確に中断した箇所から再開します
end note
@enduml

これがこの記事を書くきっかけとなったシナリオです。あなたは使っています。opencode(コード上のLLMを操作するCLIツール),そしてプロセスは一時停止されています。

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif
skinparam componentStyle rectangle

actor Développeur as dev
participant "opcode\n(LLMエージェント)" as agent
participant "シェル" as shell
participant "git" as git

dev -> agent : Session active
agent -> agent : Tour de parole\ndu LLM en cours

note over agent
  Ctrl+Z arrive
  en plein milieu
  d'une analyse de code
end note

dev -> agent : Ctrl+Z
agent -> shell : Suspendu (job [1])
dev -> git : git diff
git --> dev : changements visibles
dev -> shell : fg
shell -> agent : Reprend (job [1])
agent --> dev : Reprend exactement\nl'analyse en cours

note right of agent
  Aucune perte de contexte
  Le LLM reprend sa réponse
  exactement où il en était
end note
@enduml
$ opencode
# ... session en cours, LLM analyse le code ...
^Z
[1]+  Stoppé    opencode

$ git diff HEAD~1
# vérifier les changements récents

$ fg
opencode
# Le LLM reprend là où il s'était arrêté

利点は二重です :

  1. コンテキストの損失はありません : LLM セッション, 会話履歴, 開いているファイル — すべてが保持されます

  2. 再起動不要: opencode を再起動したり、コンテキストを再読み込みしたり、LLM に問題を再説明する必要はありません

シナリオ 4 : 複数のジョブを同時に管理

@startuml
skinparam backgroundColor #FEFEFE

rectangle "停止中のジョブ" as jobs #LightYellow {
  card "[1] vim config.yml" as j1
  card "[2] ./gradlew test" as j2
  card "[3] htop" as j3
}

rectangle "アクション" as actions #LightGreen {
  card "fg %2\n→ フォアグラウンド" as a1
  card "bg %3\n→ バックグラウンド" as a2
  card "fg %1\n→ フォアグラウンド" as a3
}

jobs --> actions : fg/bg + numéro de job
actions --> j2 : Reprend
actions --> j3 : En arrière-plan

note bottom of jobs
各ジョブは、
サスペンド/レジュームの間、
その正確な状態を保持します
end note
@enduml
$ vim config.yml
^Z
[1]+  Stoppé    vim config.yml

$ ./gradlew test
^Z
[2]+  Stoppé    ./gradlew test

$ htop
^Z
[3]+  Stoppé    htop

$ jobs
[1]   Stoppé    vim config.yml
[2]-  Stoppé    ./gradlew test
[3]+  Stoppé    htop

$ fg %2
./gradlew test
# Le build reprend en avant-plan

$ bg %3
[3] htop &
# htop reprend en arrière-plan
@startuml
skinparam backgroundColor #FEFEFE

rectangle "一時停止中のジョブ" as jobs #LightYellow {
  card "[1] vim config.yml" as j1
  card "[2] ./gradlew テスト" as j2
  card "[3] htop" as j3
}

rectangle "アクション" as actions #LightGreen {
  card "fg %2\n→ 前景" as a1
  card "bg %3\n→ 背景" as a2
  card "fg %1\n→ 前景" as a3
}

jobs --> actions : fg/bg + numéro de job
actions --> j2 : Reprend
actions --> j3 : En arrière-plan

note bottom of jobs
  Chaque job conserve
  son état exact
  entre les suspend/resume
end note
@enduml

ジョブコマンドのサマリー

注文 �効果 例

Ctrl+Z

フォアグラウンドプロセスを一時停止

送信の`SIGTSTP`

fg

フォアグラウンドでデフォルトのジョブを再開

fg ou fg %1

fg %n

ジョブ n をフォアグラウンドで再開

fg %2

bg

バックグラウンドでデフォルトのジョブを再開します

bg ou bg %1

bg %n

ジョブ n をバックグラウンドで再開

bg %2

jobs

現在のシェルのジョブをリスト

jobs -l(PID付き)

kill %n

ジョブ n を終える

kill %2

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

rectangle "**Ctrl+Z**\nSIGTSTP" as ctrlz #LightYellow {
  card "プロセスを一時停止\n(メモリには保持される)\nfg/bg で利用可能" as c1
}

rectangle "**Ctrl+C**\nSIGINT" as ctrlc #LightCoral {
  card "プロセスを強制終了\n(復帰不可)\nすべての状態が失われる" as c2
}

rectangle "**& **\n(バックグラウンド)" as ampersand #LightGreen {
  card "直接バックグラウンドで\n起動する\nターミナルは即座に解放される" as c3
}

rectangle "**fg**\n(foreground)" as fg_ #SkyBlue {
  card "一時停止したジョブを\nフォアグラウンドに戻す\nターミナルは占有される" as c4
}

rectangle "**bg**\n(background)" as bg_ #Lavender {
  card "一時停止したジョブを\nバックグラウンドで再開する\nターミナルは解放される" as c5
}

ctrlz -right-> fg_ : fg pour reprendre
ctrlz -right-> bg_ : bg pour continuer\nen arrière-plan
ctrlc -right-> c2 : Pas de retour\nprocessus tué

note bottom of ctrlz
プロセスは**停止(フリーズ)**していますが、**終了してはいません**
end note

note bottom of ampersand
代替案:`./gradlew build &` を直接バックグラウンドで実行する
end note
@enduml

多くの開発者はこれらのメカニズムを混同します。ここに決定的な説明があります。

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

rectangle "**Ctrl+Z**\nSIGTSTP" as ctrlz #LightYellow {
  card "プロセスを一時停止します\n(まだメモリに残っています)\nfg/bg で利用可能" as c1
}

rectangle "**Ctrl+C**\nSIGINT" as ctrlc #LightCoral {
  card "プロセスを殺す\n(戻ることはできません)\nすべての状態の喪失" as c2
}

rectangle "**および **\n(背景)" as ampersand #LightGreen {
  card "直接起動して\nバックグラウンドで\nフリーのターミナルすぐに" as c3
}

rectangle "**fg**\n(フォアグラウンド)" as fg_ #SkyBlue {
  card "ジョブを持ってきて\nフォアグラウンドで一時停止\nターミナルが使用中" as c4
}

rectangle "**bg**\n(背景)" as bg_ #Lavender {
  card "ジョブを再開しろ\nバックグラウンドで一時停止中\n空きターミナル" as c5
}

ctrlz -right-> fg_ : fg pour reprendre
ctrlz -right-> bg_ : bg pour continuer\nen arrière-plan
ctrlc -right-> c2 : Pas de retour\nprocessus tué

note bottom of ctrlz
  Le processus est **figé**
  mais **pas tué**
end note

note bottom of ampersand
  Alternative : lancer
  ./gradlew build &
  directement en arrière-plan
end note
@enduml
ショートカット/コマンド 信号 効果 �戻る?

Ctrl+Z

SIGTSTP

プロセスを一時停止する(凍結、メモリ上)

はい :`fg` ou bg

Ctrl+C

SIGINT

プロセスを終了させる (完了)

いいえ : プロセスが破壊されました

Ctrl+\

SIGQUIT

プロセスを kill + コアダンプ

いいえ: 破壊されたプロセス

commande &

—

バックグラウンドで直接起動

`fg`それを前面に出すために

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

title シェルジョブの完全なライフサイクル
state "存在しない" as none
state "フォアグラウンド\n(ターミナルを占有)" as foreground #LightCoral
state "停止中\n(Ctrl+Z / SIGTSTP)" as suspended #LightYellow
state "バックグラウンド\n(bg または &)" as background #LightGreen
state "完了\n(終了コード 0)" as done #LightGray

[*] --> none
none --> foreground : tape une commande
foreground --> suspended : Ctrl+Z
foreground --> done : fin normale

suspended --> foreground : fg
suspended --> background : bg
suspended --> done : kill %n

background --> foreground : fg
background --> done : fin normale\n(en arrière-plan)
background --> suspended : Ctrl+Z\n(si fg puis Ctrl+Z)

done --> [*]

note right of suspended
サスペンド状態は
ジョブシステム全体の
要(かなめ)です
end note
@enduml

ジョブの完全なライフサイクル

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

title シェルジョブの完全なライフサイクル
state "存在しない" as none
state "前景\n(ターミナルを占有します)" as foreground #LightCoral
state "一時停止\n(Ctrl+Z / SIGTSTP)" as suspended #LightYellow
state "背景\n(bg または &)" as background #LightGreen
state "完了\n(exit code 0)" as done #LightGray

[*] --> none
none --> foreground : tape une commande
foreground --> suspended : Ctrl+Z
foreground --> done : fin normale

suspended --> foreground : fg
suspended --> background : bg
suspended --> done : kill %n

background --> foreground : fg
background --> done : fin normale\n(en arrière-plan)
background --> suspended : Ctrl+Z\n(si fg puis Ctrl+Z)

done --> [*]

note right of suspended
  L'état suspendu est
  le pivot de tout le
  système de jobs
end note
@enduml

高度なヒント

fg 最新の仕事と交互に

fg`引数なしでは、マークされたジョブが戻ります+(最新の)。 しかし、あなたはまた使用することもできます%-`前のジョブを対象にする :

$ vim file1.txt
^Z
[1]+  Stoppé    vim file1.txt

$ vim file2.txt
^Z
[2]+  Stoppé    vim file2.txt

$ fg      # ramène job [2] (le plus récent)
$ fg %-   # ramène job [1] (le précédent)

ジョブをきれいに殺す

$ jobs
[1]-  Stoppé    vim config.yml
[2]+  Stoppé    ./gradlew test

$ kill %2       # envoie SIGTERM au job 2
$ kill -9 %1    # envoie SIGKILL au job 1 (force)

désown : シェルから切り離す

disown`シェルのジョブテーブルからジョブを削除します。プロセスは実行を継続しますが、再度呼び出すことはできません。`fg:

$ ./gradlew build &
[1] 12345

$ disown %1
# Le processus continue mais n'est plus lié au shell
# Ça survives à la fermeture du terminal

長いプロセスに対して nohup と組み合わせる

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

rectangle "fg" as fg_ #SkyBlue {
  card "最新のジョブを(+)\n復帰させる" as f1
  card "特定のジョブを指定: fg %n" as f2
  card "切り替え: fg %-" as f3
}

rectangle "kill %n" as kill_ #LightCoral {
  card "SIGTERM (クリーン)" as k1
  card "SIGKILL -9 (強制)" as k2
}

rectangle "disown" as disown #Lavender {
  card "シェルから切り離す\n(ログアウト後も生存)" as d1
}

rectangle "nohup + &" as nohup_ #LightGreen {
  card "SIGHUPを無視するプロセス\n(ターミナルを閉じても\n生存する)" as n1
}

fg_ -right-> kill_ : si faut\nterminer
kill_ -right-> disown_ : ou détacher
nohup_ -left-> fg_ : fg ramène en\navant-plan

note bottom of disown_
disown` 後、`fg` では
ジョブを復帰させることができなくなります
end note
@enduml
$ nohup ./gradlew build &
[1] 12345
$ disown %1
# Le build continue même si vous fermez le terminal
@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

rectangle "fg" as fg_ #SkyBlue {
  card "最新のジョブを戻す\n(+)" as f1
  card "仕事を狙う\n特定 : fg %n" as f2
  card "交互 : fg %-%" as f3
}

rectangle "殺す %n" as kill_ #LightCoral {
  card "SIGTERM (クリーン)" as k1
  card "SIGKILL -9 (強制)" as k2
}

rectangle "縁を切る" as disown #Lavender {
  card "シェルから切り離す\n(ログアウト後も生存する)" as d1
}

rectangle "nohup + &" as nohup_ #LightGreen {
  card "免疫プロセス\nSIGHUPに対して\n(端末の終了に耐える\n端末の)" as n1
}

fg_ -right-> kill_ : si faut\nterminer
kill_ -right-> disown_ : ou détacher
nohup_ -left-> fg_ : fg ramène en\navant-plan

note bottom of disown_
  Après disown, fg ne peut
  plus ramener le job
end note
@enduml

よくある落とし穴と間違い

@startuml
skinparam backgroundColor #FEFEFE

rectangle "Ctrl+Z (SIGTSTP)" as z #LightGreen {
  card "プロセスがメモリ内で停止" as z1
  card "fg でそのまま復帰" as z2
  card "損失なし" as z3
}

rectangle "Ctrl+C (SIGINT)" as c #LightCoral {
  card "プロセス終了" as c1
  card "状態は完全に消失" as c2
  card "復帰不可" as c3
}

note bottom of z
一時停止 → 再開
end note

note bottom of c
Kill → 失った
end note
@enduml

これが最も頻繁な間違いです。`Ctrl+C`プロセスを殺す。`Ctrl+Z`それを一時停止します。もしあなたが押すと`Ctrl+C`反射的に、すべての状態が失われます — 開いているファイル、LLMセッション、進行中のビルド。

@startuml
skinparam backgroundColor #FEFEFE

rectangle "Ctrl+Z (SIGTSTP)" as z #LightGreen {
  card "メモリ上で凍結しているプロセス" as z1
  card "fg はそれを無傷のまま連れ戻す" as z2
  card "損失なし" as z3
}

rectangle "Ctrl+C (SIGINT)" as c #LightCoral {
  card "プロセス終了" as c1
  card "状態は永久に失われた" as c2
  card "返品はできません" as c3
}

note bottom of z
  Pause → Reprendre
end note

note bottom of c
  Kill → Perdu
end note
@enduml

ジョブはシェルにローカルです

jobs, fg et `bg`ジョブは、それらを起動したシェル内でのみ機能します。新しいターミナルを開くと、他のジョブは表示されません。

# Terminal 1
$ vim file.txt
^Z
[1]+  Stoppé    vim file.txt

# Terminal 2 (nouveau)
$ jobs
# (rien — les jobs sont locaux au shell)

システム全体のプロセスを見るには、ps ou htop:

ps aux | grep vim

ジョブはシェルの終了後に生き残らない

ターミナルを閉じると、一時停止またはバックグラウンドで実行中のすべてのジョブが破棄されます。プロセスが存続するようにするには、使ってください`nohup`+disown(Empty)

$ nohup ./gradlew build &
[1] 12345
$ disown %1
# Fermer le terminal → le build continue

対話型プロセスと fg

一部のインタラクティブなプログラムはその後良くない状態になる`Ctrl+Z` + fg. 稀ですが、起こります:

  • シグナルを使用する一部のプログラムは(vim、htop、lessはうまく機能しています`Ctrl+Z`)

  • 長時間のネットワークプログラム (ssh、opencode) は通常、正常に取得できます

  • ターミナル (ncurses) を変更するプログラムは、ときどきターミナルを予期しない状態に残すことがあります

端末が破損した後の`fg`, 入力して :

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif
skinparam ArrowColor #444444

title "fg — 全てを変えるTips"

rectangle "**問題**" as problem #LightCoral {
  card "プロセスによって\nターミナルが停止" as p1
  card "新しいターミナルを開く = 非効率な解決策" as p2
}

rectangle "**解決策**" as solution #LightGreen {
  card "Ctrl+Z → 一時停止" as s1
  card "fg → 再開" as s2
  card "bg → バックグラウンドへ" as s3
  card "jobs → 一時停止中のタスクを確認" as s4
}

rectangle "**メリット**" as advantages #SkyBlue {
  card "コンテキストを失わない" as a1
  card "再起動が不要" as a2
  card "ターミナル1つで完結" as a3
}

problem -right-> solution : "40年前からある\nUnixネイティブの解決策"
solution -right-> advantages : "結果"

note bottom of advantages
fgはマニアックなTipsではなく、
Unixシェルの
根本的な仕組みです。
end note
@enduml

サマリーダイアグラム

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif
skinparam ArrowColor #444444

title "fg — すべてを変えるチップ"

rectangle "問題" as problem #LightCoral {
  card "端末がブロックされました\nプロセスによって" as p1
  card "開ける 一つの 新しい\nターミナル = 解決策 重い" as p2
}

rectangle "**解答**" as solution #LightGreen {
  card "Ctrl+Z → 一時停止" as s1
  card "fg → 再開する" as s2
  card "bg → 背景" as s3
  card "jobs → サスペンドされたものを表示" as s4
}

rectangle "利点" as advantages #SkyBlue {
  card "コンテキストの損失なし" as a1
  card "再起動なし" as a2
  card "ターミナル1つで十分です" as a3
}

problem -right-> solution : "Unixネイティブソリューション\n40年間"
solution -right-> advantages : "結果"

note bottom of advantages
  fg n'est pas un tips obscur
  C'est un mécanisme fondamental
  du shell Unix
end note
@enduml

なぜ fg が過小評価されているのか

ほとんどの最新の開発者は発見する`fg`�遅れたり、あるいは決して起こらないこともあります。 いくつかの理由:

  1. IDEはターミナルを隠す: VS Codeは統合ターミナルを提供しますが、複数のタブは`fg`表面的には必要性が低い

  2. グラフィカルツールは CLI を置き換えるファイルマネージャー、システムモニター、グラフィカルGitクライアント

  3. tmux と screen: これらのマルチプレクサは同じ問題を異なる方法で解決しますが`fg`よりシンプルで常に利用可能です

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

rectangle "なぜ `fg` は\n過小評価されているのか?" as why #LightCoral {
  card "マルチタブターミナル搭載の\nIDE" as w1
  card "CLIを代替する\nGUIツール" as w2
  card "tmux/screenが\n別の方法で\n問題を解決している" as w3
}

rectangle "なぜ `fg` が\n優れているのか?" as superior #LightGreen {
  card "ユニバーサル: bash,\nzsh, fish, sh..." as s1
  card "依存関係なし:\nインストール不要" as s2
  card "わずか2打鍵:\nCtrl+Z の後に `fg`" as s3
  card "コンテキストを保持:\n状態の喪失ゼロ" as s4
}

why -right-> superior : "最後には常に\nシンプルさが勝つ"
@enduml
  1. 普遍的: POSIXシェルすべてに存在する (bash, zsh, fish…​)

  2. 依存性のない: tmuxまたはscreenをインストールする必要はありません

  3. スナップショット: 2回の打撃 (Ctrl+Z`それから`fg) 一時停止および再開する

  4. コンテキスト保存者: プロセスの状態は無傷です

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

rectangle "なぜ fg は\n過小評価されているのでしょうか?" as why #LightCoral {
  card "ターミナル付きIDE\nマルチタブ" as w1
  card "グラフィカルツール\nCLIを置き換える" as w2
  card "tmux/screen\n問題を解決します\nあるいは" as w3
}

rectangle "なぜ fg は\n優れているのでしょうか?" as superior #LightGreen {
  card "汎用的 : bash,\nzsh, fish, sh..." as s1
  card "依存なし:\nインストール不要" as s2
  card "2つのキー操作:\nCtrl+Z と fg" as s3
  card "保持されたコンテキスト:\n状態損失ゼロ" as s4
}

why -right-> superior : "シンプルさ\nいつも勝つ"
@enduml

技術的なメモリ: 信号

興味のある方へ、関与している信号は以下の通り:

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

participant "ユーザー" as user
participant "シェル" as shell
participant "プロセス" as proc

user -> shell : Ctrl+Z
shell -> proc : SIGTSTP (signal 18)
note right of proc : メモリ内でフリーズしたプロセス

user -> shell : fg
shell -> proc : SIGCONT (signal 19)
note right of proc : プロセスをフォアグラウンドで\n再開します

user -> shell : bg
shell -> proc : SIGCONT (signal 19)
note right of proc : プロセスがバックグラウンドで再開されます

user -> shell : Ctrl+C
shell -> proc : SIGINT (signal 2)
note right of proc : プロセス完了\n状態喪失
@enduml

完全なフロー :

@startuml
skinparam backgroundColor #FEFEFE
skinparam defaultFontName sans-serif

participant "ユーザー" as user
participant "シェル" as shell
participant "プロセス" as proc

user -> shell : Ctrl+Z
shell -> proc : SIGTSTP (signal 18)
note right of proc : Processus figé\nen mémoire

user -> shell : fg
shell -> proc : SIGCONT (signal 19)
note right of proc : Processus reprend\nen avant-plan

user -> shell : bg
shell -> proc : SIGCONT (signal 19)
note right of proc : Processus reprend\nen arrière-plan

user -> shell : Ctrl+C
shell -> proc : SIGINT (signal 2)
note right of proc : Processus terminé\nétat perdu
@enduml

結論

`fg`これは obscure tips ではない — Unix シェルの基本的なメカニズムで、1970 年代から利用可能です。ほとんどの開発者がこれを無視するのは、私たちの時代の症状です:IDE とマルチプレクサーが私たちを基本から遠ざけている。

しかし、SSHサーバーにいるとき、ミニマルなターミナルで、あるいは単純に使っているときでも`opencode`そしてLLMはまさに作業中です —Ctrl+Z, fg, bg, `jobs`彼らはあなたの親友です。

_ Ctrl+Zで一時停止、fgで再開。これはターミナルの一時停止・再生です。 _

関連記事