Modifying the shutdown/halt procedure - Linux

This is a discussion on Modifying the shutdown/halt procedure - Linux ; I am using Red Hat 7.3 and am attempting to modify the shutdown/halt procedure. In particular, when a shutdown or halt is performed I want to execute a particular shell script to perform some additional function peform the machine is ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Modifying the shutdown/halt procedure

  1. Modifying the shutdown/halt procedure

    I am using Red Hat 7.3 and am attempting to modify the shutdown/halt
    procedure. In particular, when a shutdown or halt is performed I want
    to execute a particular shell script to perform some additional
    function peform the machine is shutdown. (I want to send a SIGTERM to
    certain processes BEFORE the network interface is shutdown and BEFORE
    a SIGTERM is sent to all processes as the shutdown man page mentions.)

    My first attempt was to modify the /etc/init.d/halt script and adding
    a call to my script (call it kill_my_jobs) before the killall -15
    command. As I then found out, this occurs after network interface it
    shutdown. (I want the processes that I want to terminate to notify
    another computer that it is going down when it receives a SIGTERM.
    Hence why I need to do this before the network interface goes down.)

    After some more investigating I learned that when the run level is
    changed, the /etc/rc.d/rc script is exectued which in turn executes
    the various scripts in /etc/rc.d/rc0.d when runlevel 0 is entered.
    The scripts (links) starting with a K are called with a "stop"
    argument while those with an S are called with a "start" argument. I
    created a K02kill_my_jobs link which pointed to /sbin/kill_my_jobs.
    Performing a shutdown I found that it still was not executing.

    Finally, looking at the /etc/rc.d/rc script during the loop to run the
    K* scripts I found that it was passing the test for file existance,
    i.e. that the script exists however, the next test checks to see if
    /var/lock/subsyst/kill_my_jobs exists, which it does not and thus
    fails the test and is not executed.

    This is just a script that I want to run when I shutdown or reboot.
    Its not a daemon (which seems what the various things in
    /etc/rc.d/rc?.d are) and so does not create anything in the
    /var/lock/subsyst directory.

    So, my question is where is the proper place to put the command to run
    my script so that it is executed early in the shutdown procedure
    without any dependence upon creating a file in /var/lock/subsyst. Is
    there another way of doing this?

  2. Re: Modifying the shutdown/halt procedure

    dirty.harry@juno.com (Dirty Harry) wrote in message news:<64a441b.0310282048.3f154038@posting.google.com>...
    > I am using Red Hat 7.3 and am attempting to modify the shutdown/halt
    > procedure. In particular, when a shutdown or halt is performed I want
    > to execute a particular shell script to perform some additional
    > function peform the machine is shutdown. (I want to send a SIGTERM to
    > certain processes BEFORE the network interface is shutdown and BEFORE
    > a SIGTERM is sent to all processes as the shutdown man page mentions.)
    >
    > My first attempt was to modify the /etc/init.d/halt script and adding
    > a call to my script (call it kill_my_jobs) before the killall -15
    > command. As I then found out, this occurs after network interface it
    > shutdown. (I want the processes that I want to terminate to notify
    > another computer that it is going down when it receives a SIGTERM.
    > Hence why I need to do this before the network interface goes down.)
    >
    > After some more investigating I learned that when the run level is
    > changed, the /etc/rc.d/rc script is exectued which in turn executes
    > the various scripts in /etc/rc.d/rc0.d when runlevel 0 is entered.
    > The scripts (links) starting with a K are called with a "stop"
    > argument while those with an S are called with a "start" argument. I
    > created a K02kill_my_jobs link which pointed to /sbin/kill_my_jobs.
    > Performing a shutdown I found that it still was not executing.
    >
    > Finally, looking at the /etc/rc.d/rc script during the loop to run the
    > K* scripts I found that it was passing the test for file existance,
    > i.e. that the script exists however, the next test checks to see if
    > /var/lock/subsyst/kill_my_jobs exists, which it does not and thus
    > fails the test and is not executed.
    >
    > This is just a script that I want to run when I shutdown or reboot.
    > Its not a daemon (which seems what the various things in
    > /etc/rc.d/rc?.d are) and so does not create anything in the
    > /var/lock/subsyst directory.
    >
    > So, my question is where is the proper place to put the command to run
    > my script so that it is executed early in the shutdown procedure
    > without any dependence upon creating a file in /var/lock/subsyst. Is
    > there another way of doing this?



    The proper place to put run scripts is usually in /etc/init.d which
    are sym links into the proper runlevel dir. If you understand the S
    and K scripts you are on your way . If you notice the numbers
    following the S and K they are the order of precedence that the system
    executes the scritps when that runlevel is reached. If you locate the
    network script in /etc/init.d and follow its symlinks to the K and S
    scripts you will then need to execute your script prior to the network
    script which will there in down your interface.. Refer to /etc/inittab
    to locate your default run level and step down from there to execute
    your script. And finally the system when hit with a shutdown command
    does a sufficient job of handleing proper process terms but halt is a
    last resort command and I recomend not using it. Good luck ....

  3. Re: Modifying the shutdown/halt procedure

    fersher@hotmail.com (Chris) wrote in message news:<1717582c.0310301825.d711c9c@posting.google.com>...
    > dirty.harry@juno.com (Dirty Harry) wrote in message news:<64a441b.0310282048.3f154038@posting.google.com>...


    > > After some more investigating I learned that when the run level is
    > > changed, the /etc/rc.d/rc script is exectued which in turn executes
    > > the various scripts in /etc/rc.d/rc0.d when runlevel 0 is entered.
    > > The scripts (links) starting with a K are called with a "stop"
    > > argument while those with an S are called with a "start" argument. I
    > > created a K02kill_my_jobs link which pointed to /sbin/kill_my_jobs.
    > > Performing a shutdown I found that it still was not executing.
    > >
    > > Finally, looking at the /etc/rc.d/rc script during the loop to run the
    > > K* scripts I found that it was passing the test for file existance,
    > > i.e. that the script exists however, the next test checks to see if
    > > /var/lock/subsyst/kill_my_jobs exists, which it does not and thus
    > > fails the test and is not executed.
    > >
    > > This is just a script that I want to run when I shutdown or reboot.
    > > Its not a daemon (which seems what the various things in
    > > /etc/rc.d/rc?.d are) and so does not create anything in the
    > > /var/lock/subsyst directory.
    > >
    > > So, my question is where is the proper place to put the command to run
    > > my script so that it is executed early in the shutdown procedure
    > > without any dependence upon creating a file in /var/lock/subsyst. Is
    > > there another way of doing this?

    >
    >
    > The proper place to put run scripts is usually in /etc/init.d which
    > are sym links into the proper runlevel dir. If you understand the S
    > and K scripts you are on your way . If you notice the numbers
    > following the S and K they are the order of precedence that the system
    > executes the scritps when that runlevel is reached. If you locate the
    > network script in /etc/init.d and follow its symlinks to the K and S
    > scripts you will then need to execute your script prior to the network
    > script which will there in down your interface.. Refer to /etc/inittab
    > to locate your default run level and step down from there to execute
    > your script. And finally the system when hit with a shutdown command
    > does a sufficient job of handleing proper process terms but halt is a
    > last resort command and I recomend not using it. Good luck ....


    As I mentioned in my rather lengthy and descriptive explaination of
    the problem I did place a K* symlink to my script (kill_my_jobs) in
    rc0.d and rc6.d. I gave it a low number 02 so that it would execute
    early. However, the problem lies with the rc script (/etc/rc.d/rc).
    It has the following for loop in it

    for i in /etc/rc$runlevel.d/K*; do
    # Check if the script is there.
    [ ! -f $i ] && continue

    # Don't run [KS]??foo.{rpmsave,rpmorig} scripts
    [ "${i%.rpmsave}" != "${i}" ] && continue
    [ "${i%.rpmorig}" != "${i}" ] && continue
    [ "${i%.rpmnew}" != "${i}" ] && continue

    # Check if the subsystem is already up.
    subsys=${i#/etc/rc$runlevel.d/K??}
    [ ! -f /var/lock/subsys/$subsys ] && \
    [ ! -f /var/lock/subsys/${subsys}.init ] &&
    continue

    # Bring the subsystem down.
    if egrep -q "(killproc |action )" $i ; then
    $i stop
    else
    action $"Stopping $subsys: " $i stop
    fi
    done

    My script is not getting executed because it passes the [! -f
    /var/lock/subsys/$subsys] and following test. That is, there is NOT a
    file /var/lock/subsys/kill_my_jobs or a
    /var/lock/subsys/kill_my_jobs.init. The files that are in there
    typically look like various daemons that are running.
    All I want to do is run my script when runlevel 0 or 6 is entered but
    it won't get pass the /var/lock/subsys tests. I assume I could
    comment or remove this test, but I assume that it is there for a
    reason. This kill loop appears to me to only run scripts if there is
    an appropriately named file in /var/lock/subsys. I could possibly
    place and empty file in /var/lock/subsys but I don't know anything
    about this directory. Is it cleared on bootup?

  4. Re: Modifying the shutdown/halt procedure

    Dirty Harry wrote:
    > fersher@hotmail.com (Chris) wrote in message news:<1717582c.0310301825.d711c9c@posting.google.com>...
    >
    >>dirty.harry@juno.com (Dirty Harry) wrote in message news:<64a441b.0310282048.3f154038@posting.google.com>...

    >
    >
    >>>After some more investigating I learned that when the run level is
    >>>changed, the /etc/rc.d/rc script is exectued which in turn executes
    >>>the various scripts in /etc/rc.d/rc0.d when runlevel 0 is entered.
    >>>The scripts (links) starting with a K are called with a "stop"
    >>>argument while those with an S are called with a "start" argument. I
    >>>created a K02kill_my_jobs link which pointed to /sbin/kill_my_jobs.
    >>>Performing a shutdown I found that it still was not executing.
    >>>
    >>>Finally, looking at the /etc/rc.d/rc script during the loop to run the
    >>> K* scripts I found that it was passing the test for file existance,
    >>>i.e. that the script exists however, the next test checks to see if
    >>>/var/lock/subsyst/kill_my_jobs exists, which it does not and thus
    >>>fails the test and is not executed.
    >>>
    >>>This is just a script that I want to run when I shutdown or reboot.
    >>>Its not a daemon (which seems what the various things in
    >>>/etc/rc.d/rc?.d are) and so does not create anything in the
    >>>/var/lock/subsyst directory.
    >>>
    >>>So, my question is where is the proper place to put the command to run
    >>>my script so that it is executed early in the shutdown procedure
    >>>without any dependence upon creating a file in /var/lock/subsyst. Is
    >>>there another way of doing this?

    >>
    >>
    >> The proper place to put run scripts is usually in /etc/init.d which
    >>are sym links into the proper runlevel dir. If you understand the S
    >>and K scripts you are on your way . If you notice the numbers
    >>following the S and K they are the order of precedence that the system
    >>executes the scritps when that runlevel is reached. If you locate the
    >>network script in /etc/init.d and follow its symlinks to the K and S
    >>scripts you will then need to execute your script prior to the network
    >>script which will there in down your interface.. Refer to /etc/inittab
    >>to locate your default run level and step down from there to execute
    >>your script. And finally the system when hit with a shutdown command
    >>does a sufficient job of handleing proper process terms but halt is a
    >>last resort command and I recomend not using it. Good luck ....

    >
    >
    > As I mentioned in my rather lengthy and descriptive explaination of
    > the problem I did place a K* symlink to my script (kill_my_jobs) in
    > rc0.d and rc6.d. I gave it a low number 02 so that it would execute
    > early. However, the problem lies with the rc script (/etc/rc.d/rc).
    > It has the following for loop in it
    >
    > for i in /etc/rc$runlevel.d/K*; do
    > # Check if the script is there.
    > [ ! -f $i ] && continue
    >
    > # Don't run [KS]??foo.{rpmsave,rpmorig} scripts
    > [ "${i%.rpmsave}" != "${i}" ] && continue
    > [ "${i%.rpmorig}" != "${i}" ] && continue
    > [ "${i%.rpmnew}" != "${i}" ] && continue
    >
    > # Check if the subsystem is already up.
    > subsys=${i#/etc/rc$runlevel.d/K??}
    > [ ! -f /var/lock/subsys/$subsys ] && \
    > [ ! -f /var/lock/subsys/${subsys}.init ] &&
    > continue
    >
    > # Bring the subsystem down.
    > if egrep -q "(killproc |action )" $i ; then
    > $i stop
    > else
    > action $"Stopping $subsys: " $i stop
    > fi
    > done
    >
    > My script is not getting executed because it passes the [! -f
    > /var/lock/subsys/$subsys] and following test. That is, there is NOT a
    > file /var/lock/subsys/kill_my_jobs or a
    > /var/lock/subsys/kill_my_jobs.init. The files that are in there
    > typically look like various daemons that are running.
    > All I want to do is run my script when runlevel 0 or 6 is entered but
    > it won't get pass the /var/lock/subsys tests. I assume I could
    > comment or remove this test, but I assume that it is there for a
    > reason. This kill loop appears to me to only run scripts if there is
    > an appropriately named file in /var/lock/subsys. I could possibly
    > place and empty file in /var/lock/subsys but I don't know anything
    > about this directory. Is it cleared on bootup?


    You want to start the script with "S" - It may sound odd but you STARTING
    runlevel 0 not stopping it.

    /dan


  5. Re: Modifying the shutdown/halt procedure

    Daniel Ganek wrote in message news:...
    > Dirty Harry wrote:>
    > > > My script is not getting executed because it passes the [! -f

    > > /var/lock/subsys/$subsys] and following test. That is, there is NOT a
    > > file /var/lock/subsys/kill_my_jobs or a
    > > /var/lock/subsys/kill_my_jobs.init. The files that are in there
    > > typically look like various daemons that are running.
    > > All I want to do is run my script when runlevel 0 or 6 is entered but
    > > it won't get pass the /var/lock/subsys tests. I assume I could
    > > comment or remove this test, but I assume that it is there for a
    > > reason. This kill loop appears to me to only run scripts if there is
    > > an appropriately named file in /var/lock/subsys. I could possibly
    > > place and empty file in /var/lock/subsys but I don't know anything
    > > about this directory. Is it cleared on bootup?

    >
    > You want to start the script with "S" - It may sound odd but you STARTING
    > runlevel 0 not stopping it.
    >
    > /dan


    That won't work because all the "K" scripts are run first which
    essentially kill everything including the network connection which I
    mentioned I need up when I want to execute my script. Then the "S"
    scripts are executed. So Using an "S" script won't work.

    I did try manually creating /var/lock/subsys/kill_my_jobs and shutting
    down. That worked except that /var/lock/subsys is cleared somewhere
    and the "lock" doesn't exist when the system comes back up.

    I believe I may have figured something out though. Haven't fully
    investigated it yet. There is a /var/lock/subsys/local file created
    by /etc/rc.d/rc.local. It is not created by any deamons. I'm
    thinking that I can create a K02local in rc0.d and rc6.d which is a
    link to some script. This script will then execute my kill_my_jobs
    script. This script should get executed since the lock
    (/var/lock/subsys/local) seems to be created on boot up some how.

+ Reply to Thread