How to kill a daemon?

I'm running Debian Sarge AMD64, and have a Bash script running as a daemon; at one-second intervals it checks a directory for the presence of files and processes them accordingly. The associated init.d script starts the daemon fine, but doesn't succeed in killing it when requested.

The init.d script uses the following commands to start the daemon (where $DAEMON is the path to the daemon):
echo "Starting $DESC: $NAME."
start-stop-daemon -Sbqx $DAEMON

and the following commands to try (unsuccessfully) to kill it:
echo "Stopping $DESC: $NAME."
start-stop-daemon -Koqx $DAEMON

The echo command does get executed in both cases. I've tried many different variations of the kill command, but nothing works.

To detect the kill signal, the daemon script uses the following command at regular intervals:
trap "exit 0" SIGKILL SIGTERM

but this does not result in the script being killed.

Can anybody see anything I'm doing wrong?

Thanks a lot.

Tony.

(P.S. I used tt tags to format the code here, but they don't seem to work!)

No votes yet

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

a few things -

1. Do you really need to check every second? What are you trying to do with this script?
2. Try killing it manually: kill -9 $(pidof yourscript)

There are much more efficient ways of monitoring for changes in a directory/file - especially if you have a kernel with the 'dnotify' module. At the moment of course you need to create a binary executable file to take advantage of that.

Thanks for that. It does

Thanks for that.

It does need to check every second or so; graphic files are coming in from document scanners at any time and they need to processed differently (possibly being printed) depending on the filenames. Users could well be irritated by delays of more than a couple of seconds.

The script can of course be killed manually, but I would prefer to stick to the standard procedures for daemons; I don't want to have a script die while it's in the middle of processing a file.

$world=~s/war/peace/g;

can you post the script?

It's best to write some dummy script to test things out; make sure you're trapping the right signals etc. Launch the dummy script manually:

./thescript &
Then send it signals as you please:
kill -SIGTERM $(pidof thescript)

The "trap" in principle registers a handler. Since you specified "KILL" and "TERM" it is possible that none of your handlers were registered because recent changes mean that KILL can no longer be handled. I'm not sure why the changes were made - perhaps too many silly people were handling KILL and not actually terminating. Try specifying only "TERM" and send -SIGTERM (or -TERM if the shell complains 'SIGTERM undefined').

By the way, does anyone know what happened to the 'bash' documentation? 'info bash' and 'man bash' on my SID system no longer bring up a manual. :(

Working!

It's working now.

There was nothing wrong with the signal trapping. The problem was the fact that it's a shell script, so the process runs with the name "bash", not "getscansd".

I've dropped the "start-stop-daemon" system, and now just start the daemon directly, creating a PID file, and retrieving the PID from the file when killing it.

Thanks for your help.

$world=~s/war/peace/g;

Syndicate content