สคริปต์ทำงานไม่ถูกต้องเมื่อดำเนินการจาก cron

ฉันมีปัญหากับสคริปต์ เมื่อดำเนินการจาก cron สคริปต์ไม่ทำงานตามที่คาดไว้ แต่เมื่อฉันเรียกใช้งานจาก ssh สคริปต์จะทำงานตามที่ควรจะเป็น

การติดตั้ง: QNAP NAS

สคริปต์ที่ฉันพยายามเรียกใช้:

#!/bin/sh
log="/share/CACHEDEV1_DATA/Jobs/logs/"
currentDate=$(date +"%m-%d-%Y")
logFile=$log"SSLCertNextCloud_"$currentDate".log"
certSource="/etc/stunnel/"
certFile="backup.cert"
certKey="backup.key"
nextCloudCert="/etc/apache2/ssl/SSLcertificate.crt"
nextCloudKey="/etc/apache2/ssl/SSLprivatekey.key"
nextCloudSSLFolder="/etc/apache2/ssl/"
containerName="NextCloudServer"

if [ "$( sudo docker container inspect -f '{{.State.Status}}' $containerName )" == "running" ]
    then
        echo $(date +"%m-%d-%Y_%T") $containerName "is up and running" >> $logFile
    else
        echo $(date +"%m-%d-%Y_%T") $containerName "is not running, trying to start" >> $logFile
        docker start $containerName
        sleep 20
        status=$( sudo docker container inspect -f '{{.State.Status}}' $containerName )
        echo $(date +"%m-%d-%Y_%T") "Status:"$status >> $logFile
        if [ $status == "running" ]
            then 
                echo $(date +"%m-%d-%Y_%T") $containerName "started successfully" >> $logFile
            else 
                echo $(date +"%m-%d-%Y_%T") "Could not start " $containerName >> $logFile
        fi
fi 

if [ ! -f $logFile ]
then
    touch $logFile
fi

if true | openssl s_client -connect myserver 2>/dev/null | \
  openssl x509 -noout -checkend 0; 
  then
    echo $(date +"%m-%d-%Y_%T") "Certificate is not expired, exiting" >> $logFile
  else
    echo $(date +"%m-%d-%Y_%T") "Certificate is expired, copying new certificate" >> $logFile
    docker cp $certSource$certFile $containerName:$nextCloudCert
    docker cp $certSource$certKey $containerName:$nextCloudKey
    echo $(date +"%m-%d-%Y_%T") "Certificates where copied, restarting server" >> $logFile
    docker restart $containerName
fi 

Crontab -ล

 [~] # crontab -l
    # m h dom m dow cmd
    29 9,21 * * * /sbin/notify_update --nc 1>/dev/null 2>&1
    0-59/20 3 * * * /sbin/adjust_time
    0 1 * * * /etc/init.d/flush_memory.sh >/dev/null 2>&1
    0 3 * * * /sbin/clean_reset_pwd
    0-59/15 * * * * /etc/init.d/nss2_dusg.sh
    30 7 * * * /sbin/clean_upload_file
    0-59/10 * * * * /etc/init.d/storage_usage.sh
    30 3 * * * /sbin/notice_log_tool -v -R
    */10 * * * * /sbin/config_cache_util 0
    0 4,16 * * * /sbin/hwclock -s
    0 0 * * 1 /sbin/hal_event --pd_self_test dev_id=0x00000002,action=1
    0 0 * * 1 /sbin/hal_event --pd_self_test dev_id=0x00000001,action=1
    00 03 * * 1 sh /share/CACHEDEV1_DATA/.qpkg/MalwareRemover/MalwareRemover.sh scan;#_QSC_:MalwareRemover:malware_remover_schedule:None:w::
    0 2 * * 0 /usr/local/medialibrary/bin/mymediadbcmd checkRepairDB  >/dev/null 2>&1
    30 6 * * 1 /sbin/storage_util --disk_sequential_read_speed_test 1>/dev/null 2>&1
    0 7 * * * /sbin/qfstrim
    22 6 * * * /share/CACHEDEV1_DATA/.qpkg/HybridBackup/rr2/scripts/insight/insight.sh -runall >/dev/null 2>&1
    10 15 * * * /usr/bin/power_clean -c 2>/dev/null
    41 * * * * /sbin/qddns_check 2>/dev/null
    0 3 * * 0 /etc/init.d/idmap.sh dump
    24 4 * * * /sbin/auto_update
    00 01 * * * sh /share/CACHEDEV1_DATA/.qpkg/MalwareRemover/Upgrade.sh;#_QSC_:MalwareRemover:malware_remover_upgrade:None:d::
    **0 1 * * * /share/CACHEDEV1_DATA/Jobs/SSLCertNextCloud.sh**
    0 8 * * * /share/CACHEDEV1_DATA/Jobs/DeleteLogs.sh
    0 2 * * 5 /etc/init.d/poweroff
    0 7 * * 5 /etc/init.d/startup
    * * * * * /var/cache/netmgr/lock_timer.sh
    50 7 * * * /sbin/qpkg_cli --check_license 0 > /dev/null 2>/dev/null
    0 4 * * * /etc/init.d/wsd.sh restart
    0 3 * * * /sbin/vs_refresh
    4 3 * * 3 /etc/init.d/backup_conf.sh
    0 0 * * * /etc/init.d/antivirus.sh archive_log
    0 12 * * * /mnt/ext/opt/LicenseCenter/bin/qlicense_tool local_check
    0 0 * * * /usr/local/sbin/qsh nc.archive >/dev/null 2>&1
    51 09 * * * /mnt/ext/opt/QcloudSSLCertificate/bin/ssl_agent_cli
    35 7 * * * /sbin/qsyncsrv_util -c  > /dev/null 2>/dev/null
    0 0 * * * /sbin/qsyncsrv_tool --fix  > /dev/null 2>/dev/null

