百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

关于kill -0 pid的作用(kill -int pid)

nanshan 2024-10-26 11:09 20 浏览 0 评论

在服务器运维以及程序开发过程中,经常会用到kill命令或者kill()方法。那么,kill是做什么以及信号0的作用又是什么,一起来探寻吧。


kill可以使用的信号

[root@localhost ~]# kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	
34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX

注意:
下面内容常作为面试题:前31个信号和后31个信号的区别?

在Linux上执行kill -l看到可使用信号共有62个(仔细看没有32、33哦)。

  • 编号为1 ~ 31的信号为传统UNIX支持的信号,是不可靠信号(非实时的)
  • 编号为34 ~ 64的信号是后来扩充的,称做可靠信号(实时信号)

不可靠信号、可靠信号区别:前者不支持排队,可能会造成信号丢失,而后者不会


kill的文档描述

通过man命令可以看到关于kill指令的描述以及参数解释,这里截取部分描述,如下:

[root@localhost ~]# man 1 kill

KILL(1)                                                                      User Commands                                                                      KILL(1)

NAME
       kill - terminate a process

SYNOPSIS
       kill [-s signal|-p] [-q sigval] [-a] [--] pid...
       kill -l [signal]

DESCRIPTION
       The  command  kill  sends  the specified signal to the specified process or process group.  If no signal is specified, the TERM signal is sent.  The TERM signal
       will kill processes which do not catch this signal.  For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.

       Most modern shells have a builtin kill function, with a usage rather similar to that of the command described here.  The '-a' and '-p' options, and  the  possi‐
       bility to specify processes by command name are a local extension.

       If sig is 0, then no signal is sent, but error checking is still performed.
       ......
       

# [root@localhost ~]# man 2 kill
KILL(2)                                                                Linux Programmer's Manual                                                                KILL(2)

NAME
       kill - send signal to a process

SYNOPSIS
       #include <sys/types.h>
       #include <signal.h>

       int kill(pid_t pid, int sig);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       kill(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE

DESCRIPTION
       The kill() system call can be used to send any signal to any process group or process.

       If pid is positive, then signal sig is sent to the process with the ID specified by pid.

       If pid equals 0, then sig is sent to every process in the process group of the calling process.

       If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init), but see below.

       If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.

       If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

       For  a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL capability), or the real or effective user ID of
       the sending process must equal the real or saved set-user-ID of the target process.  In the case of SIGCONT it suffices when the sending and receiving processes
       belong to the same session.

从描述可知,无论是man 1文档还是man 2文档都指出:kill命令用于向指定的pid进程发送信号,进程在接收到对应的信号之后会进行对应的操作。


关于信号0的作用

man 1 文档中有一句 If sig is 0, then no signal is sent, but error checking is still performed, 意思是:如果sig为0,则不发送信号,但仍然进行错误检查。

man 2 文档中有一句 If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID,意思是:如果sig为0,则不发送信号,但仍然进行错误检查; 这可以用来检查是否存在进程ID或进程组ID。

也就是说,kill -0 pid 执行时不会发送信号,但是会对pid对应进程做错误检查。如果返回0则进程、服务正在运行中;反之是其他值,则进程死了或者服务已停止。


信号0的用法

既然,信号kill -0 pid不发送信号,主要用于检查对应进程做错误检查。那么,在开发中我们就可以通过kill返回的错误信息来判断进程是否存在、正常运行。

shell脚本中示例:

#!/bin/bash

PIDFILE=$1

if [ -f $PIDFILE ]; then
	  PID="$(cat $PIDFILE)"
	
    if kill -0 "$PID" &> /dev/null; then
      echo "process is exists"
      exit 0
    else
      echo "process is not exists"
      exit 5
    fi
fi

Go代码中示例:

func processExists(pid int) bool {
	if err := syscall.Kill(pid, 0); err == nil {
		return true
	} else {
		return false
	}
}

相关推荐

【华纳云】用Ubuntu镜像配置防火墙保护VPS服务器,保姆式教学

保姆式教学:如何用Ubuntu22.04镜像配置UFW防火墙保护VPS服务器?今天讲使用UFW配置防火墙来保护服务器。UFW是uncomplicatedfirewall的简称,是IPtables的...

