Terminate NSTask even if application crashes

If my application crashes, I will not be able to stop the NSTasks that it spawned, so they are left without power.

Is there a way to start a task so that it stops when your application terminates (even if it crashes)?

+6
source share
4 answers

I assume that you need to handle application crashes manually and in a different way to complete the generated processes. For example, you can check the following article http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html and in the exception / signal handler, when the application terminates with an error, send a completion signal to your child processes using kill (pid, SIGKILL), but for this you also need to save the pid of the child processes (NSTask - (int) processIdentifier) โ€‹โ€‹somewhere to get it from the exception / signal handler.

+3
source

I really wrote the program / script / everything that does just that ... Here was created the shell of the script, which was its basis ... The project actually implements it in X-code as one executable file. Itโ€™s strange that an apple makes it so dangerous IMO.

#!/bin/bash echo "arg1 is the SubProcess: $1, arg2 is sleepytime: $2, and arg3 is ParentPID, aka $$: $3" CHILD=$1 && SLEEPYTIME=$2 || SLEEPYTIME=10; PARENTPID=$3 || PARENTPID=$$ GoSubProcess () { # define functions, start script at very end. $1 arguments & # "&" puts SubP in background subshell CHILDPID=$! # what all the fuss is about. if kill -0 $CHILDPID; then # rock the cradle to make sure it aint dead echo "Child is alive at $!" # glory be to god else echo "couldnt start child. dying."; exit 2; fi babyRISEfromtheGRAVE # keep an eye on child process } babyRISEfromtheGRAVE () { echo "PARENT is $PARENTPID"; # remember where you came from, like j.lo while kill -0 $PARENTPID; do # is that fount of life, nstask parent alive? echo "Parent is alive, $PARENTPID is it PID" sleep $SLEEPTIME # you lazy boozehound if kill -0 $CHILDPID; then # check on baby. echo "Child is $CHILDPID and is alive." sleep $SLEEPTIME # naptime! else echo "Baby, pid $CHILDPID died! Respawn!" GoSubProcess; fi # restart daemon if it dies done # if this while loop ends, the parent PID crashed. logger "My Parent Process, aka $PARENTPID died!" logger "I'm killing my baby, $CHILDPID, and myself." kill -9 $CHILDPID; exit 1 # process table cleaned. nothing is left. all three tasks are dead. long live nstask. } GoSubProcess # this is where we start the script. exit 0 # this is where we never get to 
+2
source

What I did in the past is to create a channel in the parent process and pass the end of the recording of this channel to the child. The parent never closes the end of the reading, and the child watches the end of the record close. If the end of the record ever closes, it means the parent has exited. You will also need to mark the parent end of the pipe to close exec.

+1
source

You can periodically check your tasks to make sure their parent process still exists.

0
source

Source: https://habr.com/ru/post/889495/


All Articles