简体   繁体   中英

python script doesn’t pick up environment variable that is set in crontab

When i run my python script from terminal i get error due to missing env variable. i can fix by this using export and setting it. But there are other python scripts that are running fine as cron jobs that require this as well. And i see this env variable being set in cron tab. So my question is if env variable set in cron is available for scripts run by user/root from cli? Running env command as root doesn't show this variable.

Environment variables are private to each process, not global. When a new process is started it inherits its initial environment from its parent. Your terminal shell isn't started by cron so it doesn't inherit the env vars you've set in your crontab.

Your OS may provide a standard mechanism for setting env vars for all processes (eg, your terminal which then starts your CLI shell). What I prefer to do is create a file named ~/.environ that I then source in my ~/.bashrc and cron jobs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM