Shell Script Launching Child Processes

backgrounddaemonprocessshell

Disclaimer: I'm totally new to shell scripting, but have quite a bit of experience in other languages like PHP and Obj-C.

I'm writing my first daemon script. Here are the goals:

  • I want it to run in the background
  • I want it to be triggered by an init.d script that includes start/stop/restart commands
  • I want each process in a loop to trigger its own subprocess.
  • When the parent process kicked off by the init.d script is killed, I want the subprocesses to die as well.

Essentially, I'm looking for the same kind of behavior that appears to be very common among software like apache, spamd, dovecot, etc. But, based on my research, I haven't found a single, simple answer as to how this kind of thing is achieved.

Any help is greatly appreciated.

Best Answer

You will find lots of very good information by reading Process Management.

Also, take a look at your existing init.d scripts and use them as models as you write your own. Keep in mind though that some of them could stand improving. So try not to pick up any bad habits.

You'll likely want to write your scripts in the Bourne shell (sh) rather than Bash, but you can learn a lot that's applicable to both by reading Bash FAQs and Bash Pitfalls.