OS: Ubuntu 16.04

I've looked at a lot of different articles and threads, and I still can't get my cron.hourly jobs to run automatically. I am trying to run a backup on my PostgreSQL database. In /etc/cron.hourly, I have only one script named pg_backup. Here are the permissions on it:

-rwxr-xr-x   1 root root  1543 Oct 16 19:07 pg_backup

If I run run-parts --test /etc/cron.hourly, the output is as follows:

/etc/cron.hourly//pg_backup

That looks good. If I run the script using run-parts manually, it works as expected and backs up my database. Here's the command I'm using:

run-parts -v --report /etc/cron.hourly

That's great, but for whatever reason, it's not running every hour by itself. I really don't know what could be wrong. I've checked all the logs and haven't been able to find any error messages.

What am I missing?

UPDATE

I noticed that my /etc/crontab file has the shell specified like so: SHELL=/bin/sh. My script runs in bash. Is it sufficient to have the shebang at the top of my script for it to use bash? #!/bin/bash

UPDATE 2

As suggested by Broco, I put the job in my crontab, and it's still not working. Here's what I did:

sudo crontab -e

Then I added the following to the file:

* * * * * root /etc/cron.hourly/pg_backup

I made it run every minute to get quicker feedback on whether or not it's running, and it seems to be running but isn't actually performing the backup.

Here's part of my syslog. I used the command grep CRON /var/log/syslog:

Oct 17 14:02:01 User CRON[63871]: (root) CMD (root /etc/cron.hourly/pg_backup)
Oct 17 14:02:01 User CRON[63870]: (CRON) info (No MTA installed, discarding output)
Oct 17 14:03:01 User CRON[63917]: (root) CMD (root /etc/cron.hourly/pg_backup)
Oct 17 14:03:01 User CRON[63916]: (CRON) info (No MTA installed, discarding output)
Oct 17 14:04:01 User CRON[63966]: (root) CMD (root /etc/cron.hourly/pg_backup)
Oct 17 14:04:01 User CRON[63965]: (CRON) info (No MTA installed, discarding output)
Oct 17 14:05:01 User CRON[64013]: (root) CMD (root /etc/cron.hourly/pg_backup)
Oct 17 14:05:01 User CRON[64012]: (CRON) info (No MTA installed, discarding output)
Oct 17 14:05:01 User CRON[64014]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
Oct 17 14:06:01 User CRON[64060]: (root) CMD (root /etc/cron.hourly/pg_backup)
Oct 17 14:06:01 User CRON[64059]: (CRON) info (No MTA installed, discarding output)
Oct 17 14:07:01 User CRON[64106]: (root) CMD (root /etc/cron.hourly/pg_backup)
Oct 17 14:07:01 User CRON[64105]: (CRON) info (No MTA installed, discarding output)
  • just out of curiousity, what time does cron.hourly do its job on your system? it is not necessarly at exactly on the hour, for example my system is on minute 17. you can check it out in /etc/crontab – Wissam Youssef Oct 17 '16 at 5:08
  • Thanks for the comment! Mine is on minute 17 as well. I've waited a few hours without any luck. – Alexander Oct 17 '16 at 5:48
  • Can you show the output of /var/log/cron.log or similar files? – shodanshok Oct 17 '16 at 6:08
  • Does it work if you put the file directly into /etc/crontab? E.g. 0 * * * * root /etc/cron.hourly/pg_backup – Broco Oct 17 '16 at 6:18
  • @shodanshok @Broco Thanks for the questions! I've updated my question with that information. It doesn't seem to be working even with putting the script in my crontab. – Alexander Oct 17 '16 at 14:18
up vote 0 down vote accepted

Your cron job is running, but the job is having a problem.

You normally should get an email with the output (and errors!) but you do not get email because:

Oct 17 14:02:01 User CRON[63870]: (CRON) info (No MTA installed, discarding output)

You don't have anything installed on your system that is capable of sending mail. Thus, the output is thrown away.

Consider installing an MTA so that you can have the output emailed to you and then find out what is actually happening.

I figured it out! As Michael Hampton pointed out, my job was running, but it had some errors. I didn't have an MTA installed, so I installed postfix with sudo apt-get install postfix. After doing so, I was able to look at the /var/mail/root file to see error messages related to my cron jobs.

My main issue ended up being related to missing environment variables. Since cron runs on root, my environment variables weren't set whenever the script ran. So, I included my .bashrc at the top of my script like this:

source "$HOME/.bashrc"

In my root user's .bashrc, I added the environment variables that I needed for the script. That still didn't work, though; the environment variables still weren't being set when the script ran. To fix this, I added to my script the following line before the line that includes .bashrc:

export PS1="1"

From this thread, this environment variable prevents .bashrc from loading in non-interactive sessions, so when it is set, .bashrc will load.

Thanks a lot for the help, everyone!

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.