dev*_*ull 36
引用man ps
:
Here are the different values that the s, stat and state output
specifiers (header "STAT" or "S") will display to describe the state of
a process.
...
+ is in the foreground process group
Run Code Online (Sandbox Code Playgroud)
所以你可以执行一个简单的检查:
case $(ps -o stat= -p $$) in
*+*) echo "Running in foreground" ;;
*) echo "Running in background" ;;
esac
Run Code Online (Sandbox Code Playgroud)