[arch-projects] [PATCH] added get_pid() and ck_pidfile() (FS#18654)
get_pid: Arguments: programname Returns: PID of program ck_pidfile: Arguments: PID-file programname Returns: 0 - PID in PID-file is of program 1 - PID in PID-file is not of program Signed-off-by: Andrwe Lord Weber <archlinux@andrwe.org> --- functions | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) diff --git a/functions b/functions index adf4ea9..9a741e1 100644 --- a/functions +++ b/functions @@ -231,6 +231,21 @@ ck_status() { fi } +# Return PID of $1 +get_pid() { + pidof -o %PPID $1 || return 1 +} + +# Check if PID-file $1 is still the active PID-file for command $2 +ck_pidfile() { + if [ -f "$1" ]; then + fpid=$(<"$1") + ppid=$(get_pid $2) + [ $fpid -eq $ppid ] && return 0 + fi + return 1 +} + # PIDs to be omitted by killall5 declare -a omit_pids -- 1.7.5.2
On Fri, Jun 10, 2011 at 04:20:33PM +0200, Andrwe Lord Weber wrote:
get_pid: Arguments: programname Returns: PID of program ck_pidfile: Arguments: PID-file programname Returns: 0 - PID in PID-file is of program 1 - PID in PID-file is not of program
Signed-off-by: Andrwe Lord Weber <archlinux@andrwe.org> --- functions | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/functions b/functions index adf4ea9..9a741e1 100644 --- a/functions +++ b/functions @@ -231,6 +231,21 @@ ck_status() { fi }
+# Return PID of $1 +get_pid() { + pidof -o %PPID $1 || return 1 +} + +# Check if PID-file $1 is still the active PID-file for command $2 +ck_pidfile() { + if [ -f "$1" ]; then
This is bash. Double square braces.
+ fpid=$(<"$1")
read -r fpid <"$1"
+ ppid=$(get_pid $2) + [ $fpid -eq $ppid ] && return 0
[[ "$fpid" = "$ppid" ]] Normally I'd agree with using an arithmetic comparison, but this would throw an error if the pidfile were to contain non numerical chars. We're never doing any mathmatical operations on these PIDs either, so the string compare makes more sense.
+ fi + return 1 +} + # PIDs to be omitted by killall5 declare -a omit_pids
--
Glad to see you've figured out git. d
Implemented suggestions of Dave. get_pid: Arguments: programname Returns: PID of program ck_pidfile: Arguments: PID-file programname Returns: 0 - PID in PID-file is of program 1 - PID in PID-file is not of program Signed-off-by: Andrwe Lord Weber <archlinux@andrwe.org> --- functions | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) diff --git a/functions b/functions index adf4ea9..1b077bf 100644 --- a/functions +++ b/functions @@ -231,6 +231,21 @@ ck_status() { fi } +# Return PID of $1 +get_pid() { + pidof -o %PPID $1 || return 1 +} + +# Check if PID-file $1 is still the active PID-file for command $2 +ck_pidfile() { + if [[ -f "$1" ]]; then + read -r fpid <"$1" + ppid=$(get_pid $2) + [[ "$fpid" = "$ppid" ]] && return 0 + fi + return 1 +} + # PIDs to be omitted by killall5 declare -a omit_pids -- 1.7.5.2
Ah, you beat me to it! Thanks. I'll have a look and test at the next opportunity. -t On Fri, Jun 10, 2011 at 5:00 PM, Andrwe Lord Weber <archlinux@andrwe.org> wrote:
Implemented suggestions of Dave.
get_pid: Arguments: programname Returns: PID of program ck_pidfile: Arguments: PID-file programname Returns: 0 - PID in PID-file is of program 1 - PID in PID-file is not of program
Signed-off-by: Andrwe Lord Weber <archlinux@andrwe.org> --- functions | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/functions b/functions index adf4ea9..1b077bf 100644 --- a/functions +++ b/functions @@ -231,6 +231,21 @@ ck_status() { fi }
+# Return PID of $1 +get_pid() { + pidof -o %PPID $1 || return 1 +} + +# Check if PID-file $1 is still the active PID-file for command $2 +ck_pidfile() { + if [[ -f "$1" ]]; then + read -r fpid <"$1" + ppid=$(get_pid $2) + [[ "$fpid" = "$ppid" ]] && return 0 + fi + return 1 +} + # PIDs to be omitted by killall5 declare -a omit_pids
-- 1.7.5.2
Thanks for your contribution! I'd be happy to merge something like this, once Dave's comments have been addressed. Cheers, Tom On Fri, Jun 10, 2011 at 4:20 PM, Andrwe Lord Weber <archlinux@andrwe.org> wrote:
get_pid: Arguments: programname Returns: PID of program ck_pidfile: Arguments: PID-file programname Returns: 0 - PID in PID-file is of program 1 - PID in PID-file is not of program
Signed-off-by: Andrwe Lord Weber <archlinux@andrwe.org> --- functions | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/functions b/functions index adf4ea9..9a741e1 100644 --- a/functions +++ b/functions @@ -231,6 +231,21 @@ ck_status() { fi }
+# Return PID of $1 +get_pid() { + pidof -o %PPID $1 || return 1 +} + +# Check if PID-file $1 is still the active PID-file for command $2 +ck_pidfile() { + if [ -f "$1" ]; then + fpid=$(<"$1") + ppid=$(get_pid $2) + [ $fpid -eq $ppid ] && return 0 + fi + return 1 +} + # PIDs to be omitted by killall5 declare -a omit_pids
-- 1.7.5.2
participants (3)
-
Andrwe Lord Weber
-
Dave Reisner
-
Tom Gundersen