I'm new to macOS dev. Most of my background is on Windows.
I am trying to write a function for my launch daemon that should run a Bash script via its file path and then get notified asynchronously when it finishes running and get its exit code (I think it's called "status code" on Linux.) Or send an error in the callback if it fails to run the Bash script.
I'm using the following, which also includes my two-part question in comments:
void RunExternalScript(const char* pScriptFilePath,
void (*pfnCallback)(int exitCode, const void* pParam),
const void* pParam)
{
//'pfnCallback' = should be called when 'pScriptFilePath' finishes running
//Fork our process
pid_t pidChild = fork();
if(pidChild == 0)
{
//Child process, start our Bash script
execl(pScriptFilePath, (const char*)nullptr);
//We get here only if we failed to run our script
log("Error %d running script: %s", errno, pScriptFilePath);
abort();
}
else if(pidChild != -1)
{
//Started our script OK, but how do I wait for it
//asynchronously to finish?
//
//If I call here:
//
// int nExitCode;
// waitpid(pidChild, &nExitCode, 0);
//
//It will block until the child process exits,
//which I don't want to do since I may want to
//run more than one script, plus my daemon may
//receive a request to quit, so I need to be able
//to cancel this wait for the child process...
//And the second question:
//
//How do I know from this process that the forked
//process failed to run my script?
}
else
{
log("Error running: %s", pScriptFilePath);
}
}