Linux查找全部正在運行的守護進程(daemon)

ps -eo ppid,pid,sid,stat,tty,comm  | awk '{ if ($2 == $3 && $5 == "?") {print $0}; }'apache


首先,要注意,守護進程(daemon)和後臺進程(background process)有區別。session

守護進程是一種後臺進程,可是,同時,它必須具有如下特性:app

1. 沒有控制它的ttydom

2. 必須是一個session leaderssh

3. 必須是一個進程組的leaderide

4. 執行後臺任務oop

5. root目錄即爲工做目錄ui

6. umask設置爲0spa

7. 文件描述符(file descriptor)都必須先關閉,而後再關聯相應文件(或者設備),若是有須要的話。
orm

Wiki原文摘錄以下:

On a Unix-like system, the common method for a process to become a daemon, when the process is started from the command line or from a startup script such as an init script or a SystemStarter script, involves:

  • Dissociating from the controlling tty

  • Becoming a session leader

  • Becoming a process group leader

  • Executing as a background task by forking and exiting (once or twice). This is required sometimes for the process to become a session leader. It also allows the parent process to continue its normal execution.

  • Setting the root directory (/) as the current working directory so that the process does not keep any directory in use that may be on a mounted file system (allowing it to be unmounted).

  • Changing the umask to 0 to allow open(), creat(), et al. operating system calls to provide their own permission masks and not to depend on the umask of the caller

  • Closing all inherited files at the time of execution that are left open by the parent process, including file descriptors 0, 1 and 2 for the standard streams (stdin, stdout and stderr). Required files will be opened later.

  • Using a logfile, the console, or /dev/null as stdin, stdout, and stderr

實際上,當咱們查找守護進程的時候,沒有必要用到以上那麼多的標準,咱們只須要用兩條:

1. 沒有控制終端

2. session leader

因此咱們能夠用文中最開始提到的命令來查找:

ps -eo ppid,pid,sid,stat,tty,comm  | awk '{ if ($2 == $3 && $5 == "?") {print $0}; }'


E.g


chenqi@pek-qchen1-d1:~/test [1] $ ps -eo ppid,pid,sid,stat,tty,comm  | awk '{ if ($2 == $3 && $5 == "?") {print $0}; }'    0     1     1 Ss   ?        init    1   401   401 Ss   ?        systemd-udevd    1   435   435 Ssl  ?        rsyslogd    1   446   446 Ss   ?        dbus-daemon    1   520   520 Ss   ?        rpc.idmapd    1   530   530 Ss   ?        systemd-logind    1   575   575 Ss   ?        bluetoothd    1   750   750 Ss   ?        smbd    1   762   762 Ss   ?        rpcbind    1   779   779 Ss   ?        rpc.statd    1   829   829 Ssl  ?        ModemManager    1   950   950 Ssl  ?        NetworkManager    1   993   993 Ss   ?        cups-browsed    1  1424  1424 Ss   ?        nmbd    1  1550  1550 Ss   ?        acpid    1  1559  1559 Ss   ?        cron    1  1560  1560 Ss   ?        atd    1  1565  1565 Ss   ?        sshd    1  1612  1612 Ssl  ?        automount    1  1622  1622 Ssl  ?        named    1  1627  1627 Ssl  ?        whoopsie    1  1664  1664 Ss   ?        gdomap    1  1670  1670 Ss   ?        kerneloops    1  1721  1721 Ss   ?        rpc.mountd    1  1829  1829 Ss   ?        master    1  1998  1998 Ss   ?        apache2    1  2016  2016 SLsl ?        lightdm 2561  2588  2588 Ss   ?        init 2588  2653  2653 Ss   ?        dbus-daemon 2588  2656  2656 Ss   ?        ssh-agent 2588  2663  2663 Ss   ?        upstart-event-b 2588  2667  2667 Ss   ?        window-stack-br 2588  2668  2668 Ssl  ?        ibus-daemon 2588  2681  2681 Ssl  ?        unity-settings- 2588  2686  2686 Ssl  ?        hud-service 2588  2687  2687 Ssl  ?        at-spi-bus-laun 2588  2688  2688 Ssl  ?        gnome-session 2588  2693  2693 Ssl  ?        unity-panel-ser 2588  2786  2786 Ssl  ?        indicator-messa 2588  2789  2789 Ssl  ?        indicator-bluet 2588  2792  2792 Ssl  ?        indicator-power 2588  2799  2799 Ssl  ?        indicator-datet 2588  2800  2800 Ssl  ?        indicator-sound 2588  2803  2803 Ssl  ?        indicator-print 2588  2866  2866 Ssl  ?        indicator-sessi 2588  2879  2879 Ssl  ?        indicator-appli    1 24409 24409 Ss   ?        cupsd

相關文章
相關標籤/搜索