7

How can I run a command at the background in Bash without ampersand (&) at the end of a command?

Update: I want to do it so I can do while inotifywait -e modify app.py; do killall -9 python; python app.py &; done, but that isn't possible because of the syntax.

wb9688
  • 1,477
  • 2
  • 17
  • 30

2 Answers2

7

Use coproc python app.py

From man bash

Coprocesses

A coprocess is a shell command preceded by the coproc reserved word. A coprocess is executed asynchronously in a subshell, as if the command had been terminated with the & control operator, with a two-way pipe estab‐ lished between the executing shell and the coprocess.

The format for a coprocess is:

coproc [NAME] command [redirections]

A.B.
  • 92,125
2

Your syntax is slightly off:

while inotifywait -e modify app.py; do killall -9 python; python app.py &; done

It should be:

while inotifywait -e modify app.py; do killall -9 python; python app.py & done

Unlike C-like languages, you cannot have an empty statement before ; or &.

muru
  • 207,228