Running commands in Zsh without having to wait
March 15, 2021 ‐ 2 min read
In Zsh, and for Bash this works the same, you can send commands to the background. This unblocks your terminal for other commands you'd like to run. For example in the case of a slow command or a process that keeps running like a development server.
You can send a command to the background by appending an &
at the end of the command. So like this for example.
$ hugo server &
[1] 96545
This sends the hugo development server to the background and shows the process ID. If you'd like you can use this process ID to lookup information about the process.
$ ps -p 96545
PID TTY TIME CMD
96545 pts/8 00:00:01 hugo
Or more practically kill the process.
$ kill 96545
[1] + done hugo server
To bring back the process to the foreground you just use the fg
command. This brings up the last process you ran in the background.
You might have noticed that the background process does write to the terminal while being in the background. Which has a useful purpose obviously. This way you get possible warnings and other information about the running process that might require your attention.
However, if you would rather have the process running quietly you can redirecting the output of the command to /dev/null
. As is shown in the example below. Just be aware that you might miss useful information about the running process when you do this.
$ hugo server > /dev/null &