3

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 on runlevel 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 ?

Cbhihe
  • 2,761
  • 3
  • 24
  • 47
  • +1: @AndroidDev: It does address 75% of my question, but: --- 1) as pointed by terdon, Radu Rădeanu's post is wrong in multiple places even though it is the most up-voted answer. (Does a Lemmings quietly sleep in each and every one of our geeky souls, easily impressed by big karma-posters ?) --- 2) The link does not answer my doubt about whether the power-down sequence will pause while the scripts executes to completion. --- I wouldn't use that post as a reference for dupes. – Cbhihe May 17 '16 at 15:38
  • @Cbhihe Is there a different answer there which is satisfactory? – belacqua May 22 '16 at 20:28
  • @Cbhihe re. 'there' : The same link that you referred to in your previous comment : http://askubuntu.com/questions/293312/execute-a-script-upon-logout-reboot-shutdown-in-ubuntu – belacqua May 24 '16 at 17:52
  • @Belacqua: Sorry. Now I get it. Answer: No, there is none. The question wells up being "how can I ensure that scripts executing at run levels 6 or 0, when powering, down execute to completion ?" -- I found zilch in that department. That is the reason I believe my question is NOT dupe at all. – Cbhihe May 25 '16 at 06:46

0 Answers0