How can I detect whether an /etc/rc.d/init.d script is being run at boot time versus by hand?
Mark Komarinski
mkomarinski at wayga.org
Tue May 21 11:42:11 EDT 2013
On 05/21/2013 11:22 AM, Bill Freeman wrote:
> I'm trying to figure out whether to force the removal of an almost
> certainly stale pid file or not in the service start case.
>
> While I presume that the start up sequence normally handles this by
> clearing /var/run before lighting off the init scripts for the level,
> I have a need to have my pid file in an unusual place (needs to be
> written and deleted by a non-root process).
>
> I'd like start at boot to be automatic, and if shutdown was clean, it
> will be. But if the system crashes (or someone hits the reset button,
> etc.) there will be a stale pid file come boot time.
>
> I'd like to automatically delete any stale pid file at boot time, but
> start later should fail claiming that there's an existing process.
To be more pedantic about it, you want it to start cleanly unless you
know it's already running (this gives the case of shutting the process
down without restarting the system).
> So, can I count on parent pid, or maybe process group id, to identify
> the at boot time case? Or would that be unwise?
Why not look up the pid in the process table and see if it:
a) exists
and
b) is the process you think it should be
If both a and b are true, then the process is probably legitimate and
you shouldn't start a new instance. If a is false, then you know that
PID no longer exists and the process shouldn't be running. If a is true
and b is false, then it means another process has started and got that
PID by coincidence. You can still assume the process shouldn't be
running and start a new instance.
-Mark
More information about the gnhlug-discuss
mailing list