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

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

nanshan 2024-10-26 11:09 27 浏览 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
	}
}

相关推荐

Linux/Unix 系统中非常常用的命令

Linux/Unix系统中非常常用的命令,它们是进行文件操作、文本处理、权限管理等任务的基础。下面是对这些命令的简要说明:**文件操作类:*****`ls`(list):**列出目录内容,显...

教你如何在Linux中删除分区(CLI篇)

文接上篇,继续以Ubuntu系统为例。删除分区前,急得重要数据备份!备份!备份用命令操作分区,用的最多的莫过于fdisk了,几乎所有的Linux发行版都默认带有fdisk。首先要知道的是,你想删除的分...

敲完就让你提桶跑路的Linux命令(敲完就让你提桶跑路的linux命令是什么)

不谨慎可能就会让你提桶的Linux命令!!!删除文件rm-rf该命令是删除文件或文件夹等最快的方式之一。删除后的内容很难恢复,如果删除系统文件可能会导致系统崩坏。>rm-rf/#强制...

Log文件可以删除吗(taxukeylog文件可以删除吗)

Log文件(日志文件)是否可以删除取决于具体场景和文件类型。以下是详细分析和建议:一、哪些Log文件可以删除?非关键应用日志用户级应用日志:如浏览器缓存日志、游戏临时日志等,通常不影响系统运行,可定期...

Linux 删除空目录(linux直接删除目录)

rmdir命令用来删除空目录。当目录不再被使用时,或者磁盘空间已到达使用限定值,就需要删除失去使用价值的目录。利用rmdir命令可以从一个目录中删除一个或多个空的子目录。该命令从一个目录中删除一个或...

在 Windows 11 或 10 上删除、创建和格式化分区

在Windows11或10上删除、创建和格式化分区假设您的现有电脑使用的是传统硬盘,但现在您想再添加一个硬盘或SSD。当然,后者将用于启动操作系统,而前者将作为纯数据存储。在成功将操作系统...

如何使用 Apt Clean 命令清除 APT 缓存?

APT(AdvancedPackageTool)是Debian系Linux发行版的包管理工具,用于处理软件包的安装、升级和依赖管理。在使用apt命令(如aptinstall、apt...

Linux 磁盘空间不够用?5 招快速清理文件,释放 10GB 空间不是梦!

刚收到服务器警告:磁盘空间不足90%!装软件提示Nospaceleftondevice!连日志都写不进去,系统卡到崩溃?别慌!今天教你5个超实用的磁盘清理大招,从临时文件到无用软件一键搞定...

Linux清空日志方法(linux怎么清理日志)

方法1:使用>重定向>/path/to/logfile或(需要权限时):sudosh-c'>/var/log/logfile'方法2:使用trun...

如何在Eclipse中搭建Zabbix源码的调试和开发环境

Zabbix是一款非常优秀的企业级软件,被设计用于对数万台服务器、虚拟机和网络设备的数百万个监控项进行实时监控。Zabbix是开放源码和免费的,这就意味着当出现bug时,我们可以很方便地通过调试源码来...

Linux操作系统之常用命令(linux操作系统之常用命令有哪些)

Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...

理解linux内核的vmlinuz和initrd(linux内核原理及分析)

Originaladdress:http://www.chenjunlu.com/2010/11/understanding-of-vmlinuz-initrd-and-system-map/1....

Linux纯干货知识总结|面试专用(linux面试宝典)

学习Linux的重要性相信不用我多说大家也明白,以下是小编总结的常用Linux基础知识以及面试常问的Linux命令,希望能帮助大家更规范地理解和使用~绝对路径和相对路径绝对路径以正斜杠开始完整的文件的...

Linux基础知识之启动流程分析(简述linux启动流程)

Linux系统启动原理:1.poweron开机。2.开机自检:电脑开机后首先加载BIOS(BasicInput/OutputSystem基本输入输出系统)。BIOS程序首先检查计算机能否满足运...

Java程序员必备——Linux的面试常见问题及面试题!你知道多少?

一.常用命令1.编辑相关①.awkNF:字段总数NR:第几行数据FS:分隔字符②.sed-n-i直接修改4a:在第四行后添加4i:在第四行前插入1,5csting:用sting替换1到5行...

取消回复欢迎 发表评论: