Kill:不是 pid 或有效的作业规范
我创建了一个应用程序,它基本上是一个用 C 编写的守护进程。它使用 shell 脚本停止和启动。具体来说,要停止它,可以使用kill 发送SIGTERM 信号。守护进程的 PID 以以下格式存储在光盘上的文件中:
1234\n
用户报告他们无法停止守护进程,shell 脚本返回错误:
kill: `': not a pid or valid job spec
PID 被获取并在 shell 脚本中使用,如下所示:
if [ -f "${PID_FILE}" ]
then
FCPID=`head -n 1 $PID_FILE`
kill -n SIGTERM "${FCPID}"
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
rm -f ${PID_FILE}
echo "OK"
else
echo "FAIL"
exit 1
fi
else
echo "Wasn't running"
exit 1
fi
工作正常在我的机器(Ubuntu 10.04)上,到目前为止没有其他人报告过这个问题。是否有人认识到该错误,或者 shell 脚本中是否存在可能在某些平台上导致问题的错误?
I have created an application that is basically a deamon written in C. It is stopped and started using a shell script. Specifically, to stop it, kill is used to send a SIGTERM signal. The PID of the daemon is stored in a file on the disc in the format:
1234\n
A user reports that they cannot stop the daemon, the shell script returns the error:
kill: `': not a pid or valid job spec
The PID is fetched and used in the shell script as follows:
if [ -f "${PID_FILE}" ]
then
FCPID=`head -n 1 $PID_FILE`
kill -n SIGTERM "${FCPID}"
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
rm -f ${PID_FILE}
echo "OK"
else
echo "FAIL"
exit 1
fi
else
echo "Wasn't running"
exit 1
fi
It works fine on my machine (Ubuntu 10.04) and so far no one else has reported this problem. Does anyone recognise the error or is there a mistake in the shell script that could cause problems on some platforms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您传递
kill
一个空参数作为 PID 时,就会发生该错误,即我的猜测是,当 PID_FILE 存在但为空时,您的脚本会抛出该错误,因此
${FCPID}
最终成为空字符串。检查启动脚本是否确实在用户计算机上正确写出 PID_FILE。
That error occurs when you pass
kill
an empty arg as a PID, i.e.My guess is your script throws up that error when the PID_FILE exists but is empty, hence
${FCPID}
ends up as an empty string.Check that the start script is actually writing out the PID_FILE correctly on your user's machine.
什么是
kill -n
?我认为你的意思是kill -s
。此外,我们还可以检查FCPID
是否已设置。What is
kill -n
? I think you meantkill -s
. Also, we can check thatFCPID
is being set.在 shebang 行中添加标志 -kvx(假设您使用的是 ksh),如下所示。
执行脚本后,您可以清楚地看到传递给kill命令的参数是什么。
Add the flag -kvx(assuming you are using ksh) in your shebang line like below.
After executing the script you could clearly see what is the argument that is getting passed to the kill command.