Crontab's environment variable problem for scheduled tasks

Today I wrote a script file that uses the localtime function of the time library in Python3. When executing the script file alone, it can support obtaining the server date and time. But when it is executed regularly in Crontab, it is found that the greenwich time is obtained. Obviously the system has been changed to Beijing time, why is it still showing GMT?

 

It is suspected that the system environment variables are inconsistent when the crontab is executed and when the py script is executed alone.

 

In the end, the py script was executed directly in the crontab, and the shell file was executed regularly, and passed in the shell file.

 

#!/bin/sh
source /etc/profile

 Record the user environment variables, and then execute the py file to successfully obtain the Beijing time.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326082037&siteId=291194637