Re: shell script: How to determine if I'm in the background?
On Wed, 28 Aug 2002 01:06:13 -0800 Ken Irving <firstname.lastname@example.org> wrote:
> On Wed, Aug 28, 2002, Carlos Sousa <email@example.com> wrote:
> > I've just spent one hell of a time trying to find the best code for a
> > shell script to determine if it is running in the background. I need this
> > for a cd backup script that will pause for keyboard input if it is run
> > from the command line, but should go ahead if invoked by cron.
> Not sure how universal this is, but I happened to note that variable USER
> is not defined when a process is run from cron, so I use that condition
> to decide to spew user-intended output or not. Perhaps that's too trivial
> a solution?
According to the crontab(5) man page, the variable USER will be set by cron
on BSD systems, so the test would not work there.
Besides, a script set to run in the background from the command line, with
"somescript &", will have USER set.