Report status crontab lines in a log file Linux RedHat - linux

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

Related

Script does not run correctly when executed from cron

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

Unable to have aws userdata echo onto /etc/crontab

Long story short I am trying to have my aws userdata create a cronjob on launch. I am unable to echo into the /etc/crontab location. Here is a snippet of my code.
echo '# description of cronjob being addeded'
echo '0 16 * * 2,4,6 root some commands' | sudo tee -a /etc/crontab > /dev/null
echo ' ' | sudo tee -a /etc/crontab > /dev/null
Here is the Answer incase someone else needs to know.
"sudo sh -c 'echo \"# description of cronjob being addeded\" >> /etc/crontab' \n",
"sudo sh -c 'echo \"0 16 * * 2,4,6 root some commands\" >> /etc/crontab' \n",
"sudo sh -c 'echo \" \" >> /etc/crontab' \n",

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?

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