check exists, return 0
$ pgrep httpd
46775
$ kill -0 46775
$ echo $?
0
check non-exists, return 1
$ kill -0 999999899
bash: kill: (999999899) - No such process
$ echo $?
1
kill process id stored in .pid file
#!/bin/sh
pid_file=/var/run/process.pid
kill -0 $(cat $pid_file) 2>/dev/null
if [ $? -eq 0 ]; then
kill $(cat $pid_file)
fi
时间: 2024-11-13 06:54:24