0

I know this an old question but I am having a tough time in understanding the linux runlevels w.r.t gracefully stopping and starting a service. For suppose consider myscript service is configured to auto-start after a system shutdown/reboot. Below is the output of chkconfig command (RHEL 6.5x):

[testfolk@jomohost ~]$  sudo chkconfig --list|grep myscript
myscript             0:off   1:off   2:on    3:on    4:off    5:on    6:off

I understand that myscript service auto-starts on system startup. But what takes care of it's gracefull stop/shutdown task. What if myscript is a delicate service and need to be stopped at any cost before any reboot/shutdown or the application gets corrupted. Is taken care of by default ?

Can I configure it something like below to do a graceful auto stop:

[testfolk@jomohost ~]$  sudo chkconfig --list|grep myscript
myscript             0:on   1:off   2:on    3:on    4:off    5:on    6:on

My assumption of above configuration is the myscript service gets auto stopped on invoking init 6 or init 0. Is this a valid config, I can be completely wrong, please advise. I searched as many articles as possible but all of them were talking about auto-start of a service after a reboot or shutdown but not auto-stop. I am not sure if I am understanding this whole thing correctly, pls help if you have any info!

Thanks in advance for your time!

  • The scripts in `/etc/rc``.d` will start and stop the service as required. The first configuration is correct (and default). Your service autostarts when the system transitions to runlevels 2, 3, or 5, and autostops when the system transitions to runlevels 0, 1, 4, or 6. (Hint: In Red Hat Linux, runlevels are 0 Halt, 1 Single-user, 2 User-defined, 3 Full multi-user text mode, 4 User-defined, 5 Full multi-user graphical mode, and 6 Reboot.) So the service will gracefully autostop when the system is about to reboot or to power-off, or to go into single-user mode. – AlexP Oct 17 '19 at 16:16
  • Yepp understood. So the moment a service is added to chkconfig, it takes care of the startup scripts and `on` and `off` are typically like start and stop commands that are passed to the startup script based on the runlevel it runs. Also to add a point to your answer, `man chkconfig` says: `By default, the on and off options affect only runlevels 2, 3, 4, and 5, while reset and resetpriorities affects all of the runlevels` So I guess whether the value is on or off for runlevels 0 and 6, the system seems not to care and defaults to off like operation. Thanks @AlexP – Just_existing Oct 17 '19 at 20:38
  • Never mind, I got it, reading the man page carefully tells me that if you use chkconfig to turn a service on, it will be on for runlevels 2, 3, 4, and 5, unless you use --level, in which case it will turn on for whatever levels you asked for, even if they include 1, 2, or 6. – Just_existing Oct 17 '19 at 21:29

0 Answers0