![为什么需要 /dev/null 才能在 busybox sh 中运行异步作业?](https://linux22.com/image/211544/%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%20%2Fdev%2Fnull%20%E6%89%8D%E8%83%BD%E5%9C%A8%20busybox%20sh%20%E4%B8%AD%E8%BF%90%E8%A1%8C%E5%BC%82%E6%AD%A5%E4%BD%9C%E4%B8%9A%EF%BC%9F.png)
我很好奇为什么需要这个特殊的设备来分叉命令并在最小的 Busybox shell 中异步运行它。
BusyBox v1.30.1 (Debian 1:1.30.1-4) built-in shell (ash)
Enter 'help' for a list of built-in commands.
/bin/sh: can't access tty; job control turned off
/ #
/ # echo Hello && sleep 2s && echo World &
/bin/sh: / # can't open '/dev/null': No such file or directory
/ #
/ # mknod /dev/null c 1 3 && chmod 666 /dev/null
/ # echo Hello && sleep 2s && echo World &
/ # Hello
World
/ #
答案1
从实施busybox 中的外壳:
/*
* Fork off a subshell. If we are doing job control, give the subshell its
* own process group. Jp is a job structure that the job is to be added to.
* N is the command that will be evaluated by the child. Both jp and n may
* be NULL. The mode parameter can be one of the following:
* FORK_FG - Fork off a foreground process.
* FORK_BG - Fork off a background process.
* FORK_NOJOB - Like FORK_FG, but don't give the process its own
* process group even if job control is on.
*
* When job control is turned off, background processes have their standard
* input redirected to /dev/null (except for the second and later processes
* in a pipeline).
*
* Called with interrupts off.
*/
注意“输入重定向到 /dev/null”。由于子 shell 的标准输入被重定向/dev/null
(并且将会被重定向,因为作业控制被关闭,这也是因为/dev/tty
无法访问),如果该设备文件无法访问,您将收到错误。