0
crontab -e
00 00 * * * /home/username/run.sh >> /home/username/log 2>&1

Inside run.sh:

source /home/username/.bashrc
java

When crontab job is triggered, I got this error in the log:

/home/username/run.sh: 1: /home/username/run.sh: java: not found

However, java path is set in /home/username/.bashrc, so I was expecting sourcing should set the java path:

I know I can fix it by:

export PATH="/usr/local/jdk1.8.0_231/bin:$PATH"
java

But why sourcing .bashrc is not enough? What environment variables does crontab have exactly and what does source .bashrc do exactly (so that crontab doesn't take it into account)?

Thanks.

  • either put the absolute PATH of java in your script, or set the PATH on the crontab entry itself. see man 5 crontab – Jetchisel Mar 29 at 1:35
  • What distro or flavor of linux are you using? does /home/username/.bashrc have an interactivity check (case $- in ... for example)? – steeldriver Mar 29 at 1:37
  • Add /home/username/.bashrc to your question. – Cyrus Mar 29 at 1:45
  • cron executes scripts usually with sh. sh is usually not bash. – Cyrus Mar 29 at 1:48
  • 1
    @steeldriver: yes, my sh knows nothing about source. I suggest to add a shebang with bash in /home/username/run.sh or run /home/username/run.sh with bash. – Cyrus Mar 29 at 2:09
0

I have some guessing but may be wrong:

  • when we open a bash shell, we start a bash process, and when running .bashrc, it makes change to the environment of this bash process only
  • crontab has its own process that has no relation to the bash process opened
  • when sourcing .bashrc, it only makes changes to bash process (though there's none when running crontab??), therefore crontab still does not have any change to the environment variable

Therefore, sourcing .bashrc doesn't help crontab.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.