网络&amp;多任务 day03-网络编程基础-IP地址查看

目标知道使用ifconfig命令可以查询主机的IP地址知道使用ping命令可以查看网络的连通性#1.虚拟机网卡设置NAT(网络地址转换模式):则虚拟机会使用主机VMnet8这块虚拟网卡与...

怎么把旧电脑改私有云,如何把旧电脑改私有云?

随着科技的飞速发展,许多人家中都有了闲置的旧电脑。这些电脑虽然性能不再顶尖,但仍具备相当的存储和计算能力。将它们改造成私有云,不仅可以实现资源的再利用,还能为家庭或小型团队提供一个安全、便捷的数据存储...

多功能开源终端Wave Terminal安装与远程连接内网Linux服务器教程

前言本文主要介绍一款多功能高颜值的跨平台开源终端WaveTerminal在Windows电脑上如何安装,并结合cpolar内网穿透工具轻松实现跨网络远程连接本地内网Linux服务器,无需公网IP。作...

ip地址管理之phpIPAM保姆级安装教程 (原创)

本教程基于Ubuntu24.04LTS,安装phpIPAM(最新稳定版1.7),使用Apache、PHP8.3和MariaDB,遵循最佳实践,确保安全性和稳定性。一、环境准备1....

ubuntu安装Paperless-ngx强大的文档管理工具并实现远程使用

前言在当今快节奏的办公环境中,文档管理成为了一个不可忽视的问题。想象一下这样的场景:你需要一份重要的合同,却在堆积如山的文件中迷失了方向。你手忙脚乱地翻找,汗水顺着额头滴落,心里默念:“快出现吧,合同...

ubuntu服务器运维

1.安装上传下载命令:直接执行命令就可以:sudoaptupdatesudoaptinstalllrzsz-y然后就能看到执行过程了2.安装mysql安装MySQLServersudo...

搭建nginx反向代理用作内网域名转发

情景由于公司内网有多台服务器的http服务要映射到公司外网静态IP,如果用路由的端口映射来做,就只能一台内网服务器的80端口映射到外网80端口,其他服务器的80端口只能映射到外网的非80端口。非80端...

VMware Workstation环境下DNS的安装配置,并使用ubuntu来测试

需求说明:某企业信息中心计划使用IP地址17216.11.0用于虚拟网络测试,注册域名为xyz.net.cn.并将172.16.11.2作为主域名的服务器(DNS服务器)的IP地址,将172.16.1...

如何在Linux中配置网络连接和设置静态IP地址?

#挑战30天在头条写日记#在Linux中配置网络连接并设置静态IP地址需要执行以下步骤和具体细节数据。请注意,以下步骤适用于基于Debian的Linux发行版(如Ubuntu),在其他发行版中略有不同...

Linux设置静态IP

测试服务器OS:Centos6.5x64本机OS:Ubuntu14.04x64由于Virtualbox当时安装Centos6.5的时候设置的是自动获取的IP,所以局域网内每次启动,IP有...

在不同Linux发行版中,如何静态IP地址?本文值得收藏!

想象一下,你的Linux服务器正在运行一个关键服务,比如Web服务器或文件共享平台,突然因为IP地址变更,外部设备无法连接,服务中断。这种情况在动态IP分配(DHCP)环境下并不少见。静态IP地址就像...

ubuntu 22.04配置固定IP

vi/etc/netplan/00-installer-config.yaml保存网卡配置:netplanapply##如果有报错请检查00-installer-config.yaml网卡关...

Ubuntu操作系统如何修改DNS

Ubuntu操作系统下有时难免需要进行DNS的修改,修改DNS的目的可以让你的上网速度有所提升。或者软件下载速度提升。下面介绍如何在ubunru系统修改对应的DNS方法一:使用gedit编辑器进行DN...

VMWare虚拟机下为Ubuntu 12.04.1配置静态IP(NAT方式)

背景在虚拟机下运行操作系统,尤其是Linux系统已经是非常常见的做法。有时你想在虚拟机下搭建一个(模拟)服务器来供主机访问,比如搭建一个telnet/ssh。此时你会发现,每次启动虚拟机,VMWare...

取消回复欢迎 发表评论: