I want to create a shell script (kill.sh) that will determine the elapsed time of the current script (loop.sh, which I previously ran). when loop.sh reaches 10 minutes, kill.sh will kill the process. Please help
我想创建一个shell脚本(kill.sh),它将确定当前脚本(循环)的运行时间。sh,我之前运行过)。当循环。快到10分钟,杀了我。sh会扼杀这个过程。请帮助
EDIT:
编辑:
this would be the contents of the shell script loop.sh which will basically loops infinitely. I want to create a script that will determine the duration of the loop.sh and will kill it if reaches 10 minutes
这将是shell脚本循环的内容。sh基本上是无限循环的。我想创建一个脚本,它将决定循环的持续时间。如果达到10分钟,就杀了它
#!/bin/bash
while true
do
echo "Do something; hit [CTRL+C] to stop!"
done
the kill.sh could be something like this
杀死。sh可能是这样的。
#!/bin/bash
PROC_ID=$(ps -ef | grep "loop.sh" | grep -v grep | cut -c10-15)
...some codes here to get the duration of the loop.sh if it reaches 10 minutes.
if [ "$PROC_ID" != 0 -a (elapsed time is equal or more than 10 minutes) ];
then
kill -9 "$PROC_ID"
else
echo "NOT FOUND!"
fi
4 个解决方案
#1
1
This script help you to find the elapsed time in seconds
此脚本帮助您以秒为单位查找流逝的时间
#!/bin/bash
start=$(date +%s)
#do your work
end=$(date +%s)
elapsed=$(( $end - $start ))
echo "$elapsed sec"
EDIT:
编辑:
#/bin/bash
start=$(date +%s)
elapsed=0
while [ $elapsed -le 600 ] # 60*10=600 seconds
do
#do your work
end=$(date +%s)
elapsed=$(( $end - $start ))
done
kill -9 "$pid" #pid of the script
#2
1
Do something like below. Note -C
option for ps
isn't found on every system, so do something similar to what you were doing to get pid and time, though I would still use -o
(e.g. on some os x I would do something like ps -axo start,pid,command | awk '$NF == "loop.sh" {print $1 " " $2}'
or something similar). Also I wouldn't use kill -9
unless it won't exit for some reason otherwise.
下面做一些类似。注意-C选项对于ps并不是在每个系统上都可以找到,所以做一些类似于获取pid和时间的事情,尽管我仍然会使用-o(例如,在某些os x上,我会做一些类似ps -axo start、pid、命令| awk '$NF == "loop的事情。“打印$1”“$2}”或类似的东西。我也不会用kill -9,除非它不会因为某些原因退出。
#!/bin/bash
start=$(date -d $(ps -C loop.sh -o start=) '+%s' 2>/dev/null)
timeup=$(($start + 600 )) #10*60=600
pid=$(ps -C loop.sh -o pid=)
if [[ -z $pid ]]; then
echo "NOT FOUND!"
elif [[ $(date '+%s') > $timeup ]]; then
kill -9 "$pid"
fi
#3
1
How about starting the script in the background, then starting a sleep in the background as well, and waiting for either to finish. If the sleep finishes first, kill the script.
如何在后台启动脚本,然后在后台开始睡眠,等待完成。如果睡眠先结束,那么删除脚本。
loop.sh & { sleep 600; kill %1 2>/dev/null; }
#4
0
What you want is easily achieved via the sleep command:
你想要的很容易通过睡眠命令实现:
#!/bin/bash
PROC_ID=$(ps -ef | grep "loop.sh" | grep -v grep | cut -c10-15)
sleep 10m
if [ "$PROC_ID" != 0 ];
then
kill -9 "$PROC_ID"
else
echo "NOT FOUND!"
fi
#1
1
This script help you to find the elapsed time in seconds
此脚本帮助您以秒为单位查找流逝的时间
#!/bin/bash
start=$(date +%s)
#do your work
end=$(date +%s)
elapsed=$(( $end - $start ))
echo "$elapsed sec"
EDIT:
编辑:
#/bin/bash
start=$(date +%s)
elapsed=0
while [ $elapsed -le 600 ] # 60*10=600 seconds
do
#do your work
end=$(date +%s)
elapsed=$(( $end - $start ))
done
kill -9 "$pid" #pid of the script
#2
1
Do something like below. Note -C
option for ps
isn't found on every system, so do something similar to what you were doing to get pid and time, though I would still use -o
(e.g. on some os x I would do something like ps -axo start,pid,command | awk '$NF == "loop.sh" {print $1 " " $2}'
or something similar). Also I wouldn't use kill -9
unless it won't exit for some reason otherwise.
下面做一些类似。注意-C选项对于ps并不是在每个系统上都可以找到,所以做一些类似于获取pid和时间的事情,尽管我仍然会使用-o(例如,在某些os x上,我会做一些类似ps -axo start、pid、命令| awk '$NF == "loop的事情。“打印$1”“$2}”或类似的东西。我也不会用kill -9,除非它不会因为某些原因退出。
#!/bin/bash
start=$(date -d $(ps -C loop.sh -o start=) '+%s' 2>/dev/null)
timeup=$(($start + 600 )) #10*60=600
pid=$(ps -C loop.sh -o pid=)
if [[ -z $pid ]]; then
echo "NOT FOUND!"
elif [[ $(date '+%s') > $timeup ]]; then
kill -9 "$pid"
fi
#3
1
How about starting the script in the background, then starting a sleep in the background as well, and waiting for either to finish. If the sleep finishes first, kill the script.
如何在后台启动脚本,然后在后台开始睡眠,等待完成。如果睡眠先结束,那么删除脚本。
loop.sh & { sleep 600; kill %1 2>/dev/null; }
#4
0
What you want is easily achieved via the sleep command:
你想要的很容易通过睡眠命令实现:
#!/bin/bash
PROC_ID=$(ps -ef | grep "loop.sh" | grep -v grep | cut -c10-15)
sleep 10m
if [ "$PROC_ID" != 0 ];
then
kill -9 "$PROC_ID"
else
echo "NOT FOUND!"
fi