3 added 270 characters in body
source | link

For those that just want to just kill a process and wait for it to die, but not indefinitely:

Max. 10 seconds gracefull close + 10It waits max 60 seconds brutal killper signal type.

Warning: This answer is in no way related to traping a kill signal and dispatching it.

# close_app_sub GREP_STATEMENT SIGNAL DURATION_SEC
# GREP_STATEMENT must not match itself!
close_app_sub() {
    APP_PID=$(ps -x | grep "[M]yprocess_name""$1" | grep -oP '^\s*\K[0-9]+' --color=never)
    if [ ! -z "$APP_PID" ]; then
        echo "App is open. ClosingTrying appto gracefullyclose app (SIGHUPSIGNAL $2). Max $3sec."
        kill -HUP$2 "$APP_PID"
        WAIT_LOOP=0
        while ps -p "$APP_PID" > /dev/null 2>&1; do
            sleep 1
            WAIT_LOOP=$((WAIT_LOOP+1))
            if [ "$WAIT_LOOP" = 10"$3" ]; then
                break
            fi
        done
    fi
 
    APP_PID=$(ps -x | grep "[M]yprocess_name""$1" | grep -oP '^\s*\K[0-9]+' --color=never)
    if [ ! -z "$APP_PID" ]; then
    echoreturn "Failed.0; Forceelse killingreturn app"$APP_PID"; (SIGTERM)"fi
    kill "$APP_PID"}
   
close_app() WAIT_LOOP=0{
    whileclose_app_sub ps"$1" "-p "$APP_PID" > /dev/null 2>&1;HUP" do"60"
      close_app_sub "$1" sleep"-TERM" 1"60"
     close_app_sub "$1" "-SIGINT" WAIT_LOOP=$((WAIT_LOOP+1))"60"
        if [ "$WAIT_LOOP" =close_app_sub 10"$1" ];"-KILL" then"60"
           return break$?
        fi}
    done
ficlose_app "[f]irefox"

It selects the app to kill by name or arguments. Keep the brackets for the first letter of the app name to avoid matching grep itself.

With some changes, but you can directly use the PID also, or use a simpler pidof process_name. Keep the brackets for the first letter instead of the app name to avoid matching grep itselfps statement.

Code details: Final grep is to get the PID without the trailing spaces.

For those that just want to just kill a process and wait for it to die, but not indefinitely:

Max. 10 seconds gracefull close + 10 seconds brutal kill.

Warning: This answer is in no way related to traping a kill signal and dispatching it.

APP_PID=$(ps -x | grep "[M]yprocess_name" | grep -oP '^\s*\K[0-9]+')
if [ ! -z "$APP_PID" ]; then
    echo "App is open. Closing app gracefully (SIGHUP)"
    kill -HUP "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi
 
APP_PID=$(ps -x | grep "[M]yprocess_name" | grep -oP '^\s*\K[0-9]+')
if [ ! -z "$APP_PID" ]; then
    echo "Failed. Force killing app (SIGTERM)"
    kill "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

It selects the app to kill by name or arguments, but you can directly use the PID also, or use a simpler pidof process_name. Keep the brackets for the first letter of the app name to avoid matching grep itself.

Code details: Final grep is to get the PID without the trailing spaces.

For those that just want to kill a process and wait for it to die, but not indefinitely:

It waits max 60 seconds per signal type.

Warning: This answer is in no way related to traping a kill signal and dispatching it.

# close_app_sub GREP_STATEMENT SIGNAL DURATION_SEC
# GREP_STATEMENT must not match itself!
close_app_sub() {
    APP_PID=$(ps -x | grep "$1" | grep -oP '^\s*\K[0-9]+' --color=never)
    if [ ! -z "$APP_PID" ]; then
        echo "App is open. Trying to close app (SIGNAL $2). Max $3sec."
        kill $2 "$APP_PID"
        WAIT_LOOP=0
        while ps -p "$APP_PID" > /dev/null 2>&1; do
            sleep 1
            WAIT_LOOP=$((WAIT_LOOP+1))
            if [ "$WAIT_LOOP" = "$3" ]; then
                break
            fi
        done
    fi
    APP_PID=$(ps -x | grep "$1" | grep -oP '^\s*\K[0-9]+' --color=never)
    if [ -z "$APP_PID" ]; then return 0; else return "$APP_PID"; fi
}

