View previous topic :: View next topic |
Author |
Message |
petterg Guru
Joined: 25 Mar 2004 Posts: 500 Location: Oslo, Norway
|
Posted: Wed Aug 27, 2014 8:29 am Post subject: Run script on shutdown, but not on reboot |
|
|
On shutdown or reboot gentoo will by default run any executable file in /etc/local.d/*.stop
What would be the best way to have a script run on shutdown, but not on reboot? |
|
Back to top |
|
|
krinn Watchman
Joined: 02 May 2003 Posts: 7470
|
Posted: Wed Aug 27, 2014 12:19 pm Post subject: |
|
|
Code: | cat /etc/init.d/shutdown.sh
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
opts="-d"
[ "${INIT_HALT}" != "HALT" ] && opts="${opts}p"
[ "${RC_DOWN_INTERFACE}" = "yes" ] && opts="${opts}i"
[ "${RC_DOWN_HARDDISK}" = "yes" ] && opts="${opts}h"
/sbin/halt "${opts}"
# hmm, if the above failed, that's kind of odd ...
# so let's force a halt
/sbin/halt -f
|
So i would try doing the same : look at ${INIT_HALT} value and if "HALT" continue, else end of work.
But as i don't know if the env is set for the local scripts, i would just say, test your script and echo if INIT_HALT have any value. |
|
Back to top |
|
|
Atom2 Apprentice
Joined: 01 Aug 2011 Posts: 185
|
Posted: Wed Aug 27, 2014 6:57 pm Post subject: Re: Run script on shutdown, but not on reboot |
|
|
petterg wrote: | What would be the best way to have a script run on shutdown, but not on reboot? | My take would be to integrate the script into the service architecture provided by /etc/init.d & /etc/conf.d together with OpenRC. If the script - as all scripts in /etc/init.d - uses /sbin/runscript as its interpreter you do have a number of environment variables available which you can use within your script to do certain things conditionally.
One of the variables is named RC_RUNLEVEL which is set to "shutdown" on both reboot and shutdown - so using this variable you should be able to see whether your system is going down at all or is starting up or just changing a runlevel.
Over and above this there is another environment variable available named RC_REBOOT which contains "YES" if the system is rebooting.
I have not tested this, but have done a few other things with init scripts recently and as such have invested quiet a bit of time reading the documentation. All of this information and more is available under
I hope that helps, Atom2 |
|
Back to top |
|
|
szatox Advocate
Joined: 27 Aug 2013 Posts: 3133
|
Posted: Wed Aug 27, 2014 8:10 pm Post subject: |
|
|
you can also launch 'runlevel'
It reports 2 values, previous one first and the latter is current runlevel.
I'd expect it to report 0 for shutdown and 6 for reboot. Never tested border-cases though |
|
Back to top |
|
|
Fitzcarraldo Advocate
Joined: 30 Aug 2008 Posts: 2034 Location: United Kingdom
|
Posted: Sun Sep 06, 2015 3:28 pm Post subject: |
|
|
I initially thought I would be able to use the runlevel command or the RUNLEVEL environment variable in a script (man runlevel refers to both). But I could not get my script /etc/local.d/10-run_on_shutdown.stop to work using either. However, the command who -r does work for me with OpenRC:
Code: | $ cat /etc/local.d/10-run_on_shutdown.stop
#!/bin/bash
if [ `who -r | awk '{print $2}'` = "0" ]; then
echo "shutting down" >> /home/fitzcarraldo/test.txt
date >> /home/fitzcarraldo/test.txt
elif [ `who -r | awk '{print $2}'` = "6" ]; then
echo "rebooting" >> /home/fitzcarraldo/test.txt
date >> /home/fitzcarraldo/test.txt
else
echo "neither rebooting nor shutting down" >> /home/fitzcarraldo/test.txt
date >> /home/fitzcarraldo/test.txt
fi
$ |
After rebooting, then shutting down and booting, then rebooting again, the contents of test.txt show that the above-mentioned simple script works as intended:
Code: | $ cat test.txt
rebooting
Sun 6 Sep 12:42:11 BST 2015
shutting down
Sun 6 Sep 12:46:04 BST 2015
rebooting
Sun 6 Sep 12:49:31 BST 2015
$ |
So the following script /etc/local.d/10-run_on_shutdown.stop will do the desired job:
Code: | $ cat /etc/local.d/10-run_on_shutdown.stop
#!/bin/bash
if [ `who -r | awk '{print $2}'` = "0" ]; then
######################################################################
# Put Bash commands here to be executed on shutdown but not on reboot.
######################################################################
fi
$ |
_________________ Clevo W230SS: amd64, VIDEO_CARDS="intel modesetting nvidia".
Compal NBLB2: ~amd64, xf86-video-ati. Dual boot Win 7 Pro 64-bit.
OpenRC udev elogind & KDE on both.
Fitzcarraldo's blog |
|
Back to top |
|
|
|