-4

I want to write a script that would start two daemon jobs in two different directories. This script should run in the foreground, and then when I press ctrl-c, the script would exit and bring down the two daemons with it.

Say the two daemons are two executables called daemon1 and daemon2, and the two directories in which they need to start respectively are dir1 and dir2.

How would I write such a script?

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
Derek Chiang
  • 101
  • 1
  • 2
  • 1
    This is a basics-level question, and typically outside the scope of the discussion here. – DTK Nov 08 '14 at 23:58
  • 1
    Focus on specific challenges please. Users are expected to perform some independent research on their problem, and asking a SE site to provide a roadmap for a design doesn't meet this criteria. – Andrew B Nov 09 '14 at 01:14
  • @DTK Is this really so basic? I can write basic shell script but I seriously don't know how to do it... Did you look at my specific requirements? I want to be able to terminate the two daemons when I terminate the script. If I just did `daemon1 & daemon2` they would still be running in the background when I terminate the script. – Derek Chiang Nov 09 '14 at 01:46
  • Your question is basic but more importantly it's not well defined, it is vague. Believe it or not the OS matters and to some extent so does the scripting language. Do the daemons take arguments that tell them which directory to operate in or do they assume the current directory? Do the daemons produce pid files? the list of questions goes on. – user9517 Nov 09 '14 at 09:33

1 Answers1

1

You may want to look up the scripting language for your operating environment. If this were something like korn-shell, you might look at trapping signals, executing processes in the background and looking at the last PID to determine what to kill when you trap a SIG-INT or the like.

EDIT: More details

  • At the top, trap SIG INT and SIG HUP, have them run a cleanup subroutine
  • When you launch each daemon, background it, capture its PID and shove it into a variable in the wrapper script.
  • Periodically check whether your backgrounded processes are still running.
    • If they are not, re-launch the failed one and log that you did so.
  • Have your cleanup subroutine check if each PID corresponds to an active, running process.
    • If it does, kill -9 that process, log that you did so.
    • If it does not, undefine that variable and log that you did not find expected process.


DTK
  • 1,718
  • 10
  • 15