小编Art*_*ild的帖子

包含 sudo 的 Bash 脚本 - 不可靠的后台恢复 (bg)

我有以下简单的 bash 脚本(称为test.sh),它显示根目录中的磁盘使用情况。sudo需要列出所有目录(并且不需要我输入sudo密码)。

#!/bin/bash
sudo du -h --max-depth=1 / 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

该目录位于我的路径中,然后我像这样运行脚本(以将输出输出到文本文件):

$ ./test.sh >> ./test.txt
Run Code Online (Sandbox Code Playgroud)

Ctrl现在,如果我用+暂停工作Z,我会得到:

^Z
[1]+  Stopped                 ./test.sh >> ./test.txt
Run Code Online (Sandbox Code Playgroud)

如果我然后在后台恢复bg,我仍然得到:

$ bg
[1]+ ./test.sh >> ./test.txt &

[1]+  Stopped                 ./test.sh >> ./test.txt

$ jobs
[1]+  Stopped                 ./test.sh >> ./test.txt
Run Code Online (Sandbox Code Playgroud)

(额外的尝试bg可能会导致脚本在 2-3 次尝试后实际上在后台恢复,但似乎是零星的......)

但是,如果我使用 继续fg,则脚本将在前台运行:

$ fg
./test.sh >> ./test.txt
Run Code Online (Sandbox Code Playgroud)

结果写入test.txt

3.8M    /root
4.0K    /authentic-theme
4.0K    /srv
72K     /tmp
3.2G …
Run Code Online (Sandbox Code Playgroud)

bash ubuntu sudo job-control

9
推荐指数
0
解决办法
262
查看次数

标签 统计

bash ×1

job-control ×1

sudo ×1

ubuntu ×1