Re: how to easily determine shell script is running in background from within script



2009-02-19, 10:41(-06), User102:
[...]
I think that this may be on the right track, but help me to better
understand everything if you will.

(1)
Perl "false": anything undefined, or evaluates to null, the number 0 or
the string "0"

or the empty string.

(2)
Perl "true": any-/every-thing else

(3)
shell "false": non-zero, most often 1

(4)
shell "true": zero

That's not really the shell. That's the exit status of commands.
What is passed to "return" in main() or what is passed to
exit(3), and that is retrieved using the waipid() system call.
By convention, there, 0 means true or success, while any else
means false and the actual value can help to differenciate the
kind of failures. Many control structures of the shell are based
on this: &&, ||, if, while, until...

But anywhere else in the shell (as in arithmetic expansion) or
the standard utilities (expr, awk, bc...) 0 means false and
anything else true.

(5)
( perl -e 'print "hello\n";exit' && echo "yes: in
background" ) &
hello
yes: in background
[1] 9795
[1] Done ( perl -e 'print "hello\n";exit' &&
echo "yes: in background" )

perl's exit, without arguments, defaults to exit(0).

[...]
(7)
perl -MPOSIX -e 'open A, "</dev/tty" or die($!);printf "
%s\n",tcgetpgrp(fileno(A));printf " %s\n",getpgrp();exit
(tcgetpgrp(fileno(A)) == getpgrp());' ; echo $? > perl.rc;cat perl.rc
10950
10950
1

tcgetpgrp(fileno(A)) == getpgrp() expands to 1 if the foreground
process group of the terminal is the process' process group,
that is if the process is in foreground. As that's passed to
exit, that means that the exit status will be successful (0) if
the process is in background.

[...]
P.S. I didn't realize that Perl could invoke native system library
functions directly.

Not directly. There are a number of perl functions in the POSIX
perl module that map to POSIX functions/system calls.

P.S.S. Process Group ID information is somewhat useless as there may be
multiple to many PGID's associated with any given userid or task:


ps -j
PID PGID SID TTY TIME CMD
9026 9026 9026 pts/4 0:00 tcsh
10992 10992 9026 pts/4 0:00 ps

But a process is in a single process group and you asked whether
a given process was in foreground or not. Generally, only shells
spawn process groups. login applications spawn sessions, daemons
too, but to detach themselves from a terminal.

ps -efj | grep <myuserid>|awk '{print $2" "$3" "$4" "$5}'|sort -k3,3

ps -u <mysuerid> -o pid,ppid,pgid,sid

--
Stéphane
.



Relevant Pages

  • Re: How to make a batch file to start program and close the oldwindow?
    ... 'nohup' takes care of an assortment of things that need to keep working. ... The 'nohup' redirects output to a file and starts a new process group, the '&' lets the shell continue instead of waiting, and the 'exit' should close the shell window. ...
    (Fedora)
  • RE: 0; and 1;
    ... I have executed a Perl script from csh[c shell] and saw that exit ... Exit 100; at the end is captured by shell. ... you are not the intended recipient, please notify the sender at Wipro or Mailadmin@xxxxxxxxx immediately ...
    (perl.beginners)
  • Re: How to make a batch file to start program and close the oldwindow?
    ... The problem is that you are in the same process group and get killed by ... a signal when your parent shell exits. ... nohup command & ... the '&' lets the shell continue instead of waiting, and the 'exit' ...
    (Fedora)
  • Re: How to make a batch file to start program and close the oldwindow?
    ... 'nohup' takes care of an assortment of things that need to keep working. ... The 'nohup' redirects output to a file and starts a new process group, the '&' lets the shell continue instead of waiting, and the 'exit' should close the shell window. ...
    (Fedora)
  • Re: Trap errors inside MSDOS batch file
    ... has nothing whatever to do with Perl). ... on the exit status? ... Do you really need to use MSDOS shell ...
    (comp.lang.perl.misc)