ปัญหาคือเมื่อสคริปต์ถูกเรียกใช้งานจาก cron ฉันได้รับผลลัพธ์ต่อไปนี้:

03-25-2021_21:00:00 NextCloudServer is not running, trying to start
03-25-2021_21:00:20 Status:
03-25-2021_21:00:20 Could not start  NextCloudServer
03-25-2021_21:00:20 Certificate is not expired, exiting

ดูเหมือนว่าจะไม่ได้รับสถานะ ถ้าฉันเชื่อมต่อผ่าน SSH ไปยัง NAS และเรียกใช้สคริปต์นั้น มันก็ทำงานได้ตามที่ออกแบบไว้

03-23-2021_00:00:01 NextCloudServer เปิดใช้งานแล้ว 03-23-2021_00:00:01 ใบรับรองยังไม่หมดอายุ กำลังออก


person Banu    schedule 25.03.2021    source แหล่งที่มา
comment
เปลี่ยนเส้นทาง stderr ของสคริปต์ไปยังไฟล์ /share/CACHEDEV1_DATA/Jobs/SSLCertNextCloud.sh 2>/tmp/SSLCertNextCloud.err เพื่อให้คุณสามารถเห็นข้อความแสดงข้อผิดพลาด   -  person Barmar    schedule 25.03.2021
comment
ฉันตั้งค่าให้ทำงานทุกนาที รีสตาร์ท cron ฉันรอให้มันทำงานสองสามครั้งและตรวจสอบไฟล์และว่างเปล่า -rw-r--r-- ผู้ดูแลระบบ 1 คน 0 2021-03-25 22:18 SSLCertNextCloud .ผิดพลาด   -  person Banu    schedule 25.03.2021
comment
ดูเหมือนว่าคุณกำลังเรียกใช้สคริปต์จาก crontab ของรูท เหตุใดคุณจึงต้องใช้ sudo ถ้ามันทำงานเป็นรูทอยู่แล้ว?   -  person Barmar    schedule 25.03.2021
comment
เมื่อฉันสร้างสคริปต์ครั้งแรกฉันไม่ได้ใช้ sudo ฉันเพิ่ม sudo เพื่อพยายามแก้ไขปัญหา (อ่านได้จากที่ใดที่หนึ่งบนเว็บ)   -  person Banu    schedule 25.03.2021
comment
ลองเรียกใช้สคริปต์ผ่าน env -i your-script ตัวเลือก -i จะป้องกันไม่ให้สคริปต์ของคุณใช้ตัวแปรสภาพแวดล้อมใดๆ ของคุณ สิ่งนี้ควรจำลองสคริปต์ที่ทำงานใน contab ของรูท   -  person Cole Tierney    schedule 26.03.2021
comment
ฉันเพิ่ม env -i สคริปต์ของฉันและมันก็ใช้งานไม่ได้ ฉันได้รับข้อผิดพลาดในไฟล์ err:/share/CACHEDEV1_DATA/Jobs/SSLCertNextCloud.sh: บรรทัด 13: docker: ไม่พบคำสั่ง /share/CACHEDEV1_DATA/Jobs/SSLCertNextCloud sh: บรรทัด 18: นักเทียบท่า: ไม่พบคำสั่ง /share/CACHEDEV1_DATA/Jobs/SSLCertNextCloud.sh: บรรทัด 20: นักเทียบท่า: ไม่พบคำสั่ง   -  person Banu    schedule 26.03.2021
comment
ฉันแก้ไขปัญหาโดยเพิ่มเส้นทางแบบเต็มไปยังนักเทียบท่า   -  person Banu    schedule 26.03.2021


คำตอบ (1)


การแก้ไขปัญหาของฉันคือการตั้งค่าเส้นทางแบบเต็มไปยังนักเทียบท่า เช่น:

docker=/share/CACHEDEV5_DATA/.qpkg/container-station/usr/bin/docker
if [ "$( $docker container inspect -f '{{.State.Status}}' $containerName )" == "running" ]
person Banu    schedule 26.03.2021