run a script at shutdown in Debian

9,177

Solution 1

According to the following instruction

/etc/init.d/REDME
/etc/rc?.d/REDME

you need to put the script in /etc/init.d/ and make a symbolic link in /etc/rc0.d/ this corresponds to the shutdown runlevel and if you want to execute the script when rebooting then make a symbolic link in rc6.d. If you list the folder rc0.d then you will see the script starts with S or K. S come from Start if you want to run script and K come from Kill if you want to stop script

Solution 2

Put the script in /etc/rc0.d/ directory, make sure it has execute permissions, and follow the naming convention S*

In your case, you can save your above script in a file at /etc/rc0.d/S00myscript & make sure it is executable by running chmod +x /etc/rc0.d/S00myscript

Share:
9,177
rɑːdʒɑ
Author by

rɑːdʒɑ

Updated on September 18, 2022

Comments

  • rɑːdʒɑ
    rɑːdʒɑ over 1 year

    I am using Debian wheezy. My problem is, I am using a limited 2.5 GB net connection and would like to capture my usage before shutting down. My idea is simply running a script as:

    #!/bin/bash
    date >> ~/mydatausage.txt
    ifconfig -a >> ~/mydatausage.txt
    

    at the time of shutdown.

    How can I run this script at shutdown, as root?

  • rɑːdʒɑ
    rɑːdʒɑ over 10 years
    can you improve the answer by direct instructions.
  • Chirag Bhatia - chirag64
    Chirag Bhatia - chirag64 over 10 years
    Done. You can try the instructions now.
  • Egor Skriptunoff
    Egor Skriptunoff about 7 years
    It seems that this solution does not work anymore (I'm using Debian 8.7)