【问题标题】:Script does not run correctly when executed from cron从 cron 执行时脚本无法正确运行
【发布时间】:2021-06-22 16:17:34
【问题描述】:

我的脚本有问题,当从 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 -l

 [~] # 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 证书未过期,正在退出

【问题讨论】:

  • 将脚本的 stderr 重定向到一个文件。 /share/CACHEDEV1_DATA/Jobs/SSLCertNextCloud.sh 2>/tmp/SSLCertNextCloud.err 这样你就可以看到错误信息了。
  • 我已将其设置为每分钟运行一次,重新启动 cron 我等待它运行几次并检查文件并为空 -rw-r--r-- 1 admin administrators 0 2021 -03-25 22:18 SSLCertNextCloud.err
  • 看起来您正在从 root 的 crontab 运行脚本。那么,如果 sudo 已经以 root 身份运行,为什么还要使用它呢?
  • 当我第一次创建我没有使用 sudo 的脚本时,我添加了 sudo 以试图解决这个问题(在网上某处阅读)
  • 尝试通过env -i your-script 运行脚本。 -i 选项将阻止您的脚本使用您的任何环境变量。这应该模拟在 root 的 contab 中运行的脚本。

标签: linux ssh cron


【解决方案1】:

解决我的问题的方法是设置 docker 的完整路径。 例如:

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

【讨论】:

    猜你喜欢
    • 2013-12-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多