Cron job does NOT get the environment variables set in .bashrc - bash

Here is my cron job:
plee#dragon:~$ crontab -l
* * * * * /bin/bash -l -c 'source ~/.bashrc; echo $EDITOR > /tmp/cronjob.test'
and inside ~/.bashrc file, I have export EDITOR=vim, but in the final /tmp/cronjob.test file, it's still empty?
So how can I get the environment variables (set in .bashrc file) and use it in my cron job?
plee#dragon:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04 LTS
Release: 12.04
Codename: precise
plee#dragon:~$ uname -a
Linux dragon 3.2.0-26-generic-pae #41-Ubuntu SMP Thu Jun 14 16:45:14 UTC 2012 i686 i686 i386 GNU/Linux
If use this:
* * * * * /bin/bash -l -c -x 'source ~/.bashrc; echo $EDITOR > /tmp/cronjob.test' 2> /tmp/cron.debug.res
In /tmp/cron.debug.res:
...
++ return 0
+ source /home/plee/.bashrc
++ '[' -z '' ']'
++ return
+ echo
BTW, the .bashrc file is the default one came with Ubuntu 12.04, with the exception that I added one line export EDITOR=vim.
If I don't use the cron job, instead, just directly do this on the command line:
source .bashrc; echo $EDITOR # Output: vim

The reason for source ~/.bashrc not working is the contents on your ~/.bashrc (default one from Ubuntu 12.04). If you look in it you will see on lines 5 and 6 the following:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
PS1 variable is set for an interactive shell, so it's absent when run via cron, even though you are executing it as a login shell. This is confirmed by contents of the file produced by /bin/bash -l -c -x 'source ~/.bashrc; echo $EDITOR > /tmp/cronjob.test':
+ source /home/plee/.bashrc
++ '[' -z '' ']'
++ return
To make source ~/.bashrc work, comment out the line that checks for presence of the PS1 variable in ~/.bashrc:
#[ -z "$PS1" ] && return
This will make bash execute the entire contents of ~/.bashrc via cron

Answer provided by #alex is correct but in Ubuntu 13.10 the code has been modified a little. There is no $PS1 variable but in lines 6-9 there is a code
case $- in
*i*) ;;
*) return;;
esac
Just commenting out the line which returns works. i.e. the code below works
case $- in
*i*) ;;
# *) return;;
esac

I just tried a file .env_setup_rc file with only one line export EDITOR=vim, surprisingly it's working.
So I guess there is something in .bashrc conflicting with the cron job bash command.

Related

terminal title not setting within screen

Currently, I'm setting terminal title within screen command, but the bash script gives me:
Cannot exec 'source /etc/profile && title.set root#test': No such file or directory
And I can run above command successful directly from the command line, here are my scripts:
/usr/local/bin/s
#!/bin/bash
if [ $1 ]
then
screen -D -R $1 -m "source /etc/profile && title.set `whoami`#$1"
else
screen -R
fi
/etc/profile
...
# Source global bash config
if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi
function title.set() {
if [[ -z "$ORIG" ]]; then
ORIG=$PS1
fi
TITLE="\[\e]2;$*\a\]"
PS1=${ORIG}${TITLE}
}
# Termcap is outdated, old, and crusty, kill it.
unset TERMCAP
# Man is much better than us at figuring this out
unset MANPATH
...
So What's going wrong here?
The keyword source is a bash built-in command, i.e., something for which there is not necessarily an actual file to exec (another built-in command). You can only exec something that is a file — like bash, e.g., something like this:
screen -D -R $1 -m bash -c "source /etc/profile && title.set `whoami`#$1"

Using anyenv in Fish Shell

