Reputation: 1428
I'm trying to write a script to kill a process by ID if it is older than 5 minutes. I already know the process ID in $pid
.
pid=1234
# 300 seconds = 5min
maximum_runtime=300
process_start_time=`ps -o lstart= -p $pid`
current_time=`date +%s`
let diff=${current_time}-${process_start_time}
if [ $diff -gt $maximum_runtime ]
then
kill -3 $pid
fi
This results in an error:
./script.sh: line 9: let: 11:41:01: syntax error in expression (error token is ":41:01")
I used some code from this answer. Any ideas how to resolve this?
Upvotes: 1
Views: 110
Reputation: 156
pid=1234
# 300 seconds = 5min
maximum_runtime=300
process_start_time=`ps -o lstart= -p $pid`
current_time=`date` #got the tiem in same format
let diff="$(($(date -d "$current_time" '+%s') - $(date -d "$process_start_time" '+%s')))"
#result for diff in secconds, calculated using date tool
if [ $diff -gt $maximum_runtime ]
then
kill -3 $pid
fi
Upvotes: 1