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

首先,要注意,守护进程(daemon)和后台进程(background process)有区别。

守护进程是一种后台进程,但是,同时,它必须具备以下特性:

1. 没有控制它的tty

2. 必须是一个session leader

3. 必须是一个进程组的leader

4. 执行后台任务

5. root目录即为工作目录

6. umask设置为0

7. 文件描述符(file descriptor)都必须先关闭,然后再关联相应文件(或者设备),如果有需要的话。

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.

实际上,当我们查找守护进程的时候,没有必要用到以上那么多的标准,我们只需要用两条:

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

Logo

更多推荐