cron job can't source functions from bashrc
Try changing the source ~/.bashrc
to . ~/.bashrc
That change will reload the contents of bashrc.
Then run_this_function should be available... if it is assigned properly as an alias.
Do you have the run_this_function
declaration? - please post if you can.
Cron job does NOT get the environment variables set in .bashrc
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
source .bashrc in a script not working
Some platforms come with a ~/.bashrc
that has a conditional at the top that explicitly stops processing if the shell is found to be non-interactive - even though bash
only automatically sources ~/.bashrc
in interactive (non-login) sessions anyway.
For example, on Ubuntu 18.04:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
A similar test, seen in /etc/bash.bashrc
on the same platform:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
If this is the case, sourcing ~/.bashrc
from a script will have no effect, because scripts run in non-interactive shells by default.
Your options are:
Either: deactivate the conditional in
~/.bashrc
Or: Try to to emulate an interactive shell before invoking
source ~/.bashrc
.
The specific emulation needed depends on the specifics of the conditional, but there are two likely approaches; you may have to employ them both if you don't know ahead of time which conditional you'll encounter:set -i
temporarily to make$-
containi
, indicating an interactive shell.- If you know the contents of the line that performs the interactivity test, filter it out of the
~/.bashrc
usinggrep
, and then source the result witheval
(the latter should generally be avoided, but it in this case effectively provides the same functionality as sourcing).
Note that making sure that environment variablePS1
has a value is not enough, because Bash actively resets it in non-interactive shells - see this answer for background information.eval "$(grep -vFx '[ -z "$PS1" ] && return' ~/.bashrc)"
Alternatively, if you control how your own script is invoked, you can invoke it with bash -i script
.
Crontab not using the django env variables defined in .bashrc
found the answer:
Cron not running django command
I defined these values manually inside the cron script.
Related Topics
Return Values from Bash Script
Terraform - Unable to Run Multiple Commands in Local Exec
Recursively List Files from a Given Directory in Bash
Ffmpeg Img to Video = Could Find No File with Path
Where Is The Linux Socket Implementation
Setuid on an Executable Doesn't Seem to Work
How to Use Sysfs Inside Kernel Module
Docker Warning on Cgroup Swap Limit, Memory.Use_Hierarchy
How to Print on Stderr with Swift
Count Number of Column in a Pipe Delimited File
When Did Hup Stop Getting Sent and What How to Do About It
Difference Between .So.0 and .So.0.0.0 Files
How to Use 'History-C' Command in a Bash Script
How to Compile Redis So That I Can Upload and Run It on Shared Hosting