How to close a process's stdout and stderr without `disown` or gdb?
I am porting some bash scripts to run on busybox. They use disown
, which is not supported in ash, before killing some processes to prevent messages from开发者_如何学运维 that process appearing in the stdout/stderr. I'd like to preserve this functionality. Whether that means closing the stdout/sterr or redirecting to /dev/null
after it's running.
How is it done?
exec [n]>&-
will close FD [n]
.
You cannot modify redirections for file descriptors once a process is running from outside that process. This means that you will have to do the redirection at the time the process is created by the shell. Whether that's redirecting to files or closing the fds like Ignacio showed is up to you.
And I am not sure why you think bash's disown built-in has any effect on file descriptors. Here's what the bash manual says:
disown [-ar] [-h] [jobspec ...] Without options, each jobspec is removed from the table of active jobs. If jobspec is not present, and neither -a nor -r is supplied, the shell's notion of the current job is used. If the -h option is given, each jobspec is not removed from the ta- ble, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If no jobspec is present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a job- spec does not specify a valid job.
But maybe my understanding of what you are trying to achieve is incomplete.
精彩评论