So I created the following script in order to update my Ubuntu system and added a cron job, as root, to execute it.

if ping -q -c 1 -W 1 >/dev/null; then
   /usr/bin/apt-get update -y;
   /usr/bin/apt-get upgrade -y;

I get the following output in /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)

Can someone explain to me what is the problem here? I checked /etc/crontab and can verify PATH contains what it should according to the 'Note' above.


Why won't my script execute correctly?

  • Are you actually using /etc/crontab - or root's personal crontab (e.g. sudo crontab -e)? – steeldriver May 24 '17 at 9:42

Cron seems not to use the environment which you have, try putting PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' before your script in the cronjobs list or add it to your script.


* * * * * PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' myscript.sh

  • That was it. I added the PATH variable to the script and it worked. So when it comes to running a bash script as a cron job for root, environment variables are not set? – Everardo Ibarra May 24 '17 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.