Shell Script What can check if this was set when called

I wrote a script that relies on other server responses (uses Wget to retrieve data), and I want to always run in the background without a doubt. I know that one solution is to simply write a shell script that will call my script with & added, but I want to avoid this mess.

Is there a way for the bash (or zsh) script to determine if it was called using. /foo.sh&, and if not, exit and restart it yourself?

Thanks everyone!

+3
source share
5 answers

I don’t know how to do this, but you can set the variable in the parent script and check it in child:

if [[ -z "$_BACKGROUNDED" ]] ; then
    _BACKGROUNDED=1 exec $0 $@ & exit
fi
# Put code here

bash, zsh.

+1

( ) , , . , , zsh, .

Linux (, , ) STAT ps +, . , , script main :

case $(ps -o stat= -p $$) in
  *+*) main "$@" &;;
  *) main "$@";;
esac

main "$@" &. Unix .

, . - script - - , your_script; my_postprocessing your_script && my_postprocessing, script . , script, script .

, script , , - script, , , . , script daemonizing wrapper script script. , , , , .

+7

"tty" " tty", , (,/dev/pts/1), . .

+1

, ( ) script. .

0

, (). $$ ( PID ) "jobs -l". PID (), $$ "jobs -l", , script .

-2

Source: https://habr.com/ru/post/1762467/


All Articles