指定秒たってもまだ動いてたら自爆するスクリプト #
常時利用するもんでもないけど、たまに欲しい時あるので用意した
Linux とかだと /proc/
timeout.sh 5 sleep 10
とかすると、強制終了しているサマを確認できます
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -eq 0 ];then | |
echo 'USAGE: '$0' [second] command....' | |
exit; | |
fi | |
_CMD="$@" | |
_TIME=5 | |
if [ $# -gt 1 ];then | |
if [[ "$1" =~ ^[0-9]+$ ]];then | |
_TIME=$1 | |
_CMD=$(echo $_CMD|sed s/$1//) | |
fi | |
fi | |
#bash -c "sleep ${_TIME}; test -e /proc/$$ && kill -9 $$" & | |
bash -c 'sleep '${_TIME}'; test ! -z `ps -p '$$' -o"pid="` && kill -9 '$$ & | |
disown $! | |
exec $_CMD |