# How to run command while rebuilding automatically?

**URL:** <https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459>\
**Category:** support\
**Created:** [July 12, 2022, 3:38am UTC](https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459 "2022-07-12T03:38:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qihuan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/qihuan/32/15374_2.png) [@Qihuan](https://discourse.gohugo.io/u/Qihuan)\
**Post date:** [July 12, 2022, 3:38am UTC](https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459/1 "2022-07-12T03:38:36Z")

</div>

I’m hosting my site on the local server. I wrote a python file to generate cross-reference data into a JSON file that is used to generate pages. Thus I’m looking for a method to run this python script every time before the site is rebuilt when some changes are made.

---

<div class="post-metadata">

**Author:** ![bwintx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bwintx/32/10450_2.png) [@bwintx](https://discourse.gohugo.io/u/bwintx)\
**Post date:** [July 12, 2022, 12:15pm UTC](https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459/2 "2022-07-12T12:15:03Z")

</div>

How about just using a shell script? Psuedo-code:

```bash
#!/bin/bash
python myfile.py
hugo
# add any desired switches to hugo;
# here, it's just bare-bones

```

Save that to, say, `pyhugo.sh`, then run:

```bash
./pyhugo.sh

```

---

<div class="post-metadata">

**Author:** ![Qihuan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/qihuan/32/15374_2.png) [@Qihuan](https://discourse.gohugo.io/u/Qihuan)\
**Post date:** [July 19, 2022, 6:30am UTC](https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459/3 "2022-07-19T06:30:40Z")

</div>

Yes, this is how I deploy my site, but I’d like a way to run this script automatically when it’s rebuilt on the local server.

---

<div class="post-metadata">

**Author:** ![antoinentl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/antoinentl/32/12914_2.png) [@antoinentl](https://discourse.gohugo.io/u/antoinentl)\
**Post date:** [July 19, 2022, 3:10pm UTC](https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459/4 "2022-07-19T15:10:20Z")

</div>

I think the best way is to run the script whenever a file changes, you can try with `while` in your terminal, check an example: [while command in Linux with example - GeeksforGeeks](https://www.geeksforgeeks.org/while-command-in-linux-with-example/)

---

<div class="post-metadata">

**Author:** ![Qihuan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/qihuan/32/15374_2.png) [@Qihuan](https://discourse.gohugo.io/u/Qihuan)\
**Post date:** [July 31, 2022, 6:37am UTC](https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459/5 "2022-07-31T06:37:31Z")

</div>

Hmm… it seems not easy. I was going to ascertain if hugo provides a way to do this work, for example, a hook. For now, I think your method is the best. I would mark it as the solution.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [August 2, 2022, 6:37am UTC](https://discourse.gohugo.io/t/how-to-run-command-while-rebuilding-automatically/39459/6 "2022-08-02T06:37:38Z")

</div>

Closed on August 2, 2022, 6:37am UTC

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