Script does not run correctly when executed from cron

0

i have a problem with a script, when is executed from cron the script does not work as expected but when i execute it from ssh it runs as is supposed.

Setup: QNAP NAS

The script that i'm trying to run:

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

The problem is when the script is executed from cron i get the following output:

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

It seems that it does not get a status, if i connect via SSH to the NAS and run that script, it works as designed

03-23-2021_00:00:01 NextCloudServer is up and running 03-23-2021_00:00:01 Certificate is not expired, exiting

linux
ssh
cron
asked on Stack Overflow Mar 25, 2021 by Banu

1 Answer

0

The fix to my issue was to set the full path to docker. e.g:

docker=/share/CACHEDEV5_DATA/.qpkg/container-station/usr/bin/docker
if [ "$( $docker container inspect -f '{{.State.Status}}' $containerName )" == "running" ]
answered on Stack Overflow Mar 26, 2021 by Banu

User contributions licensed under CC BY-SA 3.0