1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CentOS7 ターミナルの親プロセスを終了したらどうなるか検証してみた

Posted at

はじめに

linuxでプロセスの仕組みが曖昧な部分があったため、
ターミナル上で実行したプロセスを終了しようとして、誤ってps -efコマンドのプロセスIDの隣に記載してある
ターミナルの親プロセスIDをkillしたらどうなるか検証してみました。
また、プロセス終了とnohupコマンドを試してみました。

ps -efコマンドについて以下の記事が詳細に記載されています。
参考にさせていただきました。
https://codezine.jp/unixdic/w/ps
https://atmarkit.itmedia.co.jp/ait/articles/1603/28/news022.html

対応した内容

ターミナルの親プロセスIDを停止してみた

Teratermに接続してsleepコマンドとyesコマンドを実行しました

[root@localhost ~]# yes > /dev/null &
[1] 1641
[root@localhost ~]#
[root@localhost ~]# yes > /dev/null &
[2] 1643
[root@localhost ~]# sleep 120 &
[3] 1670

sleepコマンドのプロセスを表示しました。
-e すべてのプロセスを表示
-f 起動時間も含めた詳細なフォーマットを表示

※TeraTermで起動したターミナル名 pts/0です。
 grep --color=auto -e yes -e sleepはps -efコマンドのプロセスです。
 ps -efコマンドを実行するごとにプロセスIDが変わります。

[root@localhost ~]# ps -ef | grep -e yes -e sleep
root      1641  1248 52 09:35 pts/0    00:06:41 yes
root      1643  1248 49 09:35 pts/0    00:06:05 yes
root      1670  1248  0 09:48 pts/0    00:00:00 sleep 120
root      1676  1248  0 09:48 pts/0    00:00:00 grep --color=auto -e yes -e sleep

親プロセスを表示させるためにps axfコマンドを実行してみた
1248のプロセスIDは、TeraTermのターミナルpts/0の親プロセスBashです。

[root@localhost ~]# ps axf
 PID TTY      STAT   TIME COMMAND
    2 ?        S      0:00 [kthreadd]
    4 ?        S<     0:00  \_ [kworker/0:0H]
    5 ?        S      0:00  \_ [kworker/u2:0]
    6 ?        S      0:00  \_ [ksoftirqd/0]
    7 ?        S      0:00  \_ [migration/0]
    8 ?        S      0:00  \_ [rcu_bh]
    9 ?        R      0:00  \_ [rcu_sched]
   10 ?        S<     0:00  \_ [lru-add-drain]
   11 ?        S      0:00  \_ [watchdog/0]
   13 ?        S      0:00  \_ [kdevtmpfs]
   14 ?        S<     0:00  \_ [netns]
   15 ?        S      0:00  \_ [khungtaskd]
   16 ?        S<     0:00  \_ [writeback]
   17 ?        S<     0:00  \_ [kintegrityd]
   18 ?        S<     0:00  \_ [bioset]
   19 ?        S<     0:00  \_ [bioset]
   20 ?        S<     0:00  \_ [bioset]
   21 ?        S<     0:00  \_ [kblockd]
   22 ?        S<     0:00  \_ [md]
   23 ?        S<     0:00  \_ [edac-poller]
   24 ?        S<     0:00  \_ [watchdogd]
   30 ?        S      0:00  \_ [kswapd0]
   31 ?        SN     0:00  \_ [ksmd]
   32 ?        SN     0:00  \_ [khugepaged]
   33 ?        S<     0:00  \_ [crypto]
   41 ?        S<     0:00  \_ [kthrotld]
   42 ?        S      0:00  \_ [kworker/u2:1]
   43 ?        S<     0:00  \_ [kmpath_rdacd]
   44 ?        S<     0:00  \_ [kaluad]
   45 ?        S<     0:00  \_ [kpsmoused]
   47 ?        S<     0:00  \_ [ipv6_addrconf]
   60 ?        S<     0:00  \_ [deferwq]
   96 ?        S      0:00  \_ [kauditd]
   97 ?        S      0:04  \_ [kworker/0:3]
  290 ?        S<     0:00  \_ [ata_sff]
  295 ?        S      0:00  \_ [scsi_eh_0]
  297 ?        S<     0:00  \_ [scsi_tmf_0]
  298 ?        S      0:00  \_ [scsi_eh_1]
  299 ?        S<     0:00  \_ [scsi_tmf_1]
  301 ?        S      0:00  \_ [scsi_eh_2]
  302 ?        S<     0:00  \_ [scsi_tmf_2]
  304 ?        S      0:00  \_ [irq/18-vmwgfx]
  305 ?        S<     0:00  \_ [ttm_swap]
  339 ?        S<     0:00  \_ [kworker/0:1H]
  380 ?        S<     0:00  \_ [kdmflush]
  381 ?        S<     0:00  \_ [bioset]
  390 ?        S<     0:00  \_ [kdmflush]
  391 ?        S<     0:00  \_ [bioset]
  404 ?        S<     0:00  \_ [bioset]
  405 ?        S<     0:00  \_ [xfsalloc]
  406 ?        S<     0:00  \_ [xfs_mru_cache]
  407 ?        S<     0:00  \_ [xfs-buf/dm-0]
  408 ?        S<     0:00  \_ [xfs-data/dm-0]
  409 ?        S<     0:00  \_ [xfs-conv/dm-0]
  410 ?        S<     0:00  \_ [xfs-cil/dm-0]
  411 ?        S<     0:00  \_ [xfs-reclaim/dm-]
  412 ?        S<     0:00  \_ [xfs-log/dm-0]
  413 ?        S<     0:00  \_ [xfs-eofblocks/d]
  414 ?        S      0:00  \_ [xfsaild/dm-0]
  585 ?        S<     0:00  \_ [xfs-buf/sda1]
  587 ?        S<     0:00  \_ [xfs-data/sda1]
  588 ?        S<     0:00  \_ [xfs-conv/sda1]
  592 ?        S<     0:00  \_ [xfs-cil/sda1]
  593 ?        S<     0:00  \_ [xfs-reclaim/sda]
  594 ?        S<     0:00  \_ [xfs-log/sda1]
  595 ?        S<     0:00  \_ [xfs-eofblocks/s]
  596 ?        S      0:00  \_ [xfsaild/sda1]
 1336 ?        S      0:00  \_ [kworker/0:0]
 1642 ?        S      0:00  \_ [kworker/0:2]
 1667 ?        S      0:00  \_ [kworker/0:4]
 1673 ?        S      0:00  \_ [kworker/0:1]
    1 ?        Ss     0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
  491 ?        Ss     0:00 /usr/lib/systemd/systemd-journald
  511 ?        Ss     0:00 /usr/sbin/lvmetad -f
  520 ?        Ss     0:00 /usr/lib/systemd/systemd-udevd
  615 ?        S<sl   0:00 /sbin/auditd
  640 ?        Ssl    0:00 /usr/lib/polkit-1/polkitd --no-debug
  642 ?        Ssl    0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
  645 ?        S      0:00 /usr/sbin/chronyd
  654 ?        Ss     0:00 /usr/lib/systemd/systemd-logind
  656 ?        Ss     0:00 /usr/sbin/crond -n
  668 ?        Ss     0:00 login -- root
 1648 tty1     Ss+    0:00  \_ -bash
  671 ?        Ssl    0:00 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
  672 ?        Ssl    0:00 /usr/sbin/NetworkManager --no-daemon
 1001 ?        Ss     0:00 /usr/sbin/sshd -D
 1243 ?        Ss     0:00  \_ sshd: root@pts/0
 1248 pts/0    Ss     0:00  |   \_ -bash  ※ターミナルの親プロセスと思われる
 1641 pts/0    R      6:58  |       \_ yes
 1643 pts/0    R      6:22  |       \_ yes
 1674 pts/0    S      0:00  |       \_ sleep 120
 1678 pts/0    R+     0:00  |       \_ ps axf
 1303 ?        Ss     0:00  \_ sshd: root@pts/1
 1307 pts/1    Ss+    0:00      \_ -bash
 1003 ?        Ssl    0:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
 1004 ?        Ssl    0:00 /usr/sbin/rsyslogd -n
 1213 ?        Ss     0:00 /usr/libexec/postfix/master -w
 1223 ?        S      0:00  \_ pickup -l -t unix -u
 1224 ?        S      0:00  \_ qmgr -l -t unix -u
 1350 ?        Ss     0:00 /usr/sbin/anacron -s

