linux - Linux 中的终止命令

标签 linux bash command sleep kill

我有一个bash脚本abcd.sh,我想在其中终止这个命令(/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat') 5 秒后,但在此脚本中,它会在 5 秒后终止 sleep 命令。

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
sleep 5
kill $! 2>/dev/null && echo "Killed command on time out"

最佳答案

尝试

#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
pid=$!
sleep 5
kill $pid 2>/dev/null && echo "Killed command on time out"

更新:

一个工作示例(没有特殊命令)

#!/bin/sh
set +x
ping -i 1 google.de &
pid=$!
echo $pid
sleep 5
echo $pid
kill $pid 2>/dev/null && echo "Killed command on time out"

关于linux - Linux 中的终止命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22777618/

相关文章:

linux - 确定使用最大内存量 (RAM) 的进程?

linux - 需要扁平化目录,修改文件名。用bash?

Python 3 不会从 Git Bash 命令行运行

c++ - 使用最多 3 个命令读取输入命令

linux - 获取 git 修订之间删除的文件

c++ - 使用 Netbeans (linux/mac) 为 dylib 设置当前/兼容版本

mysql - 在 bash 中执行命令时出错

windows - for循环批处理文件

node.js - `npm i`命令有什么作用?

linux - 为 `ls` 创建别名,在 csh 中包含 `echo`