Re: how to easily determine shell script is running in background from within script
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxxxxxxx>
- Date: Fri, 20 Feb 2009 22:10:17 +0000 (UTC)
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
.
- References:
- how to easily determine shell script is running in background from within script
- From: User102
- Re: how to easily determine shell script is running in background from within script
- From: Stephane CHAZELAS
- Re: how to easily determine shell script is running in background from within script
- From: User102
- Re: how to easily determine shell script is running in background from within script
- From: Stephane CHAZELAS
- Re: how to easily determine shell script is running in background from within script
- From: User102
- how to easily determine shell script is running in background from within script
- Prev by Date: Re: Backslash interpretation
- Next by Date: link
- Previous by thread: Re: how to easily determine shell script is running in background from within script
- Next by thread: Should integer variables be quoted?
- Index(es):
Relevant Pages
|