# Update /etc/hosts at rebuild

**URL:** <https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703>\
**Category:** Self-hosting\
**Created:** [2017 年 8 月 24 日午後 3:19 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703 "2017-08-24T15:19:18Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![irek](https://avatars.discourse-cdn.com/v4/letter/i/b9bd4f/32.png) [@irek](https://meta.discourse.org/u/irek)\
**Post date:** [2019 年 7 月 18 日午後 1:15 UTC](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703/10 "2019-07-18T13:15:49Z")

</div>

この bash スクリプトは、Docker コンテナ内の /etc/hosts に SMTP エントリが定義されているかを確認します：  
ファイル: **check\_hosts\_entry**

```plaintext
#!/usr/bin/env bash

smtp=$(docker exec -t app bash -c "cat /etc/hosts | grep -m 1 your_smtp_domain" | tr -d '\r');
if [["$smtp" != "your.ip your_smtp_domain"]]; then
  echo 'SMTP エントリがありません。追加します...';
  docker exec -t app bash -c "echo your.ip your_smtp_domain >> /etc/hosts";
else
  echo 'SMTP エントリは既に存在します!';
fi

```

**crontab**  
ホストの crontab に追加して、30 分ごと（または好きな間隔）に実行するように設定してください：

```plaintext
*/30 * * * * /root/bin/check_hosts_entry >/tmp/stdout.log 2>/tmp/stderr.log

```

_ご自身のデータに合わせて your.ip と your\_smtp\_domain を置き換えるのを忘れないでください 😉_

---

_[View the full topic](https://meta.discourse.org/t/update-etc-hosts-at-rebuild/68703)._
