Linux – Run script before shutdown/restart

bashfedorainit.dlinux

I'd like to run a PHP script when an instance is told to shutdown, but of course before it actually finishes shutting down. My particular script is just looking to push some log files from the local partition to a another server.

I've got the gist of how this process works, but I need some clarification.

How I understand it. Please correct me if I'm wrong.

  1. Create an executable script in /etc/init.d (lets call it /etc/init.d/push-logs)
  2. Create a symlink to /etc/init.d/push-logs from /etc/rc0.d (shutdown) and /etc/rc6.d (reboot). The name should be KXXpush-logs

Here's my questions:

  1. Of course – am I understanding correctly?
  2. For #2 above – it sounds like the lower the XX the better – is there too low a number I can use? Does it matter if it shares a number with another script?
  3. Does the script in /etc/init.d/push-logs HAVE to follow the standard init.d template (supporting start/stop, etc. commands)? This doesn't really apply to my use case. If possible I just want the script to be the following:
#!/bin/sh
#
# Run PHP file prior to shutdown
#

/usr/bin/php /path/to/php_file.php

Best Answer

  1. Yes, ut the symlink name should begin with a S - for Start (K for Kill)
  2. The two-digit specifies the order of execution for your script, the lowest numbered being execute first.
  3. Yes, because Linux will execute the equivalent of service push-logs start for the S symlink.