diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-11-28 00:04:59 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2014-11-28 00:04:59 -0500 |
commit | 30c42d2c3f6a9ce65e85363601abd1e469a32a2f (patch) | |
tree | 5e66ec9555b0afdf7152d3f353d33409928dffe4 /offlineimap-killer.sh | |
parent | b93c35a73ebc8076758645b34232d67c72e96205 (diff) |
Cleanup of offlineimap utilities.
Diffstat (limited to 'offlineimap-killer.sh')
-rw-r--r-- | offlineimap-killer.sh | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/offlineimap-killer.sh b/offlineimap-killer.sh new file mode 100644 index 0000000..91f9509 --- /dev/null +++ b/offlineimap-killer.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# Usage: offlineimap-killer LIMIT +# +# Kills any instance of offlineimap that has been running longer than +# LIMIT seconds. + +pid_uptime() { + local pid=$1 + local sec_uptime=$(cut -d ' ' -f1 /proc/uptime) + local tic_started_at + tic_started_at=$(cut -d ' ' -f 22 /proc/${pid}/stat 2>/dev/null) || + { echo 0; return 0; } + local tic_per_sec=$(getconf CLK_TCK) + + bc <<<"${sec_uptime} - (${tic_started_at}/${tic_per_sec})" +} + +main() { + declare -i limit=$1 + local pids=($(pgrep -x offlineimap)) + local pid + for pid in "${pids[@]}"; do + declare -i uptime=$(pid_uptime $pid) + if [[ "$uptime" -gt "$pid" ]]; then + printf 'Killing %d which has been running for %d seconds' "$pid" "$uptime" + kill -9 "$pid" + fi + done +} + +main "$@" |