I need to kill some services when shutdown/reboot my Linux server, but I am not sure how Reboot command works.
To my understanding, /etc/rc0.d/ scripts will be invoked when system is shutdown, while the /etc/rc6.d/ scripts will be invoked when system is rebooted.
But according to this link: https://linux.die.net/man/8/reboot, reboot command also call shutdown command except when running on level 0 and 6, so does this mean /etc/rc0.d/ scripts are also invoked when reboot command is run ?
Do I only need to put the service kill script into /etc/rc0.d/ and /etc/rc1.d/ ? Or does it also need to be in /etc/rc6.d/ to make sure the service is stopped when reboot ?
Edit: Add my server configuration. Currently running Redhat, varying from 6.x to 7.2.
If you just want to kill some processes the way you want when you're shutting down , you can write two simple scripts as follows :
and
and make some aliases in the
.bashrc
file of each user you log-in as :and run those commands each time you want to reboot ( or shutdown)
But if you insist to use a init script , you can put your script in the
/etc/init.d
directory and issue the following command (with super-user permission) :But be aware that you should mention the runlevels in the header of your script . For example this can be found for most linux services :
In my system the contents of
/etc/rc0.d
and/etc/rc6.d
is exactly the same , so looks like shutting down and rebooting is the same from the systemd's perspective.If anybody has a better solution I'm glad to hear.