0

I have a script:

runner.sh

#!/bin/bash

DIR="$(cd "$(dirname "$0")" && pwd)"

node --trace-warnings "$DIR/../engine/runner/Runner.js" "$@"

that I call that way:

./runner.sh --program --name=simple &

With or without & at the end of line. I'd like to detect that in runner.sh so I can run node in background or regular way.

How can we detect (in bash script) that script is running in background?

ElSajko
  • 1,612
  • 3
  • 17
  • 37
  • 2
    Being _in the background_ and being _a daemon_ are two different things. – Charles Duffy Apr 17 '21 at 16:46
  • A daemon, for example, won't have a TTY handle. That's easy to detect. But it's not true for just regular background processes. – Charles Duffy Apr 17 '21 at 16:47
  • Ah so I should ask instead how to detect that script is called in background – ElSajko Apr 17 '21 at 16:48
  • The _right way_ to run your program _as a daemon_ would be more like `./runner.sh --program --name=simple /dev/null 2>&1 & disown -h $!` -- that way the program will keep running even if the window you started it from is closed. But yes, if you just want to detect being started in the background, that's different (and a little trickier). – Charles Duffy Apr 17 '21 at 16:48
  • 1
    In the linked duplicate, see in particular the answer by Gilles. – Charles Duffy Apr 17 '21 at 16:52

0 Answers0