Linux查找所有正在运行的守护进程(daemon)

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.

  • 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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

如何在 Linux 命令行中终止进程?

如果你想在linux上停止某个进程,你会怎么操作?如果命令/进程在前台运行,您可以使用CtrlC终端快捷方式,但是,如果进程不可见(在后台运行),您可以使用专用命令“杀死它”。“终止进程”是指在执行过程中停止进程,如果您知道进程ID(PID),则可以使用kill命令,如下所示:kill在上面的语法中,signal指的是要发送终止的终止信号,

think

Supervisor的安装与使用入门在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。由于在linux中,每个系统与用户进行交流的界面称为终端,每一个从此终端开始运行的进程都会依附于这个终端,这个终端被称为这些进程的控制终端,当控制终端被关闭的时候,相