LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 03-04-2010, 08:13 AM   #1
roberbizimhatemo
LQ Newbie
 
Registered: Nov 2009
Posts: 12

Rep: Reputation: 0
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,
 
Old 03-04-2010, 02:24 PM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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.
 
Old 03-05-2010, 07:25 AM   #3
roberbizimhatemo
LQ Newbie
 
Registered: Nov 2009
Posts: 12

Original Poster
Rep: Reputation: 0
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?
 
Old 03-05-2010, 02:05 PM   #4
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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.
 
Old 03-08-2010, 08:45 AM   #5
roberbizimhatemo
LQ Newbie
 
Registered: Nov 2009
Posts: 12

Original Poster
Rep: Reputation: 0
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.

Last edited by roberbizimhatemo; 03-08-2010 at 09:18 AM.
 
Old 03-08-2010, 10:09 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
When is the mailer daemon stopped during system shutdown? You probably need to send the email before then.
 
Old 03-08-2010, 05:32 PM   #7
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
Is anything appearing in your log file (the one that records whether the script was called to start or stop the process)?
 
Old 03-09-2010, 03:10 AM   #8
roberbizimhatemo
LQ Newbie
 
Registered: Nov 2009
Posts: 12

Original Poster
Rep: Reputation: 0
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.

Last edited by roberbizimhatemo; 03-09-2010 at 03:14 AM.
 
Old 03-10-2010, 10:47 PM   #9
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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

Last edited by blacky_5251; 03-10-2010 at 11:06 PM.
 
1 members found this post helpful.
Old 03-12-2010, 02:36 AM   #10
roberbizimhatemo
LQ Newbie
 
Registered: Nov 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Nice! It works. I knew it was something little... Many thanks for your time.
 
Old 03-12-2010, 02:55 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by blacky_5251 View Post
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>
 
Old 03-12-2010, 03:09 AM   #12
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
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

Let me know if your distro doesn't support the subsys lock file. I'd be interested to know
 
Old 03-12-2010, 03:29 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by blacky_5251 View Post
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.
 
Old 05-04-2012, 07:28 AM   #14
zQUEz
Member
 
Registered: Jun 2007
Distribution: Fedora, RHEL, Centos
Posts: 294

Rep: Reputation: 54
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).
 
1 members found this post helpful.
Old 10-05-2018, 01:06 AM   #15
prashant.s
LQ Newbie
 
Registered: Oct 2018
Posts: 1

Rep: Reputation: Disabled
script timing issue

hi

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.
 
  


Reply

Tags
initd, mail, restart



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
init script does not run at the shutdown time procfs Linux - Newbie 4 02-16-2010 02:00 AM
Run script on shutdown or reboot VeeDubbs Linux - Server 1 10-23-2008 01:34 PM
LXer: Shutdown, Reboot and Init Process Flow On Solaris Unix LXer Syndicated Linux News 0 10-09-2008 12:50 PM
Reboot init script doesn't work casey0999 Linux - Software 6 02-25-2004 05:15 PM
control reboot, init 6 ,shutdown paulmccs Linux - General 3 02-27-2002 02:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration