I want to run a simple rsync
-based bash script as root when a power-down cycle initiates from either:
- a login interactive shell, or
- a gui session, or
- a machine boot state where no login-user has opened a session (e.g. perhaps a headless server ?)
i.e. irrespectively of whether power down is initiated from a console-like session, from a gui-login session or from an internal mechanism (e.g. cron scheduling or ssh instructions passed remotely, ...).
Note that by power-down I mean either shutdown (rc0) or reboot (rc6).
For the use-case "run script in context of a gui session", @Krytarik offers a solution on UF (see #2 in thread), based on lightdm
. The solution (repeated elsewhere on AU, viz [ref2], [ref3]) only addresses running a script at gui session logout via configuration of /etc/lightdm/lightdm.conf
, NOT immediately before machine power-down which is what I want...
EDIT:
The alternative is a script triggered at certain run-levels, but because in that script rsync
will deal with up to 2~3 GB of data, the power-down sequence must pause during script execution and automatically resume after completion. I found:
- a solution based on placing
K##-scriptname.sh
script directly in/etc/rcX.d/
, where X depends onrunlevel
and##
conforms to regex[0-9][0-9]
. - a post, where I gather that
K##_scriptname.sh
ought to be placed in/etc/init.d/
and symbolic links pointing to the script placed in/etc/rc0.d
(shutdown) and/etc/rc6.d/
(reboot). Beside the fact that in that post, the most up-voted answer is incorrect and was not accepted, I am left with ensuring that the script (however long in executing) will actually execute to completion.
AFAIK a shutdown/reboot signal sent to the process management daemon, init
, translates (among other thing) as "stop all current processes in an orderly fashion". init
will do what it can "to honor jobs kill timeout values" [see man 8 init
]. But what if jobs take longer in completing than the timeout values admitted by system policy (currently 5 seconds I think) ? I expect that to be the case for my script.
TL&DR:
A script /etc/init.d/K00_ramdisk-sync.sh
with symlinks in /etc/rc0.d/
and /etc/rc6.d/
executes at the beginning of every shutdown/reboot sequence.
Question:
How can I ensure that the script runs to completion no matter how long-winded, if need be by having the shutdown/reboot sequence pause and resume as needed ?