Busybox

為什麼在busybox sh中執行非同步作業需要/dev/null?

  • October 4, 2022

我很好奇為什麼需要這個特殊的設備來分叉命令並在最小的 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

/ #

從busybox中shell的實現:

/*
* 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也無法訪問),因此如果該設備文件不可訪問,您將收到錯誤消息。

引用自:https://unix.stackexchange.com/questions/719741