杀-9 +禁用来自kill命令的消息(标准输出)

发布于 2024-09-06 07:20:02 字数 679 浏览 6 评论 0原文

我编写了以下脚本,如果 grep 在文件中找不到相关字符串,该脚本将启用 20 秒的超时。

该脚本运行良好,但脚本的输出如下:

./test: line 11: 30039: Killed
  1. 如何从kill 命令禁用此消息?

  2. 如果进程不存在,如何告诉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
  1. how to disable this message from the kill command?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

霊感 2024-09-13 07:20:02

kill -9 $RESULT > /dev/null

这会将 stdoutstderr 发送到 /dev/null。

kill -9 $RESULT &> /dev/null

This will send stdout and stderr to /dev/null.

烟柳画桥 2024-09-13 07:20:02

你最好看看timeout命令

man timeout

NAME
       timeout - run a command with a time limit

SYNOPSIS
       timeout [OPTION] NUMBER[SUFFIX] COMMAND [ARG]...
       timeout [OPTION]

DESCRIPTION
       Start  COMMAND,  and  kill  it if still running after NUMBER seconds.  SUFFIX may be `s' for
       seconds (the default), `m' for minutes, `h' for hours or `d' for days.

you'd better look at timeout command

man timeout

NAME
       timeout - run a command with a time limit

SYNOPSIS
       timeout [OPTION] NUMBER[SUFFIX] COMMAND [ARG]...
       timeout [OPTION]

DESCRIPTION
       Start  COMMAND,  and  kill  it if still running after NUMBER seconds.  SUFFIX may be `s' for
       seconds (the default), `m' for minutes, `h' for hours or `d' for days.
折戟 2024-09-13 07:20:02

消息是由您的 shell 打印的,而不是由被杀死的进程打印的。

尝试在另一个 shell 中运行要被杀死的进程,封装被杀死的命令,如下所示:

sh -c 'command_to_be_inettrupted&'

这个想法是让 shell 实例比它启动的进程更早退出。您可能还需要“nohup”您的命令,但这在我的系统上是不必要的。

例如:

sh -c 'sleep 10&' ; sleep 1; killall sleep

尽管第一个睡眠实例被杀死,但此代码不会产生任何输出。

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:

sh -c 'command_to_be_inettrupted&'

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:

sh -c 'sleep 10&' ; sleep 1; killall sleep

This code won't produce any output, despite first sleep instance being killed.

離人涙 2024-09-13 07:20:02

我认为这个信息来自于工作控制。尝试使用 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

国粹 2024-09-13 07:20:02

在 C:

/* Kill process without printing 'Killed' or 'Terminated' message. */
kill(pid, SIGINT);

execl("/bin/bash", "bash", "-c", "kill -SIGINT `pidof <your command>` >/dev/null 2>&1", (char *)NULL);

killpid.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
/*
    $ gcc -W -Wall -O3 -std=c99 -pedantic -o killpid killpid.c
*/
#define LEN     10

int main()
{
    char line[LEN] = {0};
    FILE *cmd = popen("pidof <your command>", "r");

    fgets(line, LEN, cmd);
    pid_t pid = strtoul(line, NULL, 10);
    printf("pid: %ld\n", pid);

    /* Kill process without printing 'Killed' or 'Terminated' message.*/
    kill(pid, SIGINT);
    pclose(cmd);

    return 0;
}

中编程通过kill 命令:

$ kill -SIGINT `ps -ef | grep <your command> | awk '{print $2}'` >/dev/null 2>&1

$ kill -SIGINT `pidof <your command>`

Programming in C:

/* Kill process without printing 'Killed' or 'Terminated' message. */
kill(pid, SIGINT);

or

execl("/bin/bash", "bash", "-c", "kill -SIGINT `pidof <your command>` >/dev/null 2>&1", (char *)NULL);

killpid.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
/*
    $ gcc -W -Wall -O3 -std=c99 -pedantic -o killpid killpid.c
*/
#define LEN     10

int main()
{
    char line[LEN] = {0};
    FILE *cmd = popen("pidof <your command>", "r");

    fgets(line, LEN, cmd);
    pid_t pid = strtoul(line, NULL, 10);
    printf("pid: %ld\n", pid);

    /* Kill process without printing 'Killed' or 'Terminated' message.*/
    kill(pid, SIGINT);
    pclose(cmd);

    return 0;
}

By kill command:

$ kill -SIGINT `ps -ef | grep <your command> | awk '{print $2}'` >/dev/null 2>&1

or

$ kill -SIGINT `pidof <your command>`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文