Have a *nix sh script put itself into the background
Posted by Alec The Geek on 24 February 2011
In the Unix Bourne style shell languages (e.g. sh, bash, ksh etc) you can put a long running command (or another script) into the background so that control returns to the user. The original idea is that you can perform other work whilst the background process continues. This is achieved by adding a
&, for example
$ myVeryLongRunningCommand and options >/tmp/myOutput &.
It would be nice to be able to have scripts place themselves in the background, when appropriate, without the user having to worry about and I assumed there would be a “shell’ish” way to do that — however I couldn’t find anything so I knocked something up in a hurry. I put it here for future generations and in the hope that improvements will be suggested.
#! /bin/sh if [[ "$_myBackGround" != "_backGroundFlag" ]]; then nohup /bin/sh -c "_myBackGround=_backGroundFlag; export _myBackGround;$0 $*" > /tmp/$0.$$.stdout 2> /tmp/$0.$$.stderr & echo $0 passed to background with arguments "$*". NB stderr is /tmp/$0.$$.stderr and stdout is /tmp/$0.$$.stdout exit fi # Your script goes here e.g. echo hello: My arguments are $0 $* echo Hello -- this is an error >&2
2 Responses to “Have a *nix sh script put itself into the background”
Sorry, the comment form is closed at this time.