O trabalho cron da raiz não está executando o script corretamente. Diretórios PATH ou HOME o problema?

1

Então eu criei o seguinte script para atualizar meu sistema Ubuntu e adicionei um cron job, como root, para executá-lo.

#!/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

Eu recebo a seguinte saída em / 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)

Alguém pode me explicar qual é o problema aqui? Eu verifiquei o / etc / crontab e posso verificar se o PATH contém o que deveria de acordo com a 'Nota' acima.

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

Por que meu script não é executado corretamente?

    
por Everardo Ibarra 24.05.2017 / 08:40

1 resposta

2

Cron parece não usar o ambiente que você tem, tente colocar PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' antes do seu script na lista de cronjobs ou adicione-o ao seu script.

por exemplo,

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

    
por Ziazis 24.05.2017 / 08:50