问题
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