diff options
author | jow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2010-09-19 14:03:32 +0000 |
---|---|---|
committer | jow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73> | 2010-09-19 14:03:32 +0000 |
commit | 69b947b01b59fdc9440ec9684d28498f2a09cb3d (patch) | |
tree | 93b925d18a0c66903fe45516431cb37bd15b177d /package/base-files/files/etc/functions.sh | |
parent | 3e2b2b02c5f09ba16b817b93681e6ea9f1333f3d (diff) |
[package] base-files: introduce service_kill() into functions.sh - a convenience wrapper for killing services by pid or pidfile with a grace period for termination before kill
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@23087 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/base-files/files/etc/functions.sh')
-rwxr-xr-x | package/base-files/files/etc/functions.sh | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/package/base-files/files/etc/functions.sh b/package/base-files/files/etc/functions.sh index d5ffc13535..c5df49970b 100755 --- a/package/base-files/files/etc/functions.sh +++ b/package/base-files/files/etc/functions.sh @@ -275,4 +275,21 @@ uci_apply_defaults() { uci commit } +service_kill() { + local name="${1}" + local pid="${2:-$(pidof "$name")}" + local grace="${3:-5}" + + [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)" + + for pid in $pid; do + [ -d "/proc/$pid" ] || continue + local try=0 + kill -TERM $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done + kill -KILL $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done + done +} + [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh |