Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • @GabrielStaples your example was fantastic thank you. I only have 1 remaining issue. My script uses set -e which kills the entire script once the first (non zero) my_sleep function returns. Usually this isn't a problem if the subprocess is part of an if statement (set -e ignores failures in ifs and a couple other situations) but I am having trouble figuring out how to work something like that into your example. Somewhere around ${procs[$i]} & pids+=("$!") I need something that set -e ignores when ${procs[$i]} fails (returns non-zero) Commented Feb 22, 2022 at 3:08
  • @Rosey, can you turn off set -e for the script? Does it have to be on? Also, you can run set +e anywhere in the script to turn it off, and set -e again to turn it back on. Try wrapping the subprocess call cmd with those. Commented Feb 22, 2022 at 4:03
  • @GabrielStaples Yeah I can sort of do that. You can't just sandwich the command like this though: set +e ${procs[$i]} & pids+=("$!") set -e because the subprocesses are async. By the time one completes you have turned set -e back on. Right now I have the set +e above the "run commands as subprocesses" for loop and set -e is in the if that breaks the while loop. It works but it's over-scoped. Simple syntax errors outside the my_sleep function will be ignored + displayed in console. Commented Feb 22, 2022 at 4:12
  • @Rosey, try asking a new question and posting a comment here with a link to it. If you do, I'll take a look and put some more effort into it. Commented Feb 23, 2022 at 0:30
  • The command ps --pid "$pid" is not 100% safe because the process may already terminated with its pid reused by other newly created processes on the system. Given the short polling duration, it is really hard to happen, and I don't know if we can do anything to improve it because the shell environment is limited. By the way, my shell BusyBox sleep command must take the minimum of 1 second as argument (floating point is not supported). I hope nothing goes wrong during that 1 second. :( Commented Sep 11, 2022 at 12:23