How to Run Hugo Serve in a Script Without Blocking Terminal?
Asked by
!mild objection~
I'm trying to create a script for my Hugo website that will help streamline posting. The issue I'm facing is that when I run `hugo serve` in the script, it takes over the terminal, preventing me from returning to the script. When I hit Ctrl-C to stop `hugo serve`, it also exits my whole script. I want to know if there's a way to run `hugo serve`, check my site in Firefox, and then continue with the script without it getting interrupted. Here's a snippet of my script:
`echo "Move to next step [Y] or exit [q]?"`
`read -r editing_finished`
`if [ $editing_finished = q ]; then`
`exit`
`elif [ $editing_finished = Y ]; then`
`# Step 6 Run hugo serve`
`cd ../../../`
`# Run hugo local server and display in firefox`
`hugo serve & firefox http://localhost:1313/`
`fi`
Any help would be appreciated!
Answers
Instead of changing directories in your script, consider using the full path to your Hugo installation. For example: `/path/to/hugo serve`. This can help avoid issues related to directory changes.
Reply to this answer
If you run `hugo serve & firefox ...` it should only let Hugo output texts while Firefox takes over the input. You can silence Hugo's output with `&> /dev/null`. To stop the server later, you could track its process ID by doing `hugo serve & hugopid="$!"` and then kill that process when you're done.
Reply to this answer
You might want to open a new terminal window for Hugo. It can keep your current terminal free for script execution.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions