bash restart sub-process using trap SIGCHLD? -


i've seen monitoring programs either in scripts check process status using 'ps' or 'service status(on linux)' periodically, or in c/c++ forks , wait on process...

i wonder if possible use bash trap , restart sub-process when sigcld received?

i have tested basic suite on redhat linux following idea (and didn't work...)

#!/bin/bash set -o monitor # can explain this? discussion on internet needed trap startprocess sigchld startprocess() {    /path/to/another/bash/script.sh & # 1 restart   while [ 1 ]       sleep 60   done } startprocess 

what bash script being started sleep few seconds , exit now.

several issues observed:

  • when shell starts in foreground, sigchld handled once. trap reset signal handling signal()?
  • the script , child seem immune sigint, means cannot stopped ^c
  • since cannot closed, closed terminal. script seems hup , many zombie children left.
  • when run in background, script caused terminal die

... anyway, not work @ all. have know little topic. can suggest or give working examples? there scripts such use?

how use wait in bash, then?

thanks

i can try answer of questions not based on know.

  1. the line set -o monitor (or equivalently, set -m) turns on job control, on default interactive shells. seems required sigchld sent. however, job control more of interactive feature , not meant used in shell scripts (see this question).

    also keep in mind not intended because once enable job control, sigchld sent every external command exists (e.g. every time run ls or grep or anything, sigchld fire when command completes , trap run).

  2. i suspect reason sigchld trap appears run once because trap handler contains foreground infinite loop, script gets stuck in trap handler. there doesn't seem point loop anyways, remove it.

  3. the script's "immunity" sigint seems effect of enabling job control (the monitor part). hunch job control turned on, sub-instance of bash runs script no longer terminates in response sigint instead passes sigint through foreground child process. in script, ^c i.e. sigint acts continue statement in other programming languages case, since sigint kill running sleep 60, whereupon while loop run new sleep 60.

  4. when tried running script , killing (from terminal), ended 2 stray sleep processes.

  5. backgrounding script kills shell me, although behavior not terribly consistent (sometimes happens immediately, other times not @ all). seems typing keys other enter causes eof sent somehow. after terminal exits script continues run in background. have no idea going on here.

being more specific want accomplish help. if want command run continuously lifetime of script, run infinite loop in background, like

while true;     some-command     echo some-command finished     echo restarting some-command ... done & 

note & after done.

for other tasks, wait better idea using job control in shell script. again, depend on trying do.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -