[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: exec 0</dev/null vs. nohup?

From: Eric Blake
Subject: Re: exec 0</dev/null vs. nohup?
Date: Thu, 11 Nov 2010 08:21:36 -0700
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20101103 Fedora/1.0-0.33.b2pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.6

On 11/11/2010 04:30 AM, Jay K wrote:
> exec 7<&0 </dev/null 6>&1    <== This is what I suspect is the problem.
> Removing the line didn't work.
> I changed it to:
> exec </dev/null 6>&1

Which lost the original stdin, and that might bite you later in the script.

> Is a shell script writer free to grab any file descriptor other than 0,1,2?

Portably, you can only grab 3-9 (some ksh variants refuse to let you
grab multi-digit fds, even though POSIX implies but not requires that it
should be legal).  And even then, fd 3 and 4 have had historical uses
that shell scripts should not interfere with (although those shells are
museum-ware these days); the Autoconf manual has more details.

Furthermore, there is no guarantee that fds > 2 are inherited into a
shell script (ksh closes them), nor that fds > 2 can be exported into a
child process from a shell script (ksh opens fds as cloexec), so it is
only within the purview of the shell script itself that you can manage
fds > 2.

> How can you be sure your parent process doesn't have some of them in use?

You can't, although you can explicitly close them out of the way before
putting it to your own use (closing an already closed descriptor is
portable - it does not fail).

> Will this just close/reopen them if they are in use?

Yes, redirections behave like dup2(), in that any previously open file
at the same fd is closed.

> What is being execed?

exec with just redirections is documented by POSIX as affecting the
current shell.

And your questions regarding redirections are probably better suited to
a generic shell programming forum rather than coreutils.

Eric Blake   address@hidden    +1-801-349-2682
Libvirt virtualization library

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]