I tried to using anyenv in Fish Shell but the error occured.
This is my ~./config/fish/config.fish file.
set -x PATH $HOME/.anyenv/bin $PATH
eval (anyenv init -)
What's wrong with this?
Errors which I got:
$# is not supported. In fish, please use 'count $argv'.
- (line 1): begin; source "/Users/kasumi/.anyenv/libexec/../completions/anyenv.bash" anyenv() { typeset command command="$1" if [ "$#" -gt 0 ]; then shift fi command anyenv "$command" "$#" } export NDENV_ROOT="/Users/kasumi/.anyenv/envs/ndenv" export PATH="/Users/kasumi/.anyenv/envs/ndenv/bin:$PATH" export PATH="/Users/kasumi/.anyenv/envs/ndenv/shims:${PATH}" source "/Users/kasumi/.anyenv/envs/ndenv/libexec/../completions/ndenv.bash" ndenv rehash 2>/dev/null ndenv() { typeset command command="$1" if [ "$#" -gt 0 ]; then shift fi case "$command" in rehash|shell) eval "`ndenv "sh-$command" "$#"`";; *) command ndenv "$command" "$#";; esac }
^
from sourcing file -
called on line 60 of file /usr/local/Cellar/fish/2.4.0/share/fish/functions/eval.fish
in function 'eval'
called on line 6 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
called on standard input
source: Error while reading file '-'
Added:
I tried answer (https://stackoverflow.com/a/42119354/7524270) but I get some new errors.
New Errors:
Variables cannot be bracketed. In fish, please use "$PATH".
- (line 1): begin; source "/Users/kasumi/.anyenv/libexec/../completions/anyenv.fish" function anyenv set command $argv[1] set -e argv[1] command anyenv "$command" $argv end set -x NDENV_ROOT "/Users/kasumi/.anyenv/envs/ndenv" set -x PATH $PATH "/Users/kasumi/.anyenv/envs/ndenv/bin" export PATH="/Users/kasumi/.anyenv/envs/ndenv/shims:${PATH}" ndenv rehash 2>/dev/null ndenv() { typeset command command="$1" if [ "$#" -gt 0 ]; then shift fi case "$command" in rehash|shell) eval "`ndenv "sh-$command" "$#"`";; *) command ndenv "$command" "$#";; esac }
^
from sourcing file -
called on line 60 of file /usr/local/Cellar/fish/2.5.0/share/fish/functions/eval.fish
in function 'eval'
called on line 4 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
called during startup
source: Error while reading file '-'
Quick fix:
In ~/.config/fish/config.fish, change:
eval (anyenv init -)
to:
eval (anyenv init - fish)
EDIT: or a slightly more bullet-proof approach:
eval (command anyenv init - fish)
(command forces fish to ignore functions)
What's going on?
anyenv init uses the $SHELL environment variable to determine your active shell and returns some commands, to be executed by the shell, in order to finish the initialization of anyenv.
For some reason the $SHELL variable in your environment points to bash and not to fish (you can verify this by running echo $SHELL).
The most probable reason is that fish is simply not the default shell for your user. You may execute grep $USER /etc/passwd to find out (look at what it says after the last :). It's possible to change the default shell with chsh, but if you decide to do it, do it with great care. There might be some important settings in your .bashrc or some other programs may depend on a POSIX-compliant shell to work properly.
Another option is that somewhere you have an override of the $SHELL variable, such that it points to bash and not fish. I have this in my byobu/tmux configuration because it helped me avoid some strange behavior.
Luckily, anyenv init lets the user (you) specify the shell manually (ignore $SHELL), and that should fix your issue with anyenv.

.profile has statements to call .bashrc, but .bashrc not executed

I'm using Ubuntu 16.04. In my $HOME/.profile, it has
echo `date` ':.profile executed starts' >> /tmp/testtmp.txt
echo $BASH_VERSION >> /tmp/testtmp.txt
if [ -n "$BASH_VERSION" ]; then
echo 'if 1 entered' >> /tmp/testtmp.txt
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
echo 'if 2 entered' >> /tmp/testtmp.txt
. "$HOME/.bashrc"
fi
else
echo 'if 1 not entered' >> /tmp/testtmp.txt
fi
Every time I login, it never even reaches the first IF.
The test file shows:
Sat Jan 14 05:15:57 EST 2017 :.profile executed starts
if 1 not entered
Sat Jan 14 05:15:57 EST 2017 :.profile executed ends
I echoed $BASH_VERSION, it shows 4.3.46(1)-release. So if if [ -n "$BASH_VERSION" ] is meant to check if that variable exists, (apparently yes), why is it not even reaching the first IF?
From the result, it looks like $BASH_VERSION is not generated until the first time I invoked terminal application.
As those IF statements in .profile are original, I don't understand if $BASH_VERSION is not generated when you login in, what is the purpose to have these statements originally.
Thanks
(This was getting overly long as a comment :) )
See this question on Ask Ubuntu. .profile is probably running under sh, not bash, so BASH_VERSION isn't set in .profile.
To test this, add
ps >> /tmp/testtmp.txt
to your .profile. You will see the process name of the shell you're running under, which may be dash per this.
To see what your default shell is, grep <whatever your username is> /etc/passwd - you will see /bin/<whatever>sh.
If indeed /bin/bash is not set as your default shell, you can change it (per this answer):
sudo chsh -s /bin/bash <whatever your username is>

Can't execute nvm from new bash

When I open a new bash, nvm command is not found. It seems that the ~/.nvm/nvm.sh is not being loaded on bash startup.
I have added the line below to ~/.bashrc
[[ -s /home/$USER/.nvm/nvm.sh ]] && . /home/$USER/.nvm/nvm.sh
Any ideas why it is happening?
If you are running from a new bash instance, and you HAVE the initialization code at your ~/.bashrc, ~/.bash_profile, etc, then you need to check this initialization file for conditionals.
On Ubuntu 14, there is a:
case $- in
*i*) ;;
*) return;;
esac
At line 6, that will halt it's execution if bash is not being ran with the "-i" (interactive) flag. So you would need to run:
bash -i
Also, at the end of the file, there is a
[ -z "$PS1" ] && return
That will halt it's execution if not being ran with $PS1 set (like on a remote ssh session).
If you do not wish to add any env vars or flags, you will need to remove those conditionals from your initialization file.
Hope that's helpful.

Something in my bashrc file causing bash: command not found?

When I open up a new shell I get:
Last login: Sun Mar 23 10:14:46 on ttys000
-bash:  : command not found
I'm not totally sure how to figure out what's going on there, as its not totally clear which command its talking about.
Is it likely something in the .bashrc file?
HISTSIZE=10000
HISTFILESIZE=20000
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad
export PS1="\[\e[01;32m\]\h \[\e[01;34m\]\W \$(parse_git_branch)\[\e[01;34m\]$\[\e[00m\] "
export PYTHONSTARTUP=/Users/JimShook/.pythonstartup
export WORKON_HOME=$HOME/.virtualenv
source /usr/local/bin/virtualenvwrapper.sh
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}
PATH=$PATH:/usr/local/rvm/bin # Add RVM to PATH for scripting
### Added by the Heroku Toolbelt
Or maybe the bash_profile?
if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Any thoughts are helpful.
Things to check - as suggested above:
anything changed recently? if so, start there... (added new line/command/sourced file?)
look for 'control codes' in the your file(s)
Try:
cat -v ~/.bashrc ## look for '^M' or other special chars - remove if present
bash -n ~/.bashrc ## try the 'sourced files' separately
If using VI, some thing to try after opening the file:
:syntax off ## turn off colors - may be easier to see 'hidden' codes
:set list ## control codes may be visible

Resources