Search code examples
linuxcentosrunlevel

How to run a script when switching from run level 3 to 0


I need to run a script when the server is going for a shutdown i.e. run level 0. But the catch here is, I need to make a curl call in the script which is not possible in run level 0 as the network will be down.

So my question is how can I achieve this ?

Basically what I am looking for is a solution to run a script when the system goes from run level 3 to run level 0.

Any help is appreciated.


Solution

  • You can do that combining all this together

    • Check the current run-level with $ runlevel, $ who -r, etc.
    • Make an executable script in /etc/init.d/ where you check if the current run level is 3.
      And perform your operations you want.
    • Make a symlink in /etc/rc0.d/ with target to your script.
    • The name of the symlink has to satisfy to some criteria in order to run before the networking services are disabled, check them here. Of course it depends on your host, and will be specific to your configuration.

    That's it, this should work !