5

I have a python application that uses twisted framework.

I make use of value stored in the pidfile generated by twistd. A launcher script checks for it's presence and will not spawn a daemon process if the pidfile already exists.

However, twistd does not remove the .pidfile when it gets SIGKILL signal. That makes the launcher script think that the daemon is already running.

I realize the proper way to stop the daemon would be to use SIGTERM signal, but the problem is that when user who started the daemon logs out, the daemon never gets a SIGTERM signal, so apparently it's killed with SIGKILL. That means once a user logs out, he will never be able to start the daemon again, because the pidfile still exists.

Is there any way I could make that file disappear in such situations?

rafalcieslak
  • 915
  • 1
  • 12
  • 25

2 Answers2

4

From the signal(2) man page:

The signals SIGKILL and SIGSTOP cannot be caught or ignored.

So there is no way the process can run any cleanup code in response to that signal. Usually you only use SIGKILL to terminate a process that doesn't exit in response to SIGTERM (which can be caught).

James Henstridge
  • 42,244
  • 6
  • 132
  • 114
  • Thank you for your answer, this seems quite logical. Yet that makes me wonder why - when a user logs out - a SIGTERM is no sent to that twisted daemon, instead a SIGKILL is used? – rafalcieslak Nov 09 '12 at 13:11
  • @Jean-PaulCalderone: To be exact, my twistd daemon is started as a dbus service. I've read that dbus terminates all services when it shuts down, yet my twistd daemon receives a SIGKILL instead. – rafalcieslak Nov 13 '12 at 15:56
0

You could change your launcher (or wrap it up in another launcher) and remove the pid file before trying to restart twistd.

Mihai
  • 2,125
  • 2
  • 14
  • 16
  • This is what I do currently. If the file is present, but the daemon is not responding, the launcher tries killing the given pid, and then I removing the file. However, there is no way to distinguish whether the daemon is running (yet e.g. hung) or if it was killed, as in both cases the pidfile is present. And the point is that if it got killed (maybe even in a previous session), I cannot try killing that pid, because it can possibly be a completly other new process. – rafalcieslak Nov 09 '12 at 15:42