杀-9 +禁用来自kill命令的消息(标准输出)
我编写了以下脚本,如果 grep 在文件中找不到相关字符串,该脚本将启用 20 秒的超时。
该脚本运行良好,但脚本的输出如下:
./test: line 11: 30039: Killed
如何从kill 命令禁用此消息?
如果进程不存在,如何告诉kill命令忽略?
谢谢
雅埃尔
#!/bin/ksh
( sleep 20 ; [[ ! -z ` ps -ef | grep "qsRw -m1" | awk '{print $2}' ` ]] && kill -9 2>/dev/null ` ps -ef | grep "qsRw -m1" | awk '{print $2}' ` ; sleep 1 ) &
RESULT=$!
print "the proccess:"$RESULT
grep -qsRw -m1 "monitohhhhhhhr" /var
if [[ $? -ne 0 ]]
then
print "kill "$RESULT
kill -9 $RESULT
fi
print "ENDED"
./test
the proccess:30038
./test: line 11: 30039: Killed
kill 3003
I wrote the following script which enables timeout of 20 seconds if grep can not find the relevant string in the file.
The script works well, but the output from the script is like this:
./test: line 11: 30039: Killed
how to disable this message from the kill command?
how to tell kill command to ignore if process not exist?
THX
Yael
#!/bin/ksh
( sleep 20 ; [[ ! -z ` ps -ef | grep "qsRw -m1" | awk '{print $2}' ` ]] && kill -9 2>/dev/null ` ps -ef | grep "qsRw -m1" | awk '{print $2}' ` ; sleep 1 ) &
RESULT=$!
print "the proccess:"$RESULT
grep -qsRw -m1 "monitohhhhhhhr" /var
if [[ $? -ne 0 ]]
then
print "kill "$RESULT
kill -9 $RESULT
fi
print "ENDED"
./test
the proccess:30038
./test: line 11: 30039: Killed
kill 3003
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
kill -9 $RESULT > /dev/null
这会将
stdout
和stderr
发送到 /dev/null。kill -9 $RESULT &> /dev/null
This will send
stdout
andstderr
to /dev/null.你最好看看
timeout
命令you'd better look at
timeout
command消息是由您的 shell 打印的,而不是由被杀死的进程打印的。
尝试在另一个 shell 中运行要被杀死的进程,封装被杀死的命令,如下所示:
这个想法是让 shell 实例比它启动的进程更早退出。您可能还需要“nohup”您的命令,但这在我的系统上是不必要的。
例如:
尽管第一个睡眠实例被杀死,但此代码不会产生任何输出。
Messages are printed by your shell, not by the killed process.
Try runnning the proccess to be killed in an another shell, encapsulating command being killed like this:
The idea is to make the shell instance exit earlier than the process it started. You may also need to "nohup" your command, but that was unnecessary on my system.
For example:
This code won't produce any output, despite first sleep instance being killed.
我认为这个信息来自于工作控制。尝试使用 set +m 将其关闭
如果这在 ksh 下不起作用,请尝试使用 #!/bin/bash 执行脚本
I think this message comes from job control. Try turning it off with set +m
if that doesn't work under ksh, try the script with #!/bin/bash
在 C:
或
killpid.c
中编程通过kill 命令:
或
Programming in C:
or
killpid.c
By kill command:
or