Why apt-get Fails When Automated with Cron – Troubleshooting Guide

aptaptitudecron

I'm trying to use cron to automate my system updates. You can see my crontab, commands, and resulting errors below.

When I run upgrades.sh as root, the script runs fine. When cron runs it, apt-get -y update runs no problem, but aptitude -y safe-upgrade fails. I'm guessing this error: debconf: (This frontend requires a controlling tty.) is because there is a kernel update which in turn updates grub, which requires my explicitly saying it's okay to overwrite /boot/grub/menu.lst. But I don't understand the path errors. And I'd like the updates that don't require my supervision to go through.

I have read through this question and it's as yet unaccepted solution of unattended-upgrades, and I may end up using it, but why can't I use cron? Seems like it should be really simple, and more linuxy.

Crontab

root@daedalus:~/bin# crontab -l
# m h  dom mon dow   command
45 06 * * * ~/bin/upgrades.sh

upgrades.sh

root@daedalus:~/bin# cat upgrades.sh 
#!/bin/bash
/usr/bin/apt-get -y update
/usr/bin/aptitude -y safe-upgrade

Errors

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 37.6MB in 4min 23s (143kB/s)
dpkg: warning: 'ldconfig' not found on PATH.
dpkg: warning: 'start-stop-daemon' not found on PATH.
dpkg: warning: 'update-rc.d' not found on PATH.
dpkg: 3 expected program(s) not found on PATH.
NB: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin.
E: Sub-process /usr/bin/dpkg returned an error code (2)
A package failed to install.  Trying to recover:
dpkg: warning: 'ldconfig' not found on PATH.
dpkg: warning: 'start-stop-daemon' not found on PATH.
dpkg: warning: 'update-rc.d' not found on PATH.
dpkg: 3 expected program(s) not found on PATH.
NB: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin.
Reading package lists...
Building dependency tree...
Reading state information...
Reading extended state information...
Initializing package states...
Writing extended state information...

Best Answer

The messages are telling you that your PATH environment variable is wrong.

Try adding

PATH=/usr/bin:/bin:/usr/sbin:/sbin

to the top of your crontab.

Or you could put the same PATH line as the second line of ~/bin/upgrades.sh. That way your test from the command line and your test from crontab should produce the same results.

Related Question