0

If I run my backup script manually from the command line it works fine. No issue backup is created. In the crontab log, I can see the entry for running the backup at 2:00 AM, /home/sgaddis/copy-production-db.sh:

Dec 23 01:01:01 mapehr anacron[11921]: Anacron started on 2019-12-23
Dec 23 01:01:01 mapehr anacron[11921]: Normal exit (0 jobs run)
Dec 23 01:01:01 mapehr run-parts(/etc/cron.hourly)[11923]: finished 0anacron
Dec 23 02:00:01 mapehr CROND[14869]: (sgaddis) CMD (sgaddis /bin/bash /home/sgaddis/copy-production-db.sh)
Dec 23 02:01:01 mapehr CROND[14934]: (root) CMD (run-parts /etc/cron.hourly)
Dec 23 02:01:01 mapehr run-parts(/etc/cron.hourly)[14934]: starting 0anacron
Dec 23 02:01:01 mapehr anacron[14943]: Anacron started on 2019-12-23

but when I look in the /home/sgaddis/backup there is no backup.

It should not be a permission issue since it is my home/backup folder. My account should have the right to dump the backup there as it does when I run the script manually. Any place else I should be looking for clues?

I went to https://linux4one.com/how-to-set-up-cron-job-on-centos-7/ to see if there was anything I left out in creating the cronjob

I searched this site was unable to find a question similar to this one. The cronjob is not hanging or not being executed. The closest I found was

  https://stackoverflow.com/questions/44723154/using-cronjob-to-automatically-backup-mysql-files, 

but as I said earlier in my post I have already run the script from the command line and it works fine so the above post does not answer my question.

Searching this site

does not produce a similar question to the one that I have posted. If you know of a similar post please point it out and not just vote down my post.

The exact line is this

0 2 * * * root /bin/bash /home/sgaddis/copy-production-db.sh

==UPDATE==

This is the command that is in the log file today.

 mapehr CROND[21076]: (sgaddis) CMD (/bin/bash /home/sgaddis/copy-production-db.sh)

Now it is using my user account to launch the cron job.

Maybe my user account does not have permission to execute a cron job?

7
  • 4
    Add in your question the exact cron record and the script. And redirect the output of the script to log file for debugging purpose Dec 30, 2019 at 7:52
  • 1
    What does your script look like? Does it depend on anything (environmental variables, current working directory etc.) that may not be the same between the cron environment and your interactive shell environment?
    – Kusalananda
    Dec 30, 2019 at 8:43
  • 2
    The log entry (sgaddis) CMD (sgaddis /bin/bash ... suggests erroneous inclusion of a username field in the crontab entry Dec 30, 2019 at 12:13
  • @steeldriver: See my anser. :)
    – Thorian93
    Dec 30, 2019 at 15:10
  • 2
    @user1794918 No, you should put it in root's crontab if it requires root privileges. You can edit root's crontab with sudo crontab -e.
    – Kusalananda
    Dec 31, 2019 at 13:32

2 Answers 2

4

Hi and welcome to the community!

On first sight the page you used as a tutorial say to use crontab -e but then says you should put a user name in your cron entry. When using the crontab tool, there is no need for a user name in the cron entry, as the crontab invoked through the tool is run in user context anyway. If you provide the exact line you used in your configuration we might be able to correct this properly.

Nevertheless this is probably simply an issue in your cron entry. Try removing the username if present from your cron entry and give it just the timing configuration and the exact command you used manually like this:

0 0 * * * your-command /path/to/script.sh
6
  • I did have the username root in the crontab. I have removed the username from the line. I will report back tomorrow as the results. Dec 30, 2019 at 18:45
  • I removed my user name from the script and it still failed to execute when called by the cron. Dec 31, 2019 at 12:34
  • See this comment on your question, that should solve it.
    – Thorian93
    Jan 2, 2020 at 8:53
  • 0 2 * * * ~/copy-production-db.sh this is the solution to my problem. Jan 2, 2020 at 12:13
  • If you call your script like that you might want to make sure, that there is a shebang in your script. Also if my answer solved your problem generally I would highly appreciate it if you marked it as the answer.
    – Thorian93
    Jan 2, 2020 at 12:37
1

From comments it is clear that you want to run the cron job as root.

You can schedule such a script in two ways:

  1. Put it in the system's crontab in /etc/crontab. This crontab is a crontab file with an extra user field, like what you show in your question. I would not recommend this though as some Unix systems may manage this crontab by automatic means. Instead...
  2. Put the job in the root user's crontab. You can edit the crontab of the root user with the command sudo crontab -e from you ordinary user account, if you have sudo rights. This crontab is the root user's "personal crontab", and it is handy for running your local scheduled jobs from that need root privileges.

Note that the environment that the script is being run in from cron may well be different from the environment that you run it from in an interactive shell. Differences may include what the current working directory is (write your script so that files are being copied to and from absolute paths), and what environment variables are set and to what values (variables set in your shell startup files will not be available; set these explicitly in the script instead).

2
  • I don't think it is clear from the comments that OP wants to run it as root, in fact I'd say it is clear from his description that he has tested the script as user sgaddis it sits in directory /home/sgaddis/ and the output should end up in /home/sgaddis/backup, so if ever there's a case for user sgaddis to use crontab -e to edit their crontab entry, this is it!
    – X Tian
    Dec 31, 2019 at 13:46
  • @XTian this comment is pretty clear
    – roaima
    Dec 31, 2019 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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