我有一个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/