0
votes

I am trying to a run a cronjob on my Windows machine with Cygwin that needs access to environment variables that I have defined in my ~/.bashrc. Here all the different things I have tried to get it to access the env variable:

  1. Simple sourcing of bashrc
PATH=/bin:/usr/bin:/usr/local/bin:$PATH
SHELL=/usr/bin/bash
*/1 * * * * source ~/.bashrc ; env | grep "MAP60" 
  1. Invoking bash and running the command
PATH=/bin:/usr/bin:/usr/local/bin:$PATH
SHELL=/usr/bin/bash
*/1 * * * * /usr/bin/bash -c "source ~/.bashrc ; env | grep MAP60"
  1. Trying a bash login shell
PATH=/bin:/usr/bin:/usr/local/bin:$PATH
SHELL=/usr/bin/bash
 */1 * * * * /usr/bin/bash -lc " source ~/.bashrc ; env | grep MAP60"

None of these work, but case 3 works if I add the variable to my ~/.bash_profile. I have also tried sourcing the bashrc with the full path, and also using . instead of source to invoke the file. I've also tried just sourcing the bash_profile directly like in case 1, but that doesn't work either. I would like to have the variables be called in the ~/.bashrc rather than having to call a login shell each time. How can I achieve that with cygwin cron?

1
Is it possible your .bashrc errors out or skips sections in non-interactive mode? Have you tried capturing error output from the cron job? How about /usr/bin/bash -lc " set -x; source ~/.bashrc ; env | grep MAP60" 2>/tmp.error.log - Gordon Davisson

1 Answers

0
votes

Works for me (and everyone else). So something is wrong with your setup. I don't have cron setup on Cygwin but one, very likely, possibility is that it can't email you the output of the command (something that is usually the default on a real UNIX). Start by modifying your third example thusly:

*/1 * * * * /usr/bin/bash -xlc " source ~/.bashrc ; env | grep MAP60" >/tmp/x 2>&1

Then check the contents of /tmp/x.

Note: Bash only reads ~/.bashrc for interactive shells. Using -l does not make the shell interactive. Putting var definitions in ~/.bashrc is not recommended. But if you want to do this you should ensure any command that should only run if the shell is interactive is inside a if [[ $- = *i* ]]; then ...; fi block in your ~/.bashrc.