Init.d script not executing at shutdown and reboot
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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".
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?
Last edited by blacky_5251; 03-04-2010 at 04:21 PM.
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.
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.
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:-
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
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
Last edited by blacky_5251; 03-10-2010 at 11:06 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.
[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:-
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
Let me know if your distro doesn't support the subsys lock file. I'd be interested to know
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.
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.
I followed all the above steps and its working fine.
But my task is little different i have created the script in /etc/init.d/backup, Its function is to take the backup of all the data which took around 10 -15 minutes.
I want to execute at shutdown (runlevel 0) and reboot (runlevel 6).
i have created the soft links as
/etc/rc.d/rc6.d/K01backup
/etc/rc.d/rc0.d/K01backup
Problem i am facing here is the backup process isn't completing the process run for maximum of 4 minutes and then system is shutdown.
what to do how can I increase the time for my script so that the backup process is completed before shutdown or reboot.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.