0

I have a process run by cron every hour. Such process calls another one (not sourced), ie: ./childProc.sh (Not sure if this detail is important)

Because of an endless loop in child process (which I'm still trying to find out why it does not end), I have to kill all processes related before run it again.

I've found this post and this article (Kill PPIDs section), but I don't understand the difference between: kill -9 -$PPID and kill -9 -- -$PPID.

My questions are:
What means here --? On the other hand, is -9 (SIGKILL) the right signal to kill a bash script process or is it too strong?

And lastly, my approach to check that behaviour (whether a process is still running) is creating a file that contains PID and PPID:

#!/bin/bash

# PID FILE 
PID_FILE="$BASE_BACKUP_DIR"/".pid.$HOSTNAME"; 

## If exists PID_FILE, kill 
if [[ -f "PID_FILE" ]]; then 
    pid_last=$(head -1 | cut -d " " -f1); 
    ppid_last=$(head -1 | cut -d " " -f2); 
    echoErr "Process is still running. Parent $ppid_last, child: $pid_last. "; 
    kill -9 -- -"$ppid_last"; 
fi;

## Create PID file
echo "$$ $PPID" > "$PID_FILE"; 

####### All process ####### 

# at the end, delete PID_FILE 
rm -f "$PID_FILE";
exit 0; 

What do you think? Any improvement?

2

There is no difference between kill -9 -$PPID and kill -9 -- -$PPID.

There is a difference between kill -$PPID and kill -- -$PPID. The -- marks the end of option arguments: any argument after --, even if it begins with a -, will not be treated as an option. Without the --, the -$PPID will be treated as the signal to be sent.

IMHO SIGKILL is too strong. Start with SIGINT or SIGHUP instead, or the default, SIGTERM.

  • I'll take a look at the differences of each signal, although I think they all will work because I do not control any signal, nor it is run with nohup or any other command that captures signals. Thanks. – Albert Dec 24 '14 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.