Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
What is the OS, your planing to implement this ? – Chakri Jun 5 '12 at 14:47
I'm using CentOS – Matt James Jun 6 '12 at 17:05
up vote 1 down vote accepted

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.

share|improve this answer

Here are the steps.

  • Lets says your script /opt/path/test. Ensure its executable

  • Ensure the following set of lines on the top of your script, right after "#!/bin/bash"

    For RedHat and cousins:

    chkconfig: - Sequence# & Level you want to start "Eg:- 91 35"

    description: Description of your DAEMON process

    processname: DAEMON NAME

  • Look at a sample script in /etc/init.d path and try to mimic your start/stop/restart process... [ BIG STEP..not going into the details ]

  • End with "&" to run your script in the background in the script

    Eg:- " /opt/path/test & "

  • Copy the script to /etc/init.d location

  • No do the chkconfig

    chkconfig --add test

    chkconfig test on

  • Verifications

    chkconfig --list | grep test

  • Do /etc/init.d/test stop/start/restart.. ensure your process is running.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.