1

I figured I'd ask my first SO question after spending all day breaking my head over this.

So, I have this bash script that fully works when I run it from anywhere and from both my user and root. All it does is move some files around, then execute a python3 script and a mysql statement:

 #!/bin/bash
 rm -rf /some/directory
 cp -r /origin/directory /destination/directory
 /usr/bin/python3 /python/script/directory.py
 /usr/bin/mysql --login-path=host --database=dbname -e 'CALL function()'

The problem arises when I try to put it into cron. Since these commands require sudo, I added it into the sudo crontab -e and did some testing. The problem is that the command executes, yet nothing happens.

This is what I put in on the crontab: 0 * * * * bash /directory/with/script.sh 2>&1 > /directory/with/script/latest_run

And this is the output when checking sudo grep CRON /var/log/syslog:

Jun 8 00:42:01 hostname CRON[7148]: (root) CMD (bash /directory/with/script.sh 2>&1 > /directory/with/script/latest_run)

However, when I check the file latest_run it returns empty. It does get created every time. And more worrisome is that the database I'm trying to update doesn't get any data at all, yet when I execute it myself manually it does everything I need it to do.

I have spent all day playing with things on my distro to see if I can fix this but I can't find any solutions. If anyone knows what's wrong I'd greatly appreciate some help!

Thanks!

asked Jun 8, 2020 at 1:05
11
  • Could you try this? Type sudo su -, type password if asked. You'll drop into root. Then do crontab -e and type your cron entry there with a time in the near future. See if runs then. Commented Jun 8, 2020 at 1:15
  • Just tried, same result. Nothing happens to the database and output file is empty. Commented Jun 8, 2020 at 1:28
  • Okay, so, perhaps your script runs fine by cron but the script doesn't do the job its supposed to do. Change your .sh file like so and allow cron to run the shell script: ideone.com/jHmRsk. In your output, do you see all the running and done running lines? Commented Jun 8, 2020 at 1:32
  • Thanks for the script. Yes, when cron executes all of the echoes are triggered and printed into the file. I think the problem could be bash trying to execute the python and/or mysql calls? I don't know how to explicitly have those print exceptions into the file. Commented Jun 8, 2020 at 2:43
  • What does directory.py do? Commented Jun 8, 2020 at 3:01

1 Answer 1

2

I figured it out!

I saw all over people mentioning that the environment of cron for root is actually very small, but being a bash beginner I was unsure on what this meant.

Turns out cron is unable to execute python3 or mysql without having an environment set previously and specified for it to be able to use them.

Therefore adding HOME=/home/myuser and SHELL=/bin/bash (as I use bash instead of sh) did the trick.

In other words the crontab ended up like this:

SHELL=/bin/bash
HOME=/home/myuser
0 * * * * /bin/bash /directory/with/script.sh 2>&1 > /directory/with/script/latest_run

I am aware that security-wise one shouldn't set the root cron to use a personal $HOME variable, but it's fine since only 3 people use this server for the same project anyway.

Thanks everyone!

answered Jun 8, 2020 at 6:02
Sign up to request clarification or add additional context in comments.

3 Comments

See if you can mark your own answer as accepted to put closure to your question. There should be a tick mark by your answer. If it's there, click it to mark your own answer as accepted.
Good show! I think you have to wait 48 hrs to accept your own answer, but please do so, as mentioned above, and it will help you gain valuable reputation points. ;-)!
Thanks to you both! Definitely will as soon as it lets me. Have a good one!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.