如何在crontab上使用env .bashrc变量? [英] How to use env .bashrc variables on crontab?

查看:185
本文介绍了如何在crontab上使用env .bashrc变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将在我的个人.bashrc上定义的个人env变量$FTP111_PASSWD发送给crontab执行. 如何在crontab上设置环境变量?

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?

我为执行crontab而创建的名为cron4_etc.sh的原始shell脚本是:

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

如果我通过终端./cron4_etc.sh执行脚本,则执行正常,但是如果使用以下crontab行启动了脚本,则

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说

密码:登录不正确.\ n登录失败.

我尝试使用此建议来改善脚本,但错误仍然存​​在:

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:我正在使用Ubuntu 18 LTS,但问题是针对通用crontab.

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

推荐答案

我最喜欢的解决方案来自 Augusto Destrero ,他为一个非常相似的问题提供了以下答案:

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

/etc/environment中设置vars在Ubuntu中也对我有用.从12.04开始,已在/etc/environment中的变量为cron加载.

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

有人建议做一个简单的env >> /etc/environment来附加您的所有环境变量,但是我会小心一点,而要手动检查/etc/environment.

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.

这篇关于如何在crontab上使用env .bashrc变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