Go Back   AnandTech Forums > Software > *nix Software

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Home and Garden
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 03-13-2012, 09:36 PM   #1
Red Squirrel
Lifer
 
Red Squirrel's Avatar
 
Join Date: May 2003
Location: Canada
Posts: 28,418
Default how do I make a script run at linux restart/shutdown?

I have an app that needs to be shutdown in a graceful way which is done by deleting a file that it checks for at certain intervals. Is there a way to script it so this file gets deleted when the system is either shut down or rebooted?

Eventually I want to learn how to do a start/stop script but I just want something quick and easy for now. I looked at them and it's way over my head for the time being. I just want something easy till I get around to diving deeper into those.
__________________
~Red Squirrel~
486dx2 @66Mhz turbo, 8MB ram, 512MB HDD, sound blaster 16 + 2x cdrom, Trident 1MB video card @ 640*480, 56k high speed modem.
Red Squirrel is online now   Reply With Quote
Old 03-13-2012, 09:54 PM   #2
bobross419
Golden Member
 
bobross419's Avatar
 
Join Date: Oct 2007
Posts: 1,979
Default

Have you tried just adding a rm to the bash.logout?
__________________
"We don't make mistakes here, we just have happy accidents... If you want sad things, watch the news. Everything is possible here. This is your little universe."

-Bob

http://railrockit.com/ "Just Rock It to Lock it"
bobross419 is offline   Reply With Quote
Old 03-13-2012, 10:01 PM   #3
Red Squirrel
Lifer
 
Red Squirrel's Avatar
 
Join Date: May 2003
Location: Canada
Posts: 28,418
Default

Isin't that just for when the logged in user logs out though? I need it to happen at system reboot. Ex: UPS or other process initializes a reboot.
__________________
~Red Squirrel~
486dx2 @66Mhz turbo, 8MB ram, 512MB HDD, sound blaster 16 + 2x cdrom, Trident 1MB video card @ 640*480, 56k high speed modem.
Red Squirrel is online now   Reply With Quote
Old 03-13-2012, 10:27 PM   #4
PCTC2
Diamond Member
 
PCTC2's Avatar
 
Join Date: Feb 2007
Location: San Diego
Posts: 3,862
Default

Well, for start/stop scripts (like init scripts at run levels) just need a start/stop/restart function block. Something like
Code:
case "$1" in
   start)
       <your code here>
        ;;
   stop)
      <your code here>
        ;;
   restart|force-reload)
       <your code here>
        ;;
   reload)
        <your code here>
        ;;
        <your code here>
        ;;
    *)
         echo "Usage: $0 {start|stop|restart|status}"
         exit 1
         ;;
esac
exit 0
Then put it in /etc/init.d (or similar) and run chkconfig (red hat) or update-rc.d (debian) with the proper arguments to get it to start and stop on certain run levels.

Another option is to run once at start, which you can do in crontab. Instead of the time codes in crontab, you can use a tag like so:
Code:
@reboot   /path_to_script/executable_script.sh
__________________
-E3200,DG45FC,4GB,SSD - Q6600,GT240,eVGA 680i SLI,4GB - Q9400,8800GTS G92,EP45-UD3P,4GB
-Ci7 920,U2711,GT240,X58 FTW3,12GB,SSD - Ci7 860,HD5670,eVGA P55M,8GB,SSD
-17" MBP Ci5 - Mac Mini Server C2D - Mac Mini Server Ci7
-2x Xeon E5520,SM X8DTH-6F,24GB,ESXi5 - Xeon E3-1220,8TB RAID5,SM X9SCM-F,8GB,SSD -E3300,G43ITX,4GB,7.5TB RAID-Z+SSD
HEATWARE
PCTC2 is offline   Reply With Quote
Old 03-13-2012, 10:29 PM   #5
Red Squirrel
Lifer
 
Red Squirrel's Avatar
 
Join Date: May 2003
Location: Canada
Posts: 28,418
Default

Oh wow is the start/stop script that simple? All the ones I looked at were really complicated. I will try that first, but good to know about the crontab option, I may try that as well.
__________________
~Red Squirrel~
486dx2 @66Mhz turbo, 8MB ram, 512MB HDD, sound blaster 16 + 2x cdrom, Trident 1MB video card @ 640*480, 56k high speed modem.

Last edited by Red Squirrel; 03-13-2012 at 10:32 PM.
Red Squirrel is online now   Reply With Quote
Old 03-14-2012, 09:35 AM   #6
Nothinman
Elite Member
 
Nothinman's Avatar
 
Join Date: Sep 2001
Posts: 30,672
Default

They're only as complicated as the system/service requires. RedHat used to come with a skeleton script that was similar to that. But the basic gist is that you just need to handle the start/stop options if you only want the script for bootup/shutdown.
__________________
http://www.debian.org
Nothinman is offline   Reply With Quote
Old 03-14-2012, 02:56 PM   #7
Gooberlx2
Lifer
 
Gooberlx2's Avatar
 
Join Date: May 2001
Location: Denver, CO
Posts: 14,848
Default

Which system? In Opensuse/SLES, for example, there's /etc/init.d/halt.local where you can enter scripts/commands to run right before shutdown/reboot.

But yeah, writing your own init scripts is probably the best way to do it.

Last edited by Gooberlx2; 03-14-2012 at 02:59 PM.
Gooberlx2 is offline   Reply With Quote
Old 03-15-2012, 01:19 PM   #8
Fallen Kell
Diamond Member
 
Fallen Kell's Avatar
 
Join Date: Oct 1999
Posts: 3,972
Default

Be aware, shutdown scripts are not guaranteed to run. There are numerous situations where they are skipped, or not given long enough time to execute (as well as the cases of the system losing power, kernel panic, or hardware failure). Better to have a wrapper script around your application's start routine which checks to see if it is already properly running, and if it is not, have it clean up things and start from the clean state. Using things like creating a file like "/var/run/<application_name>.pid" and putting the PID of your application in that file, and then you simply check if that file exists, then check to see if that PID exists, and that the PID is indeed your application is the better way to handle your situation.
__________________
We were all warned of MS problems many years ago; remember how the Magic 8 Ball always said: "Outlook not so good"?
Server
HTPC
Fallen Kell is offline   Reply With Quote
Reply

Thread Tools

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

Forum Jump


All times are GMT -5. The time now is 04:47 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.