# Shutdown -r now and stop monit process first



## CyberCr33p (Oct 27, 2018)

Is it possible during "shutdown -r now" to stop monit process before the other processes?


----------



## aragats (Oct 27, 2018)

I would use /etc/rc.local:
	
	



```
monit start all
```
 and /etc/rc.shutdown.local:
	
	



```
monit stop all
```
If you use only the latter, monit will remember its state and won't start your processes upon boot.


----------



## CyberCr33p (Oct 28, 2018)

Thank you for your reply. What I want to achieve is during a server reboot to avoid monit trying to start services already stoped by the shutdown process.


----------



## aragats (Oct 28, 2018)

In such case /etc/rc.shutdown.local should be enough.
By the way, I use monit in a commercial embedded project, and noticed that sometimes it starts processes even if they were stopped before shutdown. Haven't investigated what causes that since I do need start them anyway on boot.


----------



## CyberCr33p (Oct 28, 2018)

I think this is not what I want to achieve. My english is not so good so I will try to explain again.

During a server reboot/shutdown we have for example:

1) nginx stop
2) php-fpm stop
3) mysql stop
4) postfix stop
4) monit stop
5) something else stop, etc

If a service stops before monit then in some cases I get an e-mail from monit that says "service is down" and tries to start it again.

This is what I want to avoid, so stoping monit before all the other processes will solve this issue.


----------



## aragats (Oct 28, 2018)

CyberCr33p said:


> I get an e-mail from monit that says "service is down" and tries to start it again


Right, I understood what you mean from the very beginning. First of all, you should *disable* your services like nginx, mysql etc. in /etc/rc.conf since you start them with monit. Then, as I said before, add the corresponding lines to /etc/rc.local and /etc/rc.shutdown.local to start/stop them by monit.
Check the order of services with rcorder(8):
	
	



```
rcorder /etc/rc.d /usr/local/etc/rc.d
```
The shutdown order will be reversed. You want that monit starts before local.
You can adjust the order of services if needed by adding REQUIRE keyword to the corresponding service script in /etc/rc.d. Check rc(8) and rcorder(8). /etc/rc.d/local is the service you want (it executes /etc/rc.local and /etc/rc.shutdown.local). Alternatively, instead of using /etc/rc.d/local you can create your own service.


----------



## CyberCr33p (Oct 28, 2018)

Thank you for your replies.


----------



## bdrewery@ (Jun 16, 2022)

rc.local and rc.shutdown.local are not sufficient alone as /etc/rc.d/local is not the last script returned by rcorder.

```
> rcorder -k shutdown /etc/rc.d/* /usr/local/etc/rc.d/*
<a bunch of stuff trimmed out>
/usr/local/etc/rc.d/syncthing
/usr/local/etc/rc.d/openvpn
/usr/local/etc/rc.d/oidentd
/usr/local/etc/rc.d/monit
/usr/local/etc/rc.d/git_daemon
/usr/local/etc/rc.d/fail2ban
/usr/local/etc/rc.d/syncthing-discosrv
/usr/local/etc/rc.d/syncthing-relaypoolsrv
/usr/local/etc/rc.d/syncthing-relaysrv
/etc/rc.d/moused
/usr/local/etc/rc.d/svnserve
/etc/rc.d/ftp-proxy
/etc/rc.d/utx
/etc/rc.d/rwho
/etc/rc.d/rtadvd
/etc/rc.d/powerd
/etc/rc.d/ntpd
/etc/rc.d/nscd
/etc/rc.d/local
/usr/local/etc/rc.d/openssh
/etc/rc.d/mountd
/etc/rc.d/swaplate
/usr/local/etc/rc.d/cupsd
/etc/rc.d/nfsd
/usr/local/etc/rc.d/netatalk
/usr/local/etc/rc.d/samba_server
/etc/rc.d/inetd
/etc/rc.d/ftpd
/usr/local/etc/rc.d/clamav-clamd
/usr/local/etc/rc.d/kpropd
/usr/local/etc/rc.d/socat
/usr/local/etc/rc.d/squid
/usr/local/etc/rc.d/redis
/usr/local/etc/rc.d/rsyncd
/usr/local/etc/rc.d/vboxheadless
/usr/local/etc/rc.d/vboxwatchdog
/usr/local/etc/rc.d/vboxwebsrv
/usr/local/etc/rc.d/sentinel
/usr/local/etc/rc.d/php-fpm
/usr/local/etc/rc.d/nginx
/etc/rc.d/sshd
/etc/rc.d/cron
/usr/local/etc/rc.d/iocage
/usr/local/etc/rc.d/clamav-freshclam
/etc/rc.d/jail
/usr/local/etc/rc.d/sslh
/usr/local/etc/rc.d/ezjail
/usr/local/etc/rc.d/postfix
/usr/local/etc/rc.d/smartd
```
(This would be reversed for shutdown but what we want is monit to be listed last in the above so it stops and starts first)

See that 'monit' comes somewhere in the middle and 'local' comes somewhere in the middle after that.
There simply is no quick way to make this happen by using .local files or adding 1 line to the monit rc script.

I'm using this patch:

```
diff --git libexec/rc/rc libexec/rc/rc
index 69edcf4ac9ff..7909a5e1285a 100644
--- libexec/rc/rc
+++ libexec/rc/rc
@@ -125,10 +125,21 @@ for _rc_elem in ${files}; do
        case "$_rc_elem_done" in
        *" $_rc_elem "*)        continue ;;
        esac
+       case "${_rc_elem}" in
+       */rc.d/monit)
+               _monit="${_rc_elem}"
+               continue
+               ;;
+       esac

        run_rc_script ${_rc_elem} ${_boot}
 done

+if [ -n "${_monit}" ]; then
+       run_rc_script ${_monit} ${_boot}
+       unset _monit
+fi
+
 # Remove the firstboot sentinel, and reboot if it was requested.
 # Be a bit paranoid about removing it to handle the common failure
 # modes since the consequence of failure can be big.
diff --git libexec/rc/rc.shutdown libexec/rc/rc.shutdown
index 704546b1fb2a..2653178f9f8a 100644
--- libexec/rc/rc.shutdown
+++ libexec/rc/rc.shutdown
@@ -98,7 +98,16 @@ esac

 files=`rcorder ${rcorder_opts} /etc/rc.d/* ${local_rc} 2>/dev/null`

-for _rc_elem in `reverse_list $files`; do
+_rev_list=`reverse_list $files`
+for _rc_elem in ${_rev_list}; do
+       case "${_rc_elem}" in
+       */rc.d/monit)
+               debug "run_rc_script $_rc_elem faststop"
+               run_rc_script $_rc_elem faststop
+               ;;
+       esac
+done
+for _rc_elem in ${_rev_list}; do
        debug "run_rc_script $_rc_elem faststop"
        run_rc_script $_rc_elem faststop
 done
```

What we really need is rcorder(8) or the rc scripts to allow forcing something last. You would think "local" is that but it's not.


----------

