My cron job is giving different result as compared to the python script running manually

54 Views Asked by At

I have written the code to fetch data from opensearch in python script.
The last record printed through the python script is:
Connection done 2023-08-16 04:49:26.896033
Last input time of the retrieved records: 2023-07-20T12:31:41

But when I schedule the python script to run as cron job then the output of the last record shows the timestamp as :

2023-08-16T10:00:01+0000 - Running cron job...
Connection done

Last input time of the retrieved records: 2023-07-20T18:01:59

There is a mismatch in data. I cannot provide the code for the python script. But I have scheduled the cron job to run every hour like this:

0 * * * *  /usr/bin/env bash -c 'echo "$(date -u +\%Y-\%m-\%dT\%H:\%M:\%S\%z) - Running cron job..." >> /home/opc/output.log && /usr/bin/python3 /home/opc/path/to/script/python.py >> /home/opc/output.log 2>&1'.

If I run the script;

/usr/bin/env bash -c 'echo "$(date -u +\%Y-\%m-\%dT\%H:\%M:\%S\%z) - Running cron job..." >> /home/opc/output.log && /usr/bin/python3 /home/opc/path/to/script/python.py >> /home/opc/output.log 2>&1' 

then the data printed is correctly but not as cron job.

Have I scheduled the cron job incorrectly?
Can anyone help me.

I want the cron job to provide the exact data as the python script.

0

There are 0 best solutions below