試しにTeraTermの親プロセスIDを終了してみた

[root@localhost ~]# kill 1248

プロセスを表示したところ、親プロセスは終了していませんでした。

[root@localhost ~]# ps -ef | grep -e yes -e sleep
(起動ユーザID)(プロセスID)(親プロセスID)
root      1641  1248 52 09:35 pts/0    00:06:41 yes
root      1643  1248 49 09:35 pts/0    00:06:05 yes
root      1674  1248  0 09:48 pts/0    00:00:00 sleep 120
root      1684  1248  0 09:54 pts/0    00:00:00 grep --color=auto -e yes -e sleep

yesコマンドを終了を試してみました。

yesコマンドを一つ終了しました。

[root@localhost ~]# kill 1641
[root@localhost ~]#
[1]-  Terminated              yes > /dev/null

プロセスID(1641)のyesコマンドは終了したため、表示から消えました。

[root@localhost ~]# ps -ef | grep -e yes -e sleep
root      1643  1248 49 09:35 pts/0    00:18:36 yes
root      1704  1248  0 10:13 pts/0    00:00:00 grep --color=auto -e yes -e sleep

nohupコマンドを試してみました。

http://www.d-ken.work/2021/08/13/%E3%80%90linux%E3%80%91%E3%83%AD%E3%82%B0%E3%82%A2%E3%82%A6%E3%83%88%E5%BE%8C%E3%82%82%E3%83%90%E3%83%83%E3%82%AF%E3%82%B0%E3%83%A9%E3%82%A6%E3%83%B3%E3%83%89%E5%87%A6%E7%90%86%E3%82%92%E7%B6%9A/
sleepコマンド実行後、ログアウトしました。

[root@localhost ~]# nohup sleep 10000 &

最後、ターミナルにログインしてプロセス確認したところ、
ttyが?になりました。また親プロセスIDがsystemdのため、1の値になりました。

[root@localhost ~]# ps -ef | grep sleep
(起動ユーザID)(プロセスID)(親プロセスID)
root      1708     1  0 10:17 ?        00:00:00 sleep 10000
root      1733  1717  0 10:20 pts/2    00:00:00 grep --color=auto sleep

まとめ

ターミナルを開くとTTY(例 pts/0)の名前が割り振られてBashの親プロセスが割り振られます。
ターミナル上で実行したプロセスを終了しようとして、誤ってps -efコマンドのプロセスIDの隣に記載してある親プロセスIDをkillしても配下の子プロセスは終了しないことが分かりました。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?