# Pre & post-hook question

**URL:** <https://community.letsencrypt.org/t/pre-post-hook-question/137280>\
**Category:** Help\
**Created:** [October 31, 2020, 10:34pm UTC](https://community.letsencrypt.org/t/pre-post-hook-question/137280 "2020-10-31T22:34:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfrandin](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dfrandin](https://community.letsencrypt.org/u/dfrandin)\
**Post date:** [October 31, 2020, 10:34pm UTC](https://community.letsencrypt.org/t/pre-post-hook-question/137280/1 "2020-10-31T22:34:22Z")

</div>

I'm using certbot on my mail server, and I need to stop nginx before updating the cert and I need to restart it after updating the cert but I also need to restart dovecot.

Is it possible to do something like this:  
/usr/bin/certbot renew --pre-hook "service nginx stop" --post-hook "service nginx start;;service dovecot restart"

Or do I need to use the pre and post directories and put scripts into them  
to do the stops/starts/restarts?

---

<div class="post-metadata">

**Author:** ![rg305](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/rg305/32/91314_2.png) [@rg305](https://community.letsencrypt.org/u/rg305)\
**Post date:** [October 31, 2020, 10:45pm UTC](https://community.letsencrypt.org/t/pre-post-hook-question/137280/2 "2020-10-31T22:45:01Z")

</div>

I would start with improving the overall renewal checking experience with:

- using the `--deploy-hook` (only when the cert changes) to restart those things that weren't stopped  
[like Dovecot]
- look for a way to use `nginx` instead of having to stop it ~~(to check if a cert needs to be renewed)~~ on each renewal.  
[\<1% of regularly scheduled checks require a renewal]

So, at a bare minimum (improvement), you could do all three:

```nohighlight
/usr/bin/certbot renew \
--pre-hook "service nginx stop" \
--post-hook "service nginx start" \
--deploy-hook "service dovecot restart"

```

> [@dfrandin](#):
>
> do I need to use the pre and post directories and put scripts into them  
> to do the stops/starts/restarts?

For multiple commands... I would think so.  
EDIT (for clarity/completeness): You can either put the scripts in the directory (which will always run) or call the scripts directly from the hooks in the command line (on a one-by-one basis - different cert renewals may require different things to be restarted).  
OR maybe try with " && " instead of just ";;" as a separator.  
Not sure as I don't normally use pre/post hooks.

READERS: Get involved and participate: If you read something you like, then click to like it ❤

---

<div class="post-metadata">

**Author:** ![\_az](https://avatars.discourse-cdn.com/v4/letter/_/22d042/32.png) [@\_az](https://community.letsencrypt.org/u/_az)\
**Post date:** [October 31, 2020, 10:47pm UTC](https://community.letsencrypt.org/t/pre-post-hook-question/137280/3 "2020-10-31T22:47:04Z")

</div>

A couple of things to add:

- The `--pre-hook` won't run unless a renewal is actually going to happen.
- I can confirm for you that you can list multiple commands in a `--pre-hook` like that. This is because the hook is run inside a shell.

---

<div class="post-metadata">

**Author:** ![rg305](https://sea3.discourse-cdn.com/letsencrypt/user_avatar/community.letsencrypt.org/rg305/32/91314_2.png) [@rg305](https://community.letsencrypt.org/u/rg305)\
**Post date:** [October 31, 2020, 10:49pm UTC](https://community.letsencrypt.org/t/pre-post-hook-question/137280/4 "2020-10-31T22:49:56Z")

</div>

> [@\_az](#):
>
> The `--pre-hook` won't run unless a renewal is actually going to happen.

_Possible citation needed there._  
Was it that way always, or did it change to this way with version x.y.zz?

---

<div class="post-metadata">

**Author:** ![\_az](https://avatars.discourse-cdn.com/v4/letter/_/22d042/32.png) [@\_az](https://community.letsencrypt.org/u/_az)\
**Post date:** [October 31, 2020, 10:55pm UTC](https://community.letsencrypt.org/t/pre-post-hook-question/137280/5 "2020-10-31T22:55:14Z")

</div>

Yes, it's been the case since the original implementation of hooks (0.5.0).

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/letsencrypt/original/3X/c/a/ca6c06ea1ea201324bba7048c6841ce60236468d.png) [@system](https://community.letsencrypt.org/u/system)\
**Post date:** [November 30, 2020, 10:55pm UTC](https://community.letsencrypt.org/t/pre-post-hook-question/137280/6 "2020-11-30T22:55:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
