LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-15-2012, 02:56 PM   #1
jc_oo12
LQ Newbie
 
Registered: Feb 2005
Posts: 5

Rep: Reputation: 0
How to execute script upon reboot/shutdown?


Hi,

I'm trying to execute a script that does an rsync to another machine (among other things) upon reboot and/or shutdown. I've tried creating a symbolic link to the script in /etc/rc6.d for reboot and /etc/rc0.d for shutdown but the script never executes because the network is already killed. I realize the scripts run in alphabetical order which is why I would do /etc/rc6.d/K01MyScript.sh but that doesn't work either.

I'm running CentOS 6.2 + gnome 3. I believe NetworkManager is what is "handling" the network connection... which gets killed at K32NetworkManager.

Is the runlevel way the right approach or can I try something else?
 
Old 10-15-2012, 03:12 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
I don't use CentOS, but http://www.linuxquestions.org/questi...4/#post3893736 seems to have a reasonably informed opinion on it...

You could also put a line in K32NetworkManager and in your script which both write to a temporary file so you can check that the network is indeed up when you attempt your rsync.

Hope this helps,
 
Old 10-15-2012, 03:21 PM   #3
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,617

Rep: Reputation: 442Reputation: 442Reputation: 442Reputation: 442Reputation: 442
Quote:
I realize the scripts run in alphabetical order which is why I would do /etc/rc6.d/K01MyScript.sh but that doesn't work either.
I think it is not the Network Manager that kills but the moment when the protocol and carrier are closed. <I don't run centOS but if it uses /etc/inittab --the way Slackware does-- you might be able to define rc6 action by running your rsync script first before it hands over the signal.>

Hope that helps.
 
Old 10-15-2012, 04:45 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,259

Rep: Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624Reputation: 1624
When entering a new runlevel the start scripts run first i.e. SXX i.e S00xx - S99xx. Your rsync script should be S00MyScript.sh so it is the first script to run.

A S00 script might already exist so you will need to move links around.

Last edited by michaelk; 10-15-2012 at 04:50 PM.
 
Old 10-15-2012, 05:19 PM   #5
jc_oo12
LQ Newbie
 
Registered: Feb 2005
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by michaelk View Post
When entering a new runlevel the start scripts run first i.e. SXX i.e S00xx - S99xx. Your rsync script should be S00MyScript.sh so it is the first script to run.

A S00 script might already exist so you will need to move links around.
You'd think that would work, right? I tried a series of things:

First, that script didn't run as S00aaaa.sh (just so it executes before kill/halt/shutdown, etc.) Then, I decided to test the "network theory" by creating another simple script that just touches a file and surely, the file was there after I rebooted it. So... it appears that the K scripts run before the S scripts... right? Network was not available for it to run an rsync. I tested that theory again by changing the same script that does a ping and writes to a file. Now it writes a file but the contents are empty. What is going on?
 
Old 10-15-2012, 05:23 PM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,327

Rep: Reputation: 254Reputation: 254Reputation: 254
K=kill at shutdown
S=start at startup
 
Old 10-15-2012, 05:50 PM   #7
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 12.04/14.04, Scientific Linux 6.3/6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,799

Rep: Reputation: 157Reputation: 157
Shutdown might not necessarily work with the following, but startup should,... You can use cron/anacron for an rsync job on startup, just make sure to make the cron file reference it... On shutdown you'd likely have to modify your init states,... but you could, again, look into editing it so that it executed a cron job.

That way, startup... no mods,... shutdown,... minimal mods... All the real work would be defined in the cron job.
 
Old 10-15-2012, 11:31 PM   #8
Toadbrooks
Member
 
Registered: Jul 2008
Distribution: Linux Mint 16
Posts: 81

Rep: Reputation: 11
Quote:
Originally Posted by jc_oo12 View Post
I've tried creating a symbolic link to the script in /etc/rc6.d for reboot and /etc/rc0.d for shutdown but the script never executes because the network is already killed. I realize the scripts run in alphabetical order which is why I would do /etc/rc6.d/K01MyScript.sh but that doesn't work either.
Try putting your scripts in /etc/rc5.d or /etc/rc3.d or whatever level CentOS normally runs at. I don't use CentOS, but I assume 5 is the standard runlevel.

In my experience, since you don't run at 0 or 6, scripts placed here are not executed. I'm fairly sure that a startup script placed in /etc/rc5.d will execute if you boot up to run level 5. As far as shutting down goes, it may be necessary to use the shutdown command (shutdown -r now) rather than the init command (init 6) to get the kill version to run. Again, I have no CentOS experience, so that's just a potential gotcha to be aware of.
 
Old 10-16-2012, 12:01 AM   #9
jsaravana87
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 558
Blog Entries: 5

Rep: Reputation: Disabled
The Thing which actually runs on Reboot/Shutdown are not normally a Service.If you want the script to be executed on Reboot/shutdown place it in Crontab


crontab -e
@Reboot /path/to/script -You can find your script execute on every reboot process takes place
 
Old 10-16-2012, 04:52 AM   #10
ggallozz
Member
 
Registered: May 2011
Posts: 34

Rep: Reputation: Disabled
For Ubuntu ...

... but I guess is usable in all Linux flavors :

http://en.kioskea.net/faq/3348-ubunt...p-and-shutdown
 
Old 10-16-2012, 06:04 AM   #11
padeen
Member
 
Registered: Sep 2009
Location: Perth, W.A.
Distribution: Slackware 14, Debian Sid, FreeBSD 10, OpenBSD
Posts: 191

Rep: Reputation: 36
Is running rsync on shutdown a good idea? I'm particularly thinking of an emergency like flooding or imminent power outage. If you really have to shutdown *now*, the last thing you want is an rsync that could take several minutes to run, especially since rsync by its nature will leave buffers unsynced if the power went out in the middle of it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Init.d script not executing at shutdown and reboot roberbizimhatemo Linux - General 13 05-04-2012 08:28 AM
Adding a shutdown/reboot script? icmp_request Linux - Newbie 3 04-08-2012 10:17 PM
[SOLVED] Shutdown :The system goes down before my rc script execute ! alcazar64 Red Hat 8 04-02-2010 11:31 AM
Shutdown and Reboot Linux System via php script ramasubbu1984 Linux - Newbie 7 03-24-2009 10:09 AM
Run script on shutdown or reboot VeeDubbs Linux - Server 1 10-23-2008 02:34 PM


All times are GMT -5. The time now is 04:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration