LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Init.d script not executing at shutdown and reboot (http://www.linuxquestions.org/questions/linux-general-1/init-d-script-not-executing-at-shutdown-and-reboot-793154/)

roberbizimhatemo 03-04-2010 09:13 AM

Init.d script not executing at shutdown and reboot
 
Hi, I have a init.d script (/etc/init.d/emailtome) which must be executed at startup (runlevel 3,5), shutdown (runlevel 0) and reboot (runlevel 6).

My script is as follows:

Code:

#!/bin/bash
# chkconfig: 0356 99 1
# description: Some description
# processname: mailtome
#        /etc/rc.d/init.d/mailtome
 
# Source function library.
. /etc/init.d/functions
 
#<define any local shell functions used by the code that follows>
 
mailto=root@localhost
 
start() {
        echo -n "Starting mailtome "
        #start daemons, perhaps with the daemon function>

        echo "TEST" | mail -s "STARTING UP" $mailto
        success
}       
 
stop() {
        echo -n "Stopping mailtome "
        #start daemons, perhaps with the daemon function>

        echo "TEST" | mail -s "STOPPING" $mailto
        success
}
 
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        echo "Not applied to service"
        ;;
    restart)
            stop
        start
        ;;
    reload)       
        echo "Not applied to service"
        ;;
    condrestart)
            #<Restarts the servce if it is already running. For example:>
        echo "Not applied to service"
        ;;
    probe)
        ;;
    *)
        echo "Usage: mailtome{start|stop|status|reload|restart[|probe]"
        exit 1
        ;;
esac
exit $?

The rights are ok, root:root 755.

I executed the following:
Code:

chkconfig --add mailtome
chkconfig mailtome on

The message is only sent when the system starts. When I execute the init.d scripts manually, it works. So it is not a script problem. As for the priority, 99 for starting and 1 for ending should be good.

When executing "chkconfig --list | grep mailtome", I get the correct information (on runlevel 0, 3, 5, 6 it is set to "on".

The following files exist in /etc/rc.d:
Code:

/etc/rc.d/rc2.d/K01mailtome
/etc/rc.d/rc1.d/K01mailtome
/etc/rc.d/rc0.d/S99mailtome
/etc/rc.d/rc5.d/S99mailtome
/etc/rc.d/rc4.d/K01mailtome
/etc/rc.d/rc3.d/S99mailtome
/etc/rc.d/rc6.d/S99mailtome

What can the problem be?

Is it not strange that there are entries for runlevel 1, 2 and 4?

Thanks by advance,

blacky_5251 03-04-2010 03:24 PM

Not really sure what you're expecting to happen, except that the stop email isn't being sent. You said that when the server boots you get an email, so the script it being called. Your script doesn't have any "daemon" or "background" processes in it that would continue to run after the script terminates. So you're if you're not seeing the email from the shutdown, then try adding a "sleep" instruction to the "stop" function, so the server gets an opportunity to send the email before all the other services are stopped themselves.

Do you see anything in the mail log file?

roberbizimhatemo 03-05-2010 08:25 AM

I forgot to say which version of Linux I am using. I use RHEL 5.

I tested the sleep function, but no difference.

There is no mail send, as it is not written in the mail logs. I also added an echo to a file with each execution of the start and stop function. The script is not executed at shutdown/reboot, as no entries are made for the shutdown function in the log of the function.

Which other actions can I take?

blacky_5251 03-05-2010 03:05 PM

Try creating an entry in your appropriate run-level directory (e.g. rc5.d) for the kill process. E.g.:-
Code:

ln -s /etc/init.d/mailtome /etc/rc.d/rc5.d/K01mailtome
Hopefully this will force the shutdown process to run your script with the "stop" parameter.

roberbizimhatemo 03-08-2010 09:45 AM

No difference. Probably I have some bad syntax in the script. As it is not a service, but just a script, is there no better way to implement this?

Thanks.

catkin 03-08-2010 11:09 AM

When is the mailer daemon stopped during system shutdown? You probably need to send the email before then.

blacky_5251 03-08-2010 06:32 PM

Is anything appearing in your log file (the one that records whether the script was called to start or stop the process)?

roberbizimhatemo 03-09-2010 04:10 AM

catkin=>I also write an output to a file everytime the script is executed, so I can check if the script is really executed.

As I see from this logfile, the script is only executed at startup.

When I add a symbolic link as /etc/rc.d/rc6.d/S01mailtomn, it works (but giving the wrong mail=>START UP).

I also did the following:
Code:

chkconfig --del mailtome
chkconfig mailtome --level 0 off
chkconfig mailtome --level 6 off

chkconfig mailtome --level 3 on
chkconfig mailtome --level 5 on

Same result, the stop function and the script are not executed.

blacky_5251 03-10-2010 11:47 PM

OK, I think I've nailed it. The primary problem is that your script isn't creating a lock file in the /var/lock/subsys directory. So when the script /etc/rc.d/rc is called (which happens when the runlevel changes), it decides that your script isn't running, and so doesn't NEED to be stopped. You need the K script and the lock file for the script to be "stop"ped.

Also, did you use "chkconfig --add mailtome" to create all of the symlinks? If you didn't, then the K scripts won't be created correctly.

So here is what you need to do.
  1. Add a lock file to your script
    In the "start" section, do this:-
    Code:

    touch /var/lock/subsys/mailtome
    And in the "stop" section, do this:-
    Code:

    rm -f /var/lock/subsys/mailtome
    It would be best to create an environment variable that holds the location of your lockfile, so you can then reference it with the touch and rm commands:-
    Code:

    lockfile=/var/lock/subsys/mailtome
     ...
    touch $lockfile
     ...
    rm -f $lockfile

  2. Fix the runlevels in your script
    You only want the script to "start" in run levels 3 and 5, so you should only have 3 and 5 referenced in your chkconfig: statement.
    Code:

    # chkconfig: 35 99 10
  3. Use chkconfig correctly
    Code:

    chkconfig --add mailtome
    This will create S (start) scripts in runlevels 3 and 5, and K (kill) scripts for all other runlevels (0, 1, 2, 4 and 6).
Thanks for the question, I re-learnt stuff that I had *obviously* forgotten when researching the answer :)

roberbizimhatemo 03-12-2010 03:36 AM

Nice! It works. I knew it was something little... Many thanks for your time.

catkin 03-12-2010 03:55 AM

Quote:

Originally Posted by blacky_5251 (Post 3893736)
OK, I think I've nailed it. The primary problem is that your script isn't creating a lock file in the /var/lock/subsys directory.

[snip]

It would be best to create an environment variable that holds the location of your lockfile, so you can then reference it with the touch and rm commands:-
Code:

lockfile=/var/lock/subsys/mailtome
 ...
touch $lockfile
 ...
rm -f $lockfile


Wow! How did you know about the lock file? Is that RHEL-specific?

<pedantic mode>
$lockfile as used is a Good Thing but it is not an environment variable; it is not exported.
</pedantic mode>

blacky_5251 03-12-2010 04:09 AM

Hi catkin,

To be honest, I had to go back to /etc/inittab and review what it does at each runlevel. It pointed me to the script /etc/rc.d/rc, which I studied a little to learn about the subsys lock files.

I don't know if this is RHEL specific, but I doubt it is. It makes sense that the OpSys shouldn't try to stop a process that it knows isn't running.

As for the environment variable, I won't debate it. Suffice to say it is available in the script's environment, so that's why I used the phrase. There is possibly a white-hat view that it isn't an environment variable unless it is exported, and if so then I tips my lid to you :hattip:

Let me know if your distro doesn't support the subsys lock file. I'd be interested to know ;)

catkin 03-12-2010 04:29 AM

Quote:

Originally Posted by blacky_5251 (Post 3895463)
Let me know if your distro doesn't support the subsys lock file. I'd be interested to know ;)

On Slackware 13.0 the shutdown scripts are rc.K, rc.0 and rc.6 (a symlink to rc.0) for going to single user mode, shutting down and restarting respectively. None of them check for files in /var/lock/subsys or check for any locks. rc.0/6 removes all files in /var/lock/subsys.

zQUEz 05-04-2012 08:28 AM

I realize this is reserecting a thread from the dead, but I wanted to put a little note for any others that had a frustrating time like me on this - the lock file needs to the same name as the service name, else it doesn't stop the service.

(RHEL5 is what I am using).


All times are GMT -5. The time now is 08:39 PM.