6

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,417
  • 1
  • 17
  • 29
  • Please [edit] and explain what you are trying to do. The `&` _is_ the way to run a command in the background. Why don't you want to use it? Do you want to send an already launched command to the background? – terdon Oct 25 '15 at 16:41
  • @Jos my `&` key isn't broken and @terdon I updated the question – wb9688 Oct 26 '15 at 06:17

2 Answers2

6

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.
  • 89,123
  • 21
  • 245
  • 323
Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
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
  • 193,181
  • 53
  • 473
  • 722