cron으로 자동화 할 때 apt-get이 실패하는 이유는 무엇입니까?


15

cron을 사용하여 시스템 업데이트를 자동화하려고합니다. 내 crontab, 명령 및 결과 오류가 아래에 표시됩니다.

upgrades.sh를 루트로 실행하면 스크립트가 제대로 실행됩니다. cron이 실행하면 apt-get -y update아무런 문제가 없지만 aptitude -y safe-upgrade실패합니다. 나는이 오류를 추측하고 있습니다 : debconf: (This frontend requires a controlling tty.)커널 업데이트가 있기 때문에 grub을 업데이트합니다 /boot/grub/menu.lst. 이렇게 하면 덮어 쓸 수 있다고 명시 적으로 말해야합니다 . 그러나 경로 오류를 이해하지 못합니다. 그리고 감독이 필요없는 업데이트를 원합니다.

나는 이 질문unattended-upgrades 을 읽었 으며 아직 받아 들여지지 않은 해결책이며 , 그것을 사용하게 될 수도 있지만 왜 cron을 사용할 수 없습니까? 정말 간단하고 리눅스처럼 보입니다.

크론 탭

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

오류

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...

답변:


10

PATH환경 변수가 잘못 되었다는 메시지가 표시 됩니다.

추가해보십시오

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

당신의 상단에 crontab.

또는 PATH의 두 번째 줄과 같은 줄을 넣을 수 ~/bin/upgrades.sh있습니다. 이렇게하면 명령 줄에서 테스트 한 crontab결과와 동일한 결과가 생성됩니다.


내가 참조. #! / bin / bash 줄이 루트의 일반 경로를로드한다고 생각하여 명령을 실수로 스크립트에 넣었습니다. 분명히 나는 ​​틀렸다. 먼저 사용자의 기본 경로를 결정하는 방법은 무엇이며 어떻게 적용됩니까?
— djeikyb

cron이 왜 사용자의 경로를 따르지 않습니까? crontab 또는 스크립트에 경로를 추가하는 것이 더 낫습니까? 단점이 있습니까?
— djeikyb

보안상의 이유로 이루어져야하지만 성가신 일입니다. 1) 스크립트 PATH=...에 파일을 넣으면 파일 (예 :)을 넣고 스크립트 상단 부근에서 ~/.env사용하는 모든 스크립트에서 파일 을 가져올 수 . ~/.env있습니다. 그런 다음 변경 PATH하면 하나의 파일 만 편집하면됩니다. 2)에 넣으면 crontab모든 cron 스크립트를 편집 할 필요는 없지만 PATH(예 : ~/.bashrc및 crontab) 를 변경하려면 두 곳을 편집해야합니다 . 어느 쪽이 좋을까요.
— 미켈

멋있는. 내 질문은 왜 안전하지 않은 것 같아요. 지금 인터넷 검색 중이지만 아직 아무것도 찾지 못했습니다. 어쨌든, 나는 변화를 일으켰고, 내일 아침 그것이 실행될 때 응답이있는 것으로 표시 할 것입니다.
— djeikyb

나도 알고 있었으면 좋겠다. 이유가 있으면 문서 나 cron소스 코드가 무엇인지 말하지 마십시오. 이론적으로는 한 사용자에서 다른 사용자로 crontab을 복사 할 수 있도록 일관된 환경을 강요했을 수 있습니다.PATH 변경 가능하므로 이유가 될 수 없도록 있습니다.
— Mikel

14

주요 문제는 이미 답변되었지만 대화 형 tty없이 apt-get을 실행하고 있기 때문에 debconf 경고가 표시되는 것처럼 보입니다. 이러한 메시지를 제거하기 위해이 환경 변수를 설정할 수 있습니다.

DEBIAN_FRONTEND=noninteractive


0

에서 CronHowto :

실행중인 명령에 따라 crontab 파일 맨 위에 다음 행을 넣어 루트 사용자 PATH 변수를 확장해야 할 수 있습니다.

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

그러나 실제로 모든 것이 당신과 똑같이 잘되는 것 같습니다 .... 그 오류를 어디에서 가져 왔습니까?

RONTAB :

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

스크립트:

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

로그:

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.

내 오류는 나에게 보낸 크론 작업 로그에서 나옵니다. 나는 당신과 같은 10.04를 실행 중입니다. 이상한 ..
— djeikyb

제한적인 PATH를 설정하는 일부 cron 구성 파일이 있어야합니다 ... 지금은 문제가 없습니다.
— luri
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.