crontab not working despite trying different fixes from Stackoverflow - linux

I'm trying to run some cron jobs as root but it seems that my crontab it's not working. After reading a lot of threads here in SO I've changed these things:
I've removed the sudos inside the crontab.
I've added the full path of all the commands inside the crontab.
I've restarted the cron job each time I've changed it.
But it's not working yet.
This is my current crontab (I call it from my sudo user with "sudo crontab -e" to run it as root):
0 4 * * * /usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log
5 4 * * * /usr/bin/mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"` --ssl --sslPEMKeyFile=/etc/ssl/mongo.pem --username <mymongoadminusername> --password <mymongoadminpassword> --authenticationDatabase=admin > /var/log/log2.log
30 4 * * * /opt/letsencrypt/certbot-auto renew > /var/log/log3.log
35 4 * * * /etc/init.d/nginx reload > /var/log/log4.log
40 4 * * * /bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log
What I'm trying to do with these 5 commands is:
At 04:00. Remove all backups that are more than 7 days old.
At 04:04. Backup all my mongodb databases.
At 04:30. Try to renew my SSL certificate.
At 04:35. Reload Nginx service.
At 04:40. Update my mongodb.pem file with the new certificate.
It's important to say that each of these commands running from the shell one by one with sudo are working properly.
But from the cron the results are:
I don't know if this command is working. The log file (log1.log) is empty.
It's not working. No backup have been created. The log file (log2.log) has not been created.
It seems that it's working. The log file (log3.log) shows the standard output when you run this command correctly.
It seems that it's working. The log file (log4.log) shows the standard output when you run this command correctly.
It seems that it's not working. The mongo.pem file has the correct updated date but the file is empty. The files fullchain1.pem and privkey1.pem have the correct content, so it seems a problem with "/usr/bin/tee /etc/ssl/mongo.pem". The log file (log5.log) has not been created.
Finally the cron.log shows this:
Nov 11 04:00:01 myservername CRON[31286]: (root) CMD (/usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log)
Nov 11 04:05:01 myservername CRON[31297]: (root) CMD (/usr/bin/mongodump --out /var/backups/mongobackups/`date +")
Nov 11 04:05:01 myservername CRON[31296]: (CRON) info (No MTA installed, discarding output)
Nov 11 04:07:01 myservername CRON[31306]: (root) CMD ( test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond)
Nov 11 04:17:01 myservername CRON[31325]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Nov 11 04:30:01 myservername CRON[31353]: (root) CMD (/opt/letsencrypt/certbot-auto renew > /var/log/log3.log)
Nov 11 04:30:04 myservername CRON[31352]: (CRON) info (No MTA installed, discarding output)
Nov 11 04:35:01 myservername CRON[31393]: (root) CMD (/etc/init.d/nginx reload > /var/log/log4.log)
Nov 11 04:40:01 myservername CRON[31420]: (root) CMD (/bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log)
Nov 11 04:40:01 myservername CRON[31419]: (CRON) info (No MTA installed, discarding output)
If I'm not wrong the error "No MTA installed, discarding output" is not a problem because it's related to sending the output by email, am I right?
So anyone could tell me what is wrong with my crontab?
Thank you very much!

I don't know what is the problem with my crontab but this is working if I put all these commands inside a script and call this script inside the crontab.
The script (myscript.sh):
#!/bin/bash
/usr/bin/find /var/backups/mongobackups/ -mtime +7 -exec rm -rf {} \; > /var/log/log1.log
/usr/bin/mongodump --out /var/backups/mongobackups/`date +"%m-%d-%y"` --ssl --sslPEMKeyFile=/etc/ssl/mongo.pem --username <mymongoadminusername> --password <mymongoadminpassword> --authenticationDatabase=admin > /var/log/log2.log
/opt/letsencrypt/certbot-auto renew > /var/log/log3.log
/etc/init.d/nginx reload > /var/log/log4.log
/bin/cat /etc/letsencrypt/archive/<mydomanin>/{fullchain1.pem,privkey1.pem} | /usr/bin/tee /etc/ssl/mongo.pem > /var/log/log5.log
The crontab:
00 03 * * * <pathtomyscript>/myscript.sh
This is working now.

Related

deleting cronjob that shouldn't be there

I installed froxlor a while back and uninstalled it again, because it didn't fit my need. the server I'm running is a debian web server. after inspecting the system log file using
grep CRON /var/log/syslog
I noticed that there are still some froxlor things going on.
most noticable are log entries like:
Jun 25 10:55:01 v220200220072109810 CRON[5633]: (root) CMD (/usr/bin/nice -n 5 /usr/bin/php -q /var/www/froxlor/scripts/froxlor_master_cronjob.php --tasks 1> /dev/null)
Jun 25 11:00:01 v220200220072109810 CRON[5727]: (root) CMD (/usr/bin/nice -n 5 /usr/bin/php -q /var/www/froxlor/scripts/froxlor_master_cronjob.php --tasks 1> /dev/null)
however, when inspecting the crontab for the root user, I don't have any active crontabs. Any ideas on how to fix this issue?

Mutt not working inside Crontab but works out of it

After research the whole Internet can't find a solution to it.
I have a script which is working perfectly when I execute it from terminal.
#!/bin/bash
zip -r -j Tato.zip Csv
rm -r Csv/*.*
echo "blahblahblah" | mutt -s "Test" email#gmail.com -a Tato.zip
rm *.zip
but actually it does not work when I put it inside a crontab.
55 15 * * 7 /home/pi/Script.sh
wanted it to be executed on Sundays at 15.55.
And this is the output that /var/log/syslog tells me
Nov 1 15:55:01 raspberrypi sSMTP[3939]: Unable to locate mail
Nov 1 15:55:01 raspberrypi sSMTP[3939]: Cannot open mail:25
Nov 1 15:55:01 raspberrypi /USR/SBIN/CRON[3936]: (pi) MAIL (mailed 1 byte of output; but got status 0x0001, #012)
Don't know what to do anymore.
All help will be appreciated.

Shell script runs manually but not executed through cron job

I am using a shell script to delete log files in my Linux machine. And I have used crontab to execute this job periodically. I can execute this script manually and see it is working fine. But the script is not triggered through cron job.
Shell script:
[root#testVM-dev log]# vim /path_to_dir/log_cleanup.sh
#!/bin/bash
now=$(date)
echo "Cron Job to delete log files start...# $now" | tee -a /path_to_dir/log/cronjob_log.log
/usr/bin/find /path_to_dir/log/localhost_access_log.* -type f -mtime +5 -print -delete | tee -a /path_to_dir/log/cronjob_log.log
echo "Delete files completed!" | tee -a /path_to_dir/log/cronjob_log.log
crontab entry: (Runs every minute)
[root#testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
Also I have checked in cron logs that the job is executed every minute. But I couldn't see any logs in the targeted log file "cronjob_log.log"
cron logs:
vim /var/log/cron
Jul 16 03:56:01 rstestVM-dev crond[19131]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:57:01 rstestVM-dev crond[19150]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:58:01 rstestVM-dev crond[19168]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:59:01 rstestVM-dev crond[19188]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Can someone help me on what is going wrong here?
Besides manually specifying value of PATH, try specifying the path to tee and date.
/usr/bin/date, /usr/bin/tee
I have found the solution. That I have removed the user from crontab entry & it worked.
Original crontab entry:
[root#testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
After modification:
[root#testVM-dev log]# crontab -l
*/1 * * * * /path_to_dir/log_cleanup.sh
Now I got confused why the user entry in cron job didn't work? Can someone explain?

crontab job skipping to run randomly

I have a crontab job setup to run every 5 minutes. it runs fine without any issues. But once a while a run fails.
e.g
its run fine at 5th, 10th , 15th minute of an hour but the 20th minute will not run but runs fine again at 25th minute.
next time may be the 55th minute fails. ( random times on random servers)
i checked in /var/log/cron and there are entries for all the executed jobs but for the missed job there is not even an entry.
eg. in the below /var/log/cron job rmlogs ran fine at 21.35 missed running at 21.40 but runs again at 21.45.
Jun 12 21:35:01 [serverxxxx] CROND[4167]: (vfhttpd) CMD (/opt/vmware/vfabric-web-server/http-proxy/tools/rmlogs -t 5 >> /opt/vmware/vfabric-web-server/http-proxy/logs/rmlogs.log 2>&1 #PUPPET)
Jun 12 21:40:01 [serverxxxx] CROND[4201]: (root) CMD (/usr/lib64/sa/sa1 -S DISK -F 1 1)
Jun 12 21:41:02 [serverxxxx] CROND[4213]: (root) CMD (/usr/local/bin/monitor_mcollective >/dev/null 2>&1 #PUPPET)
Jun 12 21:45:01 [serverxxxx] CROND[4225]: (riak) CMD (/opt/riaktools/riak-create-logs -p /var/log/riak > /dev/null 2>&1 #PUPPET)
Jun 12 21:45:01 [serverxxxx] CROND[4227]: (vfhttpd) CMD (/opt/vmware/vfabric-web-server/http-proxy/tools/rmlogs -t 5 >> /opt/vmware/vfabric-web-server/http-proxy/logs/rmlogs.log 2>&1 #PUPPET)
Jun 12 21:50:01 [serverxxxx] CROND[4665]: (root) CMD (/usr/lib64/sa/sa1 -S DISK -F 1 1)
Jun 12 21:50:01 [serverxxxx] CROND[4666]: (vfhttpd) CMD (/opt/vmware/vfabric-web-server/http-proxy/tools/rmlogs -t 5 >> /opt/vmware/vfabric-web-server/http-proxy/logs/rmlogs.log 2>&1 #PUPPET)
Jun 12 21:52:01 [serverxxxx] CROND[4700]: (root) CMD (/usr/local/bin/refresh-mcollective-metadata #PUPPET)
You have a reason to doubt, if your cron job has been started every 5 minutes indeed.
To exclude the doubt you can extend the crontab line, containing your cron job with a prefix:
date >>/tmp/base_name_of_your_cronjob.log;
and check this additional log file.
If this log file contains entries every 5 minutes without a gap, then you should investigate, if /var/cron/log gets a new record for every start of the cron job, including cases, if the cron job crashes.
Additional check point is to make sure that the cron daemon has run permanently and was not restarted. You can do it by checking its process by e.g.
ps -ef|grep crond

Unable to find a cron file

I have to migrate a server to a new cloud provider. In doing so I need to copy all the LAMP code to new virtual machine. I did all that. However, on the old server one cron job runs which does something. I'm unable to find the cron file and migrate it to new server. The output of:
grep CRON /var/log/syslog
is
Nov 5 19:00:01 frroleapi CRON[22975]: (root) CMD (php /var/www/api/scheduler.php)
Nov 5 19:05:01 frroleapi CRON[23189]: (root) CMD (php /var/www/api/scheduler3.php)
Nov 5 19:05:01 frroleapi CRON[23190]: (root) CMD (php /var/www/api/scheduler.php)
Nov 5 19:09:01 frroleapi CRON[23382]: (root) CMD ( [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) -delete)
The last cronjob is a file php5 in /etc/cron.d. However, I'm unable to find the cron files for earlier entries.
You should be able to open the list of scheduled cron events with the crontab command. This should also show you what files run during the cron and where they are located:
crontab -l
You can then add these cron entries to the new server using crontab with the edit parameter, like this:
crontab -e
For more information, see also: http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
This tutorial may also be helpful for understanding cron and crontab: http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/

Resources