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.
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.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.