Por que o apt-get falha quando automatizado com o cron?

14

Estou tentando usar o cron para automatizar as atualizações do meu sistema. Você pode ver meus crontab, comandos e erros resultantes abaixo.

Quando executo o upgrades.sh como root, o script é executado corretamente. Quando o cron o executa, apt-get -y update não é executado, mas aptitude -y safe-upgrade falha. Eu estou supondo este erro: debconf: (This frontend requires a controlling tty.) é porque há uma atualização do kernel que por sua vez atualiza o grub, o que requer meu explicitamente dizendo que não há problema em sobrescrever /boot/grub/menu.lst . Mas eu não entendo os erros do caminho. E eu gostaria das atualizações que não exigem minha supervisão.

Eu li através desta esta questão e ainda é inaceitável a solução de unattended-upgrades , e eu posso acabar usando mas porque não posso usar o cron? Parece que deve ser muito simples e mais 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

Erros

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...
    
por djeikyb 29.01.2011 / 01:38

4 respostas

9

As mensagens informam que sua variável de ambiente PATH está errada.

Tente adicionar

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

para o topo do seu crontab .

Ou você pode colocar a mesma linha PATH como a segunda linha de ~/bin/upgrades.sh . Dessa forma, seu teste na linha de comando e seu teste em crontab devem produzir os mesmos resultados.

    
por Mikel 29.01.2011 / 01:50
14

Embora seu problema principal já tenha sido respondido, parece que você está recebendo avisos do debconf porque você está executando o apt-get sem um tty interativo. Para se livrar dessas mensagens, você pode definir essa variável de ambiente:

DEBIAN_FRONTEND=noninteractive
    
por TrinitronX 05.11.2011 / 01:00
1

Existe uma página wiki da comunidade: Razão pela qual o crontab não funciona . Nesse caso, parece que a causa são as variáveis de ambiente limitadas do cron. Você pode executar isso a partir de um script de shell em /etc/cron.daily?

    
por user8290 29.01.2011 / 01:53
0

De CronHowto :

  

Dependendo dos comandos que estão sendo executados,   você pode precisar expandir os usuários root   Variável PATH, colocando o seguinte   linha na parte superior do arquivo crontab:

     

CAMINHO = / usr / sbin: / usr / bin: / sbin: / bin

Mas na verdade, tudo parece estar bem fazendo o mesmo que você .... De onde você tirou esses erros?

CRONTAB:

root@PORTATIL:/var/log$ crontab -l
* * */2 * * /usr/share/myupdate.sh > /var/log/myupdate.log

SCRIPT:

root@PORTATIL:/etc# cat /usr/share/myupdate.sh 
#!/bin/bash
#Testing updates
apt-get update -y
apt-get upgrade -y

LOG:

root@PORTATIL:/etc# cat /var/log/myupdate.log 

Hit http://security.ubuntu.com lucid-security Release.gpg
Hit http://archive.canonical.com lucid Release.gpg
Hit http://archive.canonical.com lucid Release.gpg
Hit http://packages.medibuntu.org lucid Release.gpg
Get:1 http://dl.google.com stable Release.gpg [197B]
Hit http://ppa.launchpad.net lucid Release.gpg
Hit http://ppa.launchpad.net lucid Release.gpg
Hit http://ppa.launchpad.net lucid Release.gpg
Hit http://security.ubuntu.com lucid-security Release
Hit http://badgerports.org lucid Release.gpg
Hit http://archive.canonical.com lucid Release
Hit http://ppa.launchpad.net lucid Release.gpg
Hit http://ppa.launchpad.net maverick Release.gpg
Hit http://ppa.launchpad.net lucid Release.gpg
Hit http://ppa.launchpad.net lucid Release.gpg
Hit http://ppa.launchpad.net lucid Release.gpg
Get:2 http://dl.google.com stable Release [1347B]
Hit http://security.ubuntu.com lucid-security/main Packages
Hit http://ppa.launchpad.net lucid Release
Hit http://ppa.launchpad.net lucid Release
Hit http://packages.medibuntu.org lucid Release
Hit http://download.virtualbox.org lucid Release.gpg
Hit http://archive.canonical.com lucid Release
Hit http://linux.dropbox.com lucid Release.gpg
Get:3 http://dl.google.com stable/main Packages [1110B]
Hit http://security.ubuntu.com lucid-security/restricted Packages
Hit http://security.ubuntu.com lucid-security/main Sources
Hit http://security.ubuntu.com lucid-security/restricted Sources
Hit http://security.ubuntu.com lucid-security/universe Packages
Hit http://security.ubuntu.com lucid-security/universe Sources
Hit http://ppa.launchpad.net lucid Release
Hit http://ppa.launchpad.net lucid Release
Hit http://ppa.launchpad.net maverick Release
Hit http://ppa.launchpad.net lucid Release
Hit http://ppa.launchpad.net lucid Release
Hit http://badgerports.org lucid Release
Hit http://archive.canonical.com lucid/partner Packages
Hit http://security.ubuntu.com lucid-security/multiverse Packages
Hit http://security.ubuntu.com lucid-security/multiverse Sources
Hit http://ppa.launchpad.net lucid Release
Hit http://packages.medibuntu.org lucid/free Packages
Hit http://download.virtualbox.org lucid Release
Hit http://es.archive.ubuntu.com lucid Release.gpg
Hit http://linux.dropbox.com lucid Release
Hit http://archive.canonical.com lucid/partner Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://es.archive.ubuntu.com lucid-updates Release.gpg
Hit http://badgerports.org lucid/main Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://ppa.launchpad.net maverick/main Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://packages.medibuntu.org lucid/non-free Packages
Hit http://linux.dropbox.com lucid/main Packages
Hit http://es.archive.ubuntu.com lucid Release
Hit http://download.virtualbox.org lucid/contrib Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://es.archive.ubuntu.com lucid-updates Release
Hit http://es.archive.ubuntu.com lucid/main Packages
Hit http://es.archive.ubuntu.com lucid/restricted Packages
Hit http://es.archive.ubuntu.com lucid/main Sources
Hit http://es.archive.ubuntu.com lucid/restricted Sources
Hit http://es.archive.ubuntu.com lucid/universe Packages
Hit http://es.archive.ubuntu.com lucid/universe Sources
Hit http://es.archive.ubuntu.com lucid/multiverse Packages
Hit http://es.archive.ubuntu.com lucid/multiverse Sources
Hit http://es.archive.ubuntu.com lucid-updates/main Packages
Hit http://es.archive.ubuntu.com lucid-updates/restricted Packages
Hit http://es.archive.ubuntu.com lucid-updates/main Sources
Hit http://es.archive.ubuntu.com lucid-updates/restricted Sources
Hit http://es.archive.ubuntu.com lucid-updates/universe Packages
Hit http://es.archive.ubuntu.com lucid-updates/universe Sources
Hit http://es.archive.ubuntu.com lucid-updates/multiverse Packages
Hit http://es.archive.ubuntu.com lucid-updates/multiverse Sources
Fetched 2654B in 1s (1628B/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    
por luri 29.01.2011 / 02:23