Script does not run correctly when executed from cron - linux

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

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" ]

Related

Shell script diamond [duplicate]

This question already has an answer here:
Write a shell script to print a diamond figure [closed]
(1 answer)
Closed 5 years ago.
The question is: Using Shell script programming, create a script that asks the user to enter a number and then prints the following shape. The number of rows is double the columns and user input determine the size of the diamond.
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
I've been trying this for days. Any help would bee awesome
#!/bin/bash
w=${1-5}
line() { printf "%$(($1+w))s\n" "$(yes "* " | sed ${1}q | tr -d \\n)"; }
for i in $(seq $w) $(seq $((w-1)) -1 1); do line $i; done
I've wrote smth like this. 'size' variable is actualy 1/4 of whole diamond. But that's cosmetics, and you should be able to fix it:
#!/bin/bash
# reading from stdin
read -p 'Size: ' size
# checking if size is a number
echo $size | grep -q -E '^[0-9]+$' || exit 5
vpos=0
while [[ $vpos -le $(( $size * 2 )) ]] ;
do
hpos=0
while [[ $hpos -le $(( $size * 2 )) ]] ;
do
if [[ $vpos -le $size ]] ;
then
# upper vertical half
if ( [ $hpos -lt $(( $size - $vpos )) ] && [ $hpos -lt $size ] ) || ( [ $hpos -gt $(( $size + $vpos )) ] && [ $hpos -gt $size ] ) ;
then
echo -n ' '
else
echo -n '*'
fi
else
# bottom vertical half
if ( [ $hpos -lt $(( $vpos - $size )) ] && [ $hpos -lt $size ] ) || ( [ $hpos -gt $(( 3 * $size - $vpos )) ] && [ $hpos -gt $size ] ) ;
then
echo -n ' '
else
echo -n '*'
fi
fi
hpos=$((hpos + 1))
done ;
echo ''
vpos=$((vpos + 1))
done
I have written the following code for you, I hope that it helps you:
#!/usr/bin/env bash
#get the first argument passed to your script and assign it to STAR_NUMBER_MAX,
#if it is empty -> default value will be 0
readonly STAR_NUMBER_MAX="${1:-0}"
#check if your STAR_NUMBER_MAX is a number
re='^[0-9]+$'
if ! [[ $STAR_NUMBER_MAX =~ $re ]] ; then
echo "error: your column_size is not a number" >&2; exit 1
fi
#variable that will be used to compute the number of stars to display per line
star_number=0
#variable that will be used in order to know if we have reached the max amount of stars
test_star_max=0
for i in `seq 1 $((2*STAR_NUMBER_MAX +1))`;
do
#print the spaces
for j in `seq $star_number $STAR_NUMBER_MAX`
do
echo -n " "
done
#print the stars
for j in `seq 1 $star_number`;
do
if (( star_number == STAR_NUMBER_MAX )); then
test_star_max=1
fi
echo -n "*"
done
#print the remaining stars
for j in `seq 2 $star_number`;
do
echo -n "*"
done
if (( test_star_max == 0 )); then
star_number=$((star_number + 1));
else
star_number=$((star_number - 1));
fi
echo ""
done
The result of the script is you described.

Removing a cron job via bash script

I'm creating a cron job in a script like this:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
crontab -l > crontask
grep "* * * * * $DIR/somescript" crontask || echo "* * * * * $DIR/somescript" >> crontask
I'd like to build a script that can delete this job, how should I do that?

cronjob not working hourly

If I schedule my cronjob to work with minutes (*, */5 etc) the cronjob is executing. Working with hours, the cronjob doesnt seem to work.
This is my list of cronjobs:
* * * * * /path/to/script/ swiftmailer:spool:send --env=prod >> /path/to/cron.log 2>&1
* * * * * /path/to/script >> /path/to/cron.log 2>&1
0 0 * * * /path/to/script >> /path/to/cron.log 2>&1
*/3 * * * * /path/to/script >> /path/to/cron.log 2>&1
0 0 * * * /path/to/script >> /path/to/cron.log 2>&1
0 0 * * * /path/to/script >> /path/to/cron.log 2>&1
0 0 * * * /path/to/script >> /path/to/cron.log 2>&1
The ones at midnight are not running. If I change
*/3 * * * * /path/to/script >> /path/to/cron.log 2>&1
to
0 * * * * /path/to/script >> /path/to/cron.log 2>&1
that particular script will also stop running.
If I set all the scripts to run every minute, there are no errors in the cron.log.
how is this possible?

Report status crontab lines in a log file Linux RedHat

I would like to know how to extract the state of crontab.
Currently I have these lines, with the execution off:
crontab -l
#* * * * * cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
#* * * * * sleep 15 && curl cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
#* * * * * sleep 30 && curl cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
#* * * * * sleep 45 && curl cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
Desire, based on the state of the crontab for these lines, report ON or OFF in a log file.
In this case I must report "OFF"
Instead:
crontab -l
* * * * * cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
* * * * * sleep 15 && curl cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
* * * * * sleep 30 && curl cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
* * * * * sleep 45 && curl cd $HOME;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
In this case I must report "ON"
They can help me with any ideas?
Thanks
crontab -l | awk '{if($0~/^ *#/){ print "OFF: " $0 } else print "ON : " $0 }' \
> cronStatusReport.$(/bin/date +%Y%m%d.%H%M)
should produce the output you're looking for.
output (from reduced set of your data)
OFF: #* * * * * cd /C/Users/Neil_2;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
OFF: #* * * * * sleep 15 && curl cd /C/Users/Neil_2;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
ON : * * * * * sleep 30 && curl cd /C/Users/Neil_2;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
ON : * * * * * sleep 45 && curl cd /C/Users/Neil_2;sh .bash_profile >/dev/null 2>&1;cd /home/gisdesa/GIS/DESA;. ./loader_DESA.sh
IHTH

how to prevent duplicate entry for creating cron jobs?

here the below code for creating cron jobs successfully but i need to prevent duplicate entry so next time i'm trying to create cron jobs for same file. i need to overwrite existing cron jobs
#!/bin/bash
crontab -l > cd3.new
file1="/home/admin/Desktop/n_com.sh -EI"
file2="/home/admin/Desktop/com.sh -NI"
file3="/home/admin/Desktop/fcom.sh -EF"
file4="/home/admin/Desktop/fcm.sh -NF"
echo "$1 $2 $3 $4 $5 $file1" >> cd3.new
echo "$6 $7 $8 $9 ${10} $file2" >> cd3.new
echo "${11} ${12} ${13} ${14} ${15} $file3" >> cd3.new
echo "${16} ${17} ${18} ${19} ${20} $file4" >> cd3.new
cat cd3.new
crontab cd3.new
I like the idea of setting up directories for each "period" of cron, and using run-parts to execute them. Updating just requires rsync-ing the new cron directories and there's no chance of messing up crontabs.
And then I setup directories and a crontab as such:
# Run hourly, daily, weekly, monthly and yearly tasks for axon
00 0 * * * run-parts /etc/cron/midnight >/dev/null 2>&1
01 * * * * run-parts /etc/cron/hourly >/dev/null 2>&1
02 1 * * * run-parts /etc/cron/daily >/dev/null 2>&1
55 23 * * * run-parts /etc/cron/dailyend >/dev/null 2>&1
45 2 * * 6 run-parts /etc/cron/weekly >/dev/null 2>&1
02 0 1 * * run-parts /etc/cron/monthly >/dev/null 2>&1
03 0 1 1 * run-parts /etc/cron/yearly >/dev/null 2>&1

Resources