Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can try to answer some of your questions but not all based on what I know.</p> <ol> <li><p>The line <code>set -o monitor</code> (or equivalently, <code>set -m</code>) turns on job control, which is only on by default for interactive shells. This seems to be required for SIGCHLD to be sent. However, job control is more of an interactive feature and not really meant to be used in shell scripts (see also <a href="https://stackoverflow.com/questions/690266/why-cant-i-use-job-control-in-a-bash-script" title="Why can&#39;t I use job control in a bash script?">this question</a>). </p> <p>Also keep in mind this is probably not what you intended to do because once you enable job control, SIGCHLD will be sent for <em>every</em> external command that exists (e.g. every time you run <code>ls</code> or <code>grep</code> or anything, a SIGCHLD will fire when that command completes and your trap will run).</p></li> <li><p>I suspect the reason the SIGCHLD trap only appears to run once is because your trap handler contains a foreground infinite loop, so your script gets stuck in the trap handler. There doesn't seem to be a point to that loop anyways, so you could simply remove it.</p></li> <li><p>The script's "immunity" to SIGINT seems to be an effect of enabling job control (the monitor part). My hunch is with job control turned on, the sub-instance of bash that runs your script no longer terminates itself in response to a SIGINT but instead passes the SIGINT through to its foreground child process. In your script, the <code>^C</code> i.e. SIGINT simply acts like a <code>continue</code> statement in other programming languages case, since SIGINT will just kill the currently running <code>sleep 60</code>, whereupon the while loop will immediately run a new <code>sleep 60</code>.</p></li> <li><p>When I tried running your script and then killing it (from another terminal), all I ended up with were two stray sleep processes.</p></li> <li><p>Backgrounding that script also kills my shell for me, although the behavior is not terribly consistent (sometimes it happens immediately, other times not at all). It seems typing any keys other than enter causes an EOF to get sent somehow. Even after the terminal exits the script continues to run in the background. I have no idea what is going on here.</p></li> </ol> <p>Being more specific about what you want to accomplish would help. If you just want a command to run continuously for the lifetime of your script, you could run an infinite loop in the background, like</p> <pre><code>while true; do some-command echo some-command finished echo restarting some-command ... done &amp; </code></pre> <p>Note the <code>&amp;</code> after the <code>done</code>. </p> <p>For other tasks, <code>wait</code> is probably a better idea than using job control in a shell script. Again, it would depend on what exactly you are trying to do.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload