为什么需要 /dev/null 才能在 busybox sh 中运行异步作业?

为什么需要 /dev/null 才能在 busybox sh 中运行异步作业?

我很好奇为什么需要这个特殊的设备来分叉命令并在最小的 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无法访问),如果该设备文件无法访问,您将收到错误。

相关内容