2

EDITED:

I got a script running with a cronjob every day at 1 am:

0 1 * * * /bin/bash /home/performanceRatio.sh

It also appears in the cron.log:

Feb  2 01:00:01 inf-education-67 CRON[108963]: (root) CMD (bash performanceRatio.sh)
Feb  2 01:00:01 inf-education-67 CRON[108962]: (CRON) info (No MTA installed, discarding output)

Expected behavior is to update a mysql database. Changes appear if it's run manually.

Using Ubuntu 20.04

My script looks like:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

python3 get_weather_data_crn.py && python3 pv.py

* * * * * env > /tmp/env.output shows:

HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
LANG=de_DE.UTF-8
SHELL=/bin/sh
PWD=/root
why me
  • 131
  • 1
    You will likely want to use the full path to bash: /usr/bin/bash –  Feb 03 '21 at 07:35
  • @Matigo Thanks, i will try this out and respond tomorrow if it worked. Do I also have to use the full path of the file? Like /home/performanceRatio.sh ? – why me Feb 03 '21 at 08:02
  • 1
    Yes, that would be important as well. Always use full paths for everything when working with crontab. –  Feb 03 '21 at 08:09
  • @Matigo All right, I'll try this out and respond to you tomorrow. Thanks so far! :) – why me Feb 03 '21 at 08:09
  • 2
    You will need the full path to performanceRatio.sh unless it's located in the HOME of the user whose crontab the job is in ... you should not need the full path to bash since /bin and /usr/bin are in the default cron PATH. However you wouldn't need bash at all if you use an appropriate shebang in your script and make it executable. – steeldriver Feb 03 '21 at 13:49
  • 1
    Is the full path of your script /home/performanceRatio.sh ?? – Artur Meinild Feb 04 '21 at 10:08
  • @ArturMeinild Yes, it's the full path. – why me Feb 04 '21 at 10:23
  • 1
    And what's the path of get_weather_data_crn.py and pv.py? Are they also in /home? Because I can't see /home anywhere in your path. You need to reference full path for python scripts also. – Artur Meinild Feb 04 '21 at 10:53
  • @ArturMeinild Yes, they are in /home . Do I have to add it in performanceRatio.sh too? Like: python3 /home/get_weather_data_crn.py && /home/python3 pv.py – why me Feb 04 '21 at 10:55
  • 1
    Yes you have to. – Artur Meinild Feb 04 '21 at 10:56
  • @ArturMeinild Sadly still not working. – why me Feb 04 '21 at 10:58
  • 1
    You made an error in the last part. It should be python3 /home/get_weather_data_crn.py && python3 /home/pv.py – Artur Meinild Feb 04 '21 at 11:11
  • @ArturMeinild Yea, I recognized that. I tested to run env > /tmp/env.output inside the sh file and it did the jib. But the Python scripts weon't run.. – why me Feb 04 '21 at 11:25

2 Answers2

1

Well, maybe it was a combination of the edits I already made (also in my post). But the final solution to get it all working was this:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

python3 /home/get_weather_data_crn.py && python3 /home/pv.py

Yes, not making a NEWLINE! after && prevented Crontab to run the python scripts.

why me
  • 131
0

Jobs run through cron, crontab, aren't run in the same runtime environment that you have on your desktop. None of your PATH changes, or other environment variable settings are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

waltinator
  • 36,399