# Jails are not shutdown during reboot?!



## Ludwig (Mar 25, 2015)

I am new to FreeBSD and experimenting with jails. I have two jails configured with jail and ezjail. These jails should always getting started if the computer boots. If I reboot my computer the jails are sometimes not started and I get this message during the boot up sequence.

```
Starting jails: [jailname already running (/var/run/jail_jailname.id exists)]
```
I have a suspicion that the shutdown command is not send to the jails. The processes in the jail are just killed by the host and the lock file gets not removed. How can I be sure that the jails are always running if I start the computer? I am running FreeBSD 9.3-RELEASE-p9.


----------



## junovitch@ (Mar 25, 2015)

By default, `/bin/sh /etc/rc.shutdown` is called by /etc/rc.d/jail to shutdown jails gracefully.  However, that is only called when `shutdown -r now` is used instead of `reboot`.  This is a little FreeBSD nuance with the reboot(8) command.


> Normally, the shutdown(8) utility is used when the system needs to be
> halted or restarted, giving users advance warning of their impending doom
> and cleanly terminating specific programs.



Basically `reboot` just has the init(8) process terminate everything while shutdown(8) calls /etc/rc.shutdown which asks this to shut down gracefully.  Generally use `shutdown -r now` to reboot and `shutdown -p now` to power off.


----------



## wblock@ (Mar 26, 2015)

Dan Langille pointed out that ezjail does not run /etc/rc.shutdown by default.  To do that, this needs to be added to the jail configuration file in /usr/local/etc/ezjail/_jailname_:

```
export jail_dns1_exec_stop="/bin/sh /etc/rc.shutdown"
```


----------



## junovitch@ (Apr 5, 2015)

Another way, just so you don't have to remember to add it to each jail:
`echo 'ezjail_exec_stop="/bin/sh /etc/rc.shutdown"' >> /usr/local/etc/ezjail.conf`


----------



## gkontos (Apr 5, 2015)

I have seen many people coming from the Linux world using `reboot` instead of `shutdown -r now`. Is there any real advantage of `reboot` other than in single user mode?


----------



## wblock@ (Apr 5, 2015)

Er... it is quicker because it doesn't have to wait for services to shut down?


----------



## gkontos (Apr 5, 2015)

wblock@ said:


> Er... it is quicker because it doesn't have to wait for services to shut down?


Imagine doing that on a busy database server


----------



## Kiiski (Apr 5, 2015)

wblock@ said:


> Dan Langille pointed out that ezjail does not run /etc/rc.shutdown by default.  To do that, this needs to be added to the jail configuration file in /usr/local/etc/ezjail/_jailname_:
> 
> ```
> export jail_dns1_exec_stop="/bin/sh /etc/rc.shutdown"
> ```





junovitch said:


> Another way, just so you don't have to remember to add it to each jail:
> `echo 'ezjail_exec_stop="/bin/sh /etc/rc.shutdown"' >> /usr/local/etc/ezjail.conf`



From man page ezjail(7):



> jail_JAILNAME_exec_stop
> The command to run inside the jail when stopping it. Defaults to
> the empty string, which means ``/bin/sh /etc/rc.shutdown''.



So according to man page ezjail should run /etc/rc.shutdown by default, shouldn't it?


----------



## gkontos (Apr 5, 2015)

It should. My jails are usually configured manually without the use of a utility because I am a masochist. But I never use `reboot` in FreeBSD.


----------



## junovitch@ (Apr 8, 2015)

Kiiski said:


> From man page ezjail(7):
> 
> 
> 
> ...



That's a good catch.  It also makes sense that it's blank by default if that is the default behavior.  I did a quick search and found this on the topic: 

https://elektropost.org/ezjail/msg00800.html


----------



## dvl@ (Feb 25, 2017)

This has been biting me lately.

Everytime I shutdown my Ubuiqity Unifi controller jail, it would not start up properly. I suspected a database issue.  The only want to recover from was a database backup.

This is far from ideal.

I suspect others are also being bitten.

Is it time for this to be the default?  Claims are made above that it is the default, but clearly it is not.

My fix was:


```
$ grep stop unifi01 
export jail_unifi01_exec_stop="/bin/sh /etc/rc.shutdown"
```

I also added this to /usr/local/etc/ezjail.conf:


```
ezjail_exec_stop="/bin/sh /etc/rc.shutdown"
```


----------

