Wonton Wonton - 16 days ago 7
Linux Question

crontab not working despite trying different fixes from Stackoverflow

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:


  1. At 04:00. Remove all backups that are more than 7 days old.

  2. At 04:04. Backup all my mongodb databases.

  3. At 04:30. Try to renew my SSL certificate.

  4. At 04:35. Reload Nginx service.

  5. 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:


  1. I don't know if this command is working. The log file (log1.log) is empty.

  2. It's not working. No backup have been created. The log file (log2.log) has not been created.

  3. It seems that it's working. The log file (log3.log) shows the standard output when you run this command correctly.

  4. It seems that it's working. The log file (log4.log) shows the standard output when you run this command correctly.

  5. 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!

Answer

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.

Comments