2

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.

B--rian
  • 5,578
  • 10
  • 38
  • 89
Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
  • Did you check out the answers to [this post](https://unix.stackexchange.com/questions/67940/cron-ignores-variables-defined-in-bashrc-and-bash-profile) and especially [this one](https://askubuntu.com/a/1080812/133393), which explains the pitfalls of using Ubuntu’s default .bashrc in non-interactive shells? – amain May 07 '20 at 01:30
  • Thanks @amain, make sense. The correct question seems this two steps: 1. Where the best place to my `$FTP_etc_PASSWD` variables, `.bash_profile`? 2. how to "run this place" at my Crontab script? ... Or the best is put in `/etc/environment `? – Peter Krauss May 07 '20 at 04:05

1 Answers1

6

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.

B--rian
  • 5,578
  • 10
  • 38
  • 89
  • Hi B--rian, thanks, good clues and explanations! Can you show a script? (I will copy/paste and test it, I need a full "recipe for dummies").. Need to start with `#!/bin/bash` or other magic that you must explain. – Peter Krauss May 06 '20 at 02:16
  • .... Seem that it is **not** the best answer at cited answer page, the best is the simplest and runs with UBUNTU 20 LTS also, is a simple "add `-l` option", see [here](https://stackoverflow.com/a/51591762/287948). – Peter Krauss May 06 '20 at 02:26
  • Well, my solution does not really require a script, and I just said that it is my preferred solution *for Ubuntu* since it bundles all environment variables on a single location. `bash -l`works as well, and so does the accepted answer of the question I quoted. – B--rian May 06 '20 at 14:58
  • Oops, @amain remember that is not a good practice to run `.bashrc` on crontab... So, your answer shows the best choice and a good practice. I tested and crontab is running `/etc/environment`! – Peter Krauss May 07 '20 at 04:32