How can I detect whether an /etc/rc.d/init.d script is being run at boot time versus by hand?

Kevin D. Clark kevin_d_clark at comcast.net
Tue May 21 15:23:57 EDT 2013


Bill Freeman writes:

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

I'm not specifically answering your question here, but, here is some
code that I believe to be reasonable and related to the problem you
appear to be trying to solve.

Regards,

--kevin




#!/bin/bash

#
#  General functions
# 

function try_lock () {
    local tempfile="$1.$$"
    local lockfile="$1.lock"

    # do we have permission at all to write here?
    if ! echo $$ >$tempfile ; then
      echo "You do not have permission to access `dirname $tempfile`" 1>&2
      return 1
    fi

    # attempt to create the lockfile ; if successful return success
    if ln $tempfile $lockfile 2>/dev/null ; then
        rm -f $tempfile
        return 0
    fi

    # no success creating lockfile?  well, is there any current process
    # that is holding onto this lock?  if so, return (and thus:  wait)
    if kill -0 `cat $lockfile` 2>/dev/null ; then
        rm -f $tempfile
        return 1
    fi

    # if we are here the lockfile must be stale
    echo "Removing stale lock file: $lockfile"
    rm -f $lockfile
    if ln $tempfile $lockfile 2>/dev/null ; then
        rm -f $tempfile
        return 0
    fi

    rm -f $tempfile
    return 1
}

release_lock() {
    local lockfile="$1.lock"
    local tempfile="$1"

    rm -f "$lockfile" "$tempfile"
}

function get_lock() {
    local tempfile="$1"

    until try_lock $tempfile ; do
        echo waiting for the lock
        sleep 1
    done
}


....

#
#  Convenience functions for specific locks
#

function get_AAA_lock() {
    get_lock /var/run/AAA
}

function release_AAA_lock() {
    release_lock /var/run/AAA
}

function get_BBB_lock() {
    get_lock /some/other/path/BBB
}

function release_BBB_lock() {
    release_lock /some/other/path/BBB
}

..........



function do_something() {

   get_BBB_lock

   critical_section

   release_BBB_lock
}







-- 
GnuPG Fingerprint: D87F DAD6 0291 289C EB1E 781C 9BF8 A7D8 B280 F24E 


More information about the gnhlug-discuss mailing list