How to use env .bashrc variables on crontab?

半腔热情 提交于 2020-05-14 09:07:17

问题


I need to send a personal env variable $FTP111_PASSWD defined at my personal .bashrc to crontab execution. How to set an environment variable on crontab?

My original shell script, named cron4_etc.sh, created for crontab execution was:

#!/bin/bash

cd /myFolder/
ftp -n -i <<EOF
open 101.111.111.111
user myUser "$FTP111_PASSWD"
mget check_*.log
bye
EOF

If I execute the script via terminal ./cron4_etc.sh it is executing fine, but if I have start it using the following crontab line

  */20 *     *   *   *     /home/myUser/cron4_etc.sh > /tmp/cron4.log 2>&1

crontab says

Password: Login incorrect.\nLogin failed.

I tried to improve my script using this suggestion, but the error persists:

#!/usr/bin/env bash

# set environment
source /home/myUser/.bashrc

cd /tmp/pg_io/PGW
ftp -n -i <<EOF
open 101.111.111.111
user myUser "$FTP111_PASSWD"
mget check_*.log
bye
EOF

PS: I am using Ubuntu 18 LTS, but the question is for generic crontab.


回答1:


My favorite solution comes from Augusto Destrero, who provided the following answer to a very similar question:

Setting vars in /etc/environment also worked for me in Ubuntu. As of 12.04, variables in /etc/environment are loaded for cron.

Some people suggest to do a simple env >> /etc/environment to append all your environment variables, but I would be careful with that and rather review /etc/environment manually.



来源:https://stackoverflow.com/questions/61438824/how-to-use-env-bashrc-variables-on-crontab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!