งาน Cron ไม่ทำงาน

Oct 25 2020

ฉันมีงาน cron ซึ่งควรใช้สัปดาห์ละครั้งupdate, upgradeและautocleanฉลาด แต่มันก็ไม่น่าจะทำงานอย่างน้อยไม่เท่าที่ผมสามารถบอกได้

สิ่งนี้ชัดเจนเนื่องจากการรันsudo apt-get upgrade(สัปดาห์หลังจากเพิ่มงาน cron) แสดงว่ามีแพ็คเกจที่พร้อมให้อัปเกรด

ข้อมูลระบบ

Linux squire 4.15.0-88-generic #88-Ubuntu SMP Tue Feb 11 20:11:34 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

งาน Cron

$ sudo crontab -e

0 12 * * 1 apt-get update && apt-get -y upgrade && apt-get -y autoclean

กระบวนการ

$ ps -aux | grep cron

root       674  0.0  0.0   4640   768 ?        Ss   Oct14   0:01 /bin/sh /snap/nextcloud/23743/bin/nextcloud-cron
root       757  0.0  0.0  31320  1636 ?        Ss   Jul27   0:17 /usr/sbin/cron -f
squire   22697  0.0  0.0  14428  1000 pts/0    R+   15:25   0:00 grep --color=auto cron

บริการ Systemd

● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-07-27 22:15:57 UTC; 2 months 27 days ago
     Docs: man:cron(8)
 Main PID: 757 (cron)
    Tasks: 1 (limit: 2312)
   CGroup: /system.slice/cron.service
           └─757 /usr/sbin/cron -f

Oct 24 14:09:01 squire CRON[15986]: pam_unix(cron:session): session closed for user root
Oct 24 14:17:01 squire CRON[16575]: pam_unix(cron:session): session opened for user root by (uid=0)
Oct 24 14:17:01 squire CRON[16576]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Oct 24 14:17:01 squire CRON[16575]: pam_unix(cron:session): session closed for user root
Oct 24 15:03:01 squire cron[757]: (root) RELOAD (crontabs/root)
Oct 24 15:09:01 squire CRON[21350]: pam_unix(cron:session): session opened for user root by (uid=0)
Oct 24 15:09:01 squire CRON[21350]: pam_unix(cron:session): session closed for user root
Oct 24 15:17:01 squire CRON[22008]: pam_unix(cron:session): session opened for user root by (uid=0)
Oct 24 15:17:01 squire CRON[22009]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Oct 24 15:17:01 squire CRON[22008]: pam_unix(cron:session): session closed for user root

อัพเกรดด้วยตนเอง

$ sudo apt-get update && sudo apt-get upgrade

Hit:1 http://ppa.launchpad.net/certbot/certbot/ubuntu bionic InRelease
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:3 https://deb.torproject.org/torproject.org bionic InRelease
Hit:4 http://us.archive.ubuntu.com/ubuntu bionic InRelease      
Get:5 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Fetched 252 kB in 1s (178 kB/s)                               
Reading package lists... Done
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  base-files linux-generic linux-headers-generic linux-image-generic netplan.io python3-parsedatetime ubuntu-server
The following packages will be upgraded:
  cryptsetup cryptsetup-bin libcryptsetup12 libfreetype6
4 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
Need to get 714 kB of archives.
After this operation, 6,144 B of additional disk space will be used.
Do you want to continue? [Y/n] 

อัปเดตงาน cron สำหรับการทดสอบเอาต์พุต

0 12 * * 1 ( apt-get update && apt-get -y upgrade && apt-get -y autoclean ) >/tmp/apt.cron.log 2>&2

บันทึกจากการทดสอบ

Hit:1 http://ppa.launchpad.net/certbot/certbot/ubuntu bionic InRelease
Hit:2 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:3 https://deb.torproject.org/torproject.org bionic InRelease
Hit:4 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Hit:5 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:6 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
The following packages have been kept back:
  base-files linux-generic linux-headers-generic linux-image-generic
  netplan.io python3-parsedatetime ubuntu-server
The following packages will be upgraded:
  cryptsetup cryptsetup-bin libcryptsetup12 libfreetype6
4 upgraded, 0 newly installed, 0 to remove and 7 not upgraded.
Need to get 714 kB of archives.
After this operation, 6,144 B of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libcryptsetup12 amd64 2:2.0.2-1ubuntu1.2 [134 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 cryptsetup-bin amd64 2:2.0.2-1ubuntu1.2 [93.0 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 cryptsetup amd64 2:2.0.2-1ubuntu1.2 [152 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libfreetype6 amd64 2.8.1-2ubuntu2.1 [335 kB]
Fetched 714 kB in 1s (997 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

คำตอบ

2 roaima Oct 24 2020 at 23:34

ฉันเพิ่งเห็นข้อความแสดงข้อผิดพลาดของคุณจากการเรียกใช้บันทึก

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

ที่ไหนสักแห่งใกล้ด้านบน - เหนือคำจำกัดความงานของคุณ - ควรเป็นหนึ่งหรือสองบรรทัดเหล่านี้

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

คุณไม่จำเป็นต้องใช้SHELLสาย แต่เนื่องจากสิ่งอื่น ๆ ส่วนใหญ่ในระบบที่ใช้ Linux มักจะทำงานด้วยbashฉันขอแนะนำ

หากคุณไม่มีอะไรเป็นของคุณ/usr/localคุณสามารถตัดPATHบรรทัดที่ฉันให้คุณกลับไปที่ไดเร็กทอรีสี่รายการแรกได้ แต่โดยปกติcron's PATHไม่รวมถึงทั้งสองsbinไดเรกทอรีซึ่งเป็นเหตุผลที่มัน erroring ที่มีคำสั่งไม่ถูกพบ