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

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
  • 37,856