因此,我创建了以下脚本来更新我的 Ubuntu 系统,并添加了一个 cron 作业以 root 身份执行它。
#!/bin/bash
if ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
/usr/bin/apt-get update -y;
/usr/bin/apt-get upgrade -y;
exit
fi
我在 /var/mail/root 中得到以下输出
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Hit:2 http://us.archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu xenial-updates/main Sources [250 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe Sources [155 kB]
Get:7 http://us.archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [526 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu xenial-updates/main Translation-en [219 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [454 kB]
Get:10 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [184 kB]
Fetched 2,093 kB in 14s (149 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
The following packages will be upgraded:
libnma-common libnma0 network-manager-gnome
3 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 389 kB of archives.
After this operation, 2,048 B of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main i386 network-manager-gnome i386 1.2.6-0ubuntu0.16.04.3 [310 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main i386 libnma0 i386 1.2.6-0ubuntu0.16.04.3 [73.5 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/main i386 libnma-common all 1.2.6-0ubuntu0.16.04.3 [5,650 B]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 389 kB in 0s (563 kB/s)
dpkg: warning: 'ldconfig' not found in PATH or not executable
dpkg: warning: 'start-stop-daemon' not found in PATH or not executable
dpkg: error: 2 expected programs not found in PATH or not executable
Note: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin
E: Sub-process /usr/bin/dpkg returned an error code (2)
有人能向我解释一下这里的问题是什么吗?我检查了 /etc/crontab,并可以根据上面的“注释”验证 PATH 是否包含应有的内容。
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
为什么我的脚本无法正确执行?
答案1
Cron 似乎不使用您拥有的环境,请尝试将
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
其放在 cronjobs 列表的脚本之前或将其添加到脚本中。
例如
* * * * * PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' myscript.sh