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

Stella981
• 阅读 595

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
芝士年糕 芝士年糕
1年前
如何在 Linux 命令行中终止进程?
如果你想在linux上停止某个进程,你会怎么操作?如果命令/进程在前台运行,您可以使用CtrlC终端快捷方式,但是,如果进程不可见(在后台运行),您可以使用专用命令“杀死它”。“终止进程”是指在执行过程中停止进程,如果您知道进程ID(PID),则可以使用kill命令,如下所示:kill在上面的语法中,signal指的是要发送终止的终止信号,
Easter79 Easter79
2年前
think
Supervisor的安装与使用入门在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。由于在linux中,每个系统与用户进行交流的界面称为终端,每一个从此终端开始运行的进程都会依附于这个终端,这个终端被称为这些进程的控制终端,当控制终端被关闭的时候,相
Easter79 Easter79
2年前
supervisor运行golang守护进程
Supervisor是一个C/S系统,它可以在类UNIX系统上控制系统进程,由python编写,它提供了大量的功能来实现对进程的管理。程序的多进程启动,可以配置同时启动的进程数,而不需要一个个启动程序的退出码,可以根据程序的退出码来判断是否需要自动重启程序所产生日志的处理进程初始化的环境,包括目录,用户,umask,
Stella981 Stella981
2年前
Linux进程守护——Supervisor 使用记录
0、旁白Supervisor是个父进程,你要守护的进程会以Supervisor的子进程形式存在,所以老子才可以管儿子官网链接:http://supervisord.org/(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fsupervisord.org%2F)【5、参数
Wesley13 Wesley13
2年前
4、jstack查看线程栈信息
1、介绍利用jps、top、jstack命令找到进程中耗时最大的线程,以及线程状态等等,同时最后还可以显示出死锁的线程查找:FoundoneJavaleveldeadlock即可1、jps获得进程号!(https://oscimg.oschina.net/oscnet/da00a309fa6
Stella981 Stella981
2年前
PostgreSQL死锁进程及慢查询处理
1、死锁进程查看:SELECTFROMpg_stat_activityWHEREdatname'数据库名称'andwaitingtrue;pid进程id。2、慢查询SQL:selectdatname,pid,usename,application_name,client_addr,client
Stella981 Stella981
2年前
Python实现守护进程
概念守护进程(Daemon)也称为精灵进程是一种生存期较长的一种进程。它们独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。他们常常在系统引导装入时启动,在系统关闭时终止。unix系统有很多守护进程,大多数服务器都是用守护进程实现的,例如inetd守护进程。需要了解的相关概念进程(process)
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
稚然 稚然
2个月前
探索Android底层开发
//下仔のke:https://yeziit.cn/15444/在Unix和Linux系统中,init是系统启动后由内核首先启动的用户级进程,其进程ID(PID)始终为1。init进程负责启动和管理系统服务、配置系统环境以及处理系统或服务的终止等任务。在现