PDA

View Full Version : Error running apt-get from crontab



leslie4
January 21st, 2015, 04:34 PM
When I run "apt-get -y upgrade" as root at the command prompt, it works fine, but when I run the same command from root's crontab it fails and leaves this message in the apt-get history:
Error: Sub-process /usr/bin/dpkg returned an error code (2) Any ideas why one works but not the other? I'm running 14.04.1 LTS (3.13.0-32-generic #57-Ubuntu), if that helps. Thanks in advance to all who respond.

TheFu
January 21st, 2015, 10:57 PM
cron runs without ANY environment - ok, just an extremely minimal env. You must setup anything desired/needed before calling programs. I don't know what those are for apt-get. Sorry. There is an automatic security updates option, if that is what you want somewhere.

I much prefer to manually perform updates across the 30 boxes here and log the output, so if anything strange happens, the log is there to figure it out. Been doing this weekly for about 6 yrs. Can't recall having any major issues, ever.

leslie4
January 28th, 2015, 05:46 PM
cron runs without ANY environment - ok, just an extremely minimal env. You must setup anything desired/needed before calling programs. That was the problem. I added a path to my crontab entry, and it worked fine:
0 4 * * * PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' /usr/bin/apt-get -y upgrade Thanks for the help.

ian-weisser
January 29th, 2015, 01:13 AM
FYI, apt already includes a daily 'update' in /etc/cron.daily/apt
It's trivial to add 'upgrade' using /etc/apt/apt.conf.d/50unattended-upgrades instead of a separate cron job.

That file tracks by repository - simply uncomment (or add) the repositories you want to auto-upgrade.
That's how Ubuntu offers auto-updates for Security, for example.

TheFu
January 29th, 2015, 02:23 AM
That was the problem. I added a path to my crontab entry, and it worked fine:
0 4 * * * PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' /usr/bin/apt-get -y upgrade Thanks for the help.

Or you could have just followed recommended practice and used the full path to the exec in the command (or script). /usr/bin/apt-get