2

All we know that cron ignores variables defined in “.bashrc” and “.bash_profile”, so we have to define it inside cron. I constantly do the same what was written inside the similar question https://unix.stackexchange.com/questions/67940/cron-ignores-variables-defined-in-bashrc-and-bash-profile but still global variables inside .bashrc still not working. I found way to execute it - by defining sh script with "set +a" bashrc script. But "source" still doesn't work.

SHELL=/bin/bash
BASH_ENV=/root/.bashrc
PATH=:/opt/spark/spark-2.2.0-bin-hadoop2.7/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
SPARK_HOME=/opt/spark/spark-2.2.0-bin-hadoop2.7
MAILTO=root HOME=/

# m h  dom mon dow   command
* * * * * /bin/bash -c 'source $HOME/.bashrc; echo "SPARK_HOME: '$SPARK_HOME'"; echo "JAVA_HOME: '$JAVA_HOME'"' > /var/log/file.log 2>&1

# DO NOT DELETE LAST LINE

return log file

SPARK_HOME: /opt/spark/spark-2.2.0-bin-hadoop2.7
JAVA_HOME:

also tried to execute this in interactive mode as it was written by mklement0
source .bashrc in a script not working

* * * * * /bin/bash -i source /root/.bashrc; echo $JAVA_HOME > /var/log/file.log 2>&1

As you can see SPARK_HOME is defined inside crontab whilst JAVA_HOME only in .bashrc. P.S in bashrc java home is defined "export JAVA_HOME=/usr/jdk1.8.0_131" All different cron jobs work fine when I change JAVA_HOME to SPARK_HOME, tried different crontab jobs but the answer is the same as it was.

ubuntu kernel version is

uname -a
Linux 6101c32b9243 4.9.62-21.56.amzn1.x86_64 #1 SMP Thu Nov 16 05:37:08 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Ivan Shelonik
  • 1,958
  • 5
  • 25
  • 49

1 Answers1

7

Your last attempt runs bash and sources the file, then exits Bash and runs the echo in the calling shell, which isn't Bash, and of course doesn't know or care that the now-defunct Bash process loaded some settings and then forgot them when it exited. (Or, well, this is what would happen if you fixed the simple quoting errors to keep the source command and its argument as a single string.)

The superficial fix is easy;

* * * * * bash -c 'source $HOME/.bashrc; echo "$JAVA_HOME"' >/var/log/file.log 2>&1

The proper fix, however, is probably to encapsulate all of this in a separate script and keep your crontab really simple.

(Are you really sure your user is allowed to overwrite the log file? Replacing the log once a minute seems rather misdirected, although it's good enough for a quick test to see if the job is running at all.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks a lot, I've tried what you have written and still it only works with $SPARK_HOME variable. anything else that I can try ?) it's already even doesn't funny. P.S the overwriting log is just for testing. – Ivan Shelonik Jan 17 '18 at 14:25
  • If you have two jobs writing to the same file, it will obviously look like only one of them is working. But again, putting all your commands in a script file is probably the way to go, and removes this sort of problem. – tripleee Jan 17 '18 at 15:51
  • I have only 1 job. – Ivan Shelonik Jan 18 '18 at 09:59
  • Your post clearly shows two `cron` jobs writing to the same file, pretty much simultaneously. If that's not correct then I really suggest you review the [troubleshooting help in the Stack Overflow `cron` tag info page](/tags/cron/info) and maybe then if you still can't figure it out, post a new question with more details about what *exactly* you are doing and how *exactly* you conclude that only one variable can be printed but not the other. – tripleee Jan 18 '18 at 10:02
  • As repeatedly suggested already, putting `echo "SPARK_HOME: '$SPARK_HOME'"; echo "JAVA_HOME: '$JAVA_HOME'"` in a single script and demonstrating that one is printed while the other is not when you run this script out of `cron` would be a nice demo. Log entries showing when exactly `cron` ran what exactly should also be useful for diagnostics. – tripleee Jan 18 '18 at 10:03
  • Have done what you said. can see tre reply. ps only 1 job is existing. Maybe there is something with bash interactive mode? – Ivan Shelonik Jan 18 '18 at 10:18
  • So it sources both variables but prints only one? If that is indeed the case, I really think a separate, new question is required. Again, the `cron` tag info page I linked above should suggest a number of relevant diagnostics to include. – tripleee Jan 18 '18 at 10:27