Where to put script to be executed before shutdown ?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-24-2017
RedHat Where to put script to be executed before shutdown ?

Hello,

My AIM is to run my script every time when someone shutdown or reboot my RHEL7 AWS instance.

Query ) Where should i put my script .So, that it gets automatically executed when a server is going down or reboot ?
# 2  
Old 05-24-2017
You would be looking to create the equivalent of a service. If you look in /etc/init.d you will see the scripts used for controlling them and you can create yours here. Make sure it is extremely robust or you may have trouble with the shutdown, or worse problems on boot. Use an existing script to get the skeleton of the script correct.

To get it to run, first determine what is your usual run-level with who -r

Then, using the number returned (usually 2, 3 or 5) link to your script from /etc/rc.d/rc.n.d as a script named with a leading K This will cause it to be run when you leave the run level, i.e. to shut down or reboot.

It is not a process to be done lightly, so be careful. If you get it right, you can run it with service yourname stop to test it. It should be written to ignore or at least recognise service yourname start You could even do similar or varied things with it on system startup. Base it on a known good script that is relatively simple, such as the ntpd service script.


Let us know how you are getting on and if we can help more.



Robin
(half expecting to be castigated/castrated because there is an official way to create a script like this Smilie)
# 4  
Old 05-28-2017
Write a service file and place it in /etc/systemd/system/beforeshuttingdown.service
Code:
[Unit]
Description=Before Shutting Down

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=<path to script or program>

[Install]
WantedBy=multi-user.target

Your program or script must be executable.
Code:
systemctl daemon-reload
systemctl enable beforeshuttingdown.service
systemctl start beforeshuttingdown.service


Last edited by Aia; 05-28-2017 at 09:41 PM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Create file when script has executed drew77 Shell Programming and Scripting 2 08-26-2018 08:41 PM
Script is not getting executed ripudaman.singh Shell Programming and Scripting 6 11-03-2017 08:50 AM
Capture run time of python script executed inside shell script Adfire Shell Programming and Scripting 2 04-15-2017 01:45 AM
/etc/cron.daily script is not being executed anil529 UNIX for Beginners Questions & Answers 3 03-31-2017 01:56 PM
Script executed by Cron or commandline API Shell Programming and Scripting 3 06-30-2015 07:38 AM
INIT Script Getting Executed Twice? mrm5102 Shell Programming and Scripting 6 06-11-2014 10:47 AM
Shell script not getting executed pracheth Shell Programming and Scripting 3 08-01-2013 04:57 AM
Shell script executed from Informatica ETL tool is spawning 2 processes for one script chekusi Shell Programming and Scripting 2 05-21-2013 10:00 AM
help with shell script executed by php. dmallia Shell Programming and Scripting 1 01-25-2012 12:34 PM
script has been executed successfully or not?? bobby1015 Shell Programming and Scripting 2 07-21-2011 01:58 PM
Script not getting executed via cron but executes when executed manually. samsungsamsung AIX 3 07-15-2011 10:33 AM
Variables of executed script available in executing script dips_ag Shell Programming and Scripting 9 05-11-2010 11:54 PM
Script to force Oracle database shutdown when shutdown immediate does not work aixhp UNIX for Dummies Questions & Answers 7 03-11-2010 01:04 PM
Script errors out only when its executed via job mrx1350 Shell Programming and Scripting 6 03-26-2009 01:50 PM
How to know who executed a script? on9west Shell Programming and Scripting 2 07-28-2008 05:52 AM