close_app() {
    close_app_sub "$1" "-HUP" "60"
    close_app_sub "$1" "-TERM" "60"
    close_app_sub "$1" "-SIGINT" "60"
    close_app_sub "$1" "-KILL" "60"
    return $?
}

close_app "[f]irefox"

It selects the app to kill by name or arguments. Keep the brackets for the first letter of the app name to avoid matching grep itself.

With some changes, you can directly use the PID or a simpler pidof process_name instead of the ps statement.

Code details: Final grep is to get the PID without the trailing spaces.

2 added 93 characters in body
source | link

For those that just want to just kill a process and wait for it to die, but not indefinitely:

Max. 10 seconds gracefull close + 10 seconds brutal kill.

Warning: This answer is in no way related to traping a kill signal and dispatching it.

APP_PID=$(ps -x | grep "[M]yprocess_name" | cutgrep -d" "oP '^\s*\K[0-f 19]+')
if [ ! -z "$APP_PID" ]; then
    echo "App is open. Closing app gracefully (SIGHUP)"
    kill -HUP "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

APP_PID=$(ps -x | grep "[M]yprocess_name" | cutgrep -d" "oP '^\s*\K[0-f 19]+')
if [ ! -z "$APP_PID" ]; then
    echo "Failed. Force killing app (SIGTERM)"
    kill "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

It selects the app to kill by name or arguments, but you can directly use the PID also, or use a simpler pidof process_name. Keep the brackets for the first letter of the app name to avoid matching grep itself.

Code details: Final grep is to get the PID without the trailing spaces.

For those that just want to just kill a process and wait for it to die, but not indefinitely:

Max. 10 seconds gracefull close + 10 seconds brutal kill.

Warning: This answer is in no way related to traping a kill signal and dispatching it.

APP_PID=$(ps -x | grep "[M]yprocess_name" | cut -d" " -f 1)
if [ ! -z "$APP_PID" ]; then
    echo "App is open. Closing app gracefully (SIGHUP)"
    kill -HUP "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

APP_PID=$(ps -x | grep "[M]yprocess_name" | cut -d" " -f 1)
if [ ! -z "$APP_PID" ]; then
    echo "Failed. Force killing app (SIGTERM)"
    kill "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

It selects the app to kill by name, but you can directly use the PID also. Keep the brackets for the first letter of the app name to avoid matching grep itself.

For those that just want to just kill a process and wait for it to die, but not indefinitely:

Max. 10 seconds gracefull close + 10 seconds brutal kill.

Warning: This answer is in no way related to traping a kill signal and dispatching it.

APP_PID=$(ps -x | grep "[M]yprocess_name" | grep -oP '^\s*\K[0-9]+')
if [ ! -z "$APP_PID" ]; then
    echo "App is open. Closing app gracefully (SIGHUP)"
    kill -HUP "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

APP_PID=$(ps -x | grep "[M]yprocess_name" | grep -oP '^\s*\K[0-9]+')
if [ ! -z "$APP_PID" ]; then
    echo "Failed. Force killing app (SIGTERM)"
    kill "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

It selects the app to kill by name or arguments, but you can directly use the PID also, or use a simpler pidof process_name. Keep the brackets for the first letter of the app name to avoid matching grep itself.

Code details: Final grep is to get the PID without the trailing spaces.

1
source | link

For those that just want to just kill a process and wait for it to die, but not indefinitely:

Max. 10 seconds gracefull close + 10 seconds brutal kill.

Warning: This answer is in no way related to traping a kill signal and dispatching it.

APP_PID=$(ps -x | grep "[M]yprocess_name" | cut -d" " -f 1)
if [ ! -z "$APP_PID" ]; then
    echo "App is open. Closing app gracefully (SIGHUP)"
    kill -HUP "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

APP_PID=$(ps -x | grep "[M]yprocess_name" | cut -d" " -f 1)
if [ ! -z "$APP_PID" ]; then
    echo "Failed. Force killing app (SIGTERM)"
    kill "$APP_PID"
    WAIT_LOOP=0
    while ps -p "$APP_PID" > /dev/null 2>&1; do
        sleep 1
        WAIT_LOOP=$((WAIT_LOOP+1))
        if [ "$WAIT_LOOP" = 10 ]; then
            break
        fi
    done
fi

It selects the app to kill by name, but you can directly use the PID also. Keep the brackets for the first letter of the app name to avoid matching grep itself.