2

I'v hit a brick wall with crontab... I'm trying to set a crontab to run a python script that gathers 4 variables from ~/.bashrc

Bellow my current crontab.

SHELL=/bin/bash
BASH_ENV=/home/m.bienias/.bashrc
# m h  dom mon dow   command
30 12,15,18 * * 1,2,3,4,5 source /home/m.bienias/.bashrc; /usr/bin/python3
/home/m.bienias/skrypty/mail_reporter/Kwanty_bez_eng.py >> /home/m.bienias/cron-log/mail_reporter.log 2>&1``` 

I have tried ```source /home/m.bienias/.bashrc;``` and ```. /home/m.bienias/.bashrc;```

Any idea what more I've could miss. Please note that I'm not sudo user on the machine where I try do run crontab

Niviral
  • 127
  • 1
  • 10
  • 1
    maybe `bash -c '. home/m.bienias/.bashrc; python...'` – F. Hauri Nov 06 '19 at 07:15
  • Unfortunately didn't work – Niviral Nov 06 '19 at 07:25
  • Have you tried to run a script which starts with #!/bin/bash and put your code in it? As I remember, crontab runs under cshell or sth like that ... by script you can probably change the shell for it, I guess so. I would give it a try ... – Honza P. Nov 06 '19 at 07:26
  • @F Hauri your initial answer has missing leading `/` before `home` – Darren Smith Nov 06 '19 at 07:54
  • @HonzaP. Do you mean adding ``` #!/bin/bash``` to the beginning of crontab? – Niviral Nov 06 '19 at 09:14
  • No, I mean add it as the first row in .sh script file – Honza P. Nov 06 '19 at 12:11
  • Does this answer your question? [Where can I set environment variables that crontab will use?](https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use) – B--rian May 05 '20 at 13:52

2 Answers2

6

I would recommend creating a specific start wrapper script for your job.

Name the script something like run-Kwanty_bez_eng.sh and store it inside /home/m.bienias/skrypty/mail_reporter/

This script is responsible for setting the environment and starting the job, so rough contents would be like:

#!/usr/bin/env bash

# set environment
source /home/m.bienias/.bashrc

# start job
/usr/bin/python3 /home/m.bienias/skrypty/mail_reporter/Kwanty_bez_eng.py

... this ultimately allows you greater control of the environment and even error handling.

I would further recommend to decouple the reliance on the .bashrc and your job. The problem that can arise by having the job depend on .bashrc is that changes to .bashrc can cause the job to fail or behave incorrectly, and .bashrc is a busy file in terms of responsibilities it serves. So better to build a job-specific environment file that contains only the minimum required variables to execute the job.

Darren Smith
  • 1,966
  • 14
  • 14
  • Still python file could not get variable from ```env```, for the record script work when I trigger it by hand. – Niviral Nov 06 '19 at 09:10
  • 1
    it's possible then that your `.bashrc` has some conditional logic to detect if it is running from a console (i.e. a terminal is attached) or from crontab and behave differently in each case ... you will have to debug it. Add the command `env` just after the `source` to dump out the environment the script sees. – Darren Smith Nov 06 '19 at 09:20
  • ```LANG=en_US.UTF-8 PWD=/home/m.bienias HOME=/home/m.bienias BASH_ENV=/home/m.bienias/.bashrc SHELL=/bin/bash SHLVL=2 LOGNAME=m.bienias PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin ``` – Niviral Nov 06 '19 at 09:41
  • Ordinary executable script starts by `#!/bin/bash`, preserve it adding `#!/usr/bin/env bash` **or replace it?** – Peter Krauss Apr 26 '20 at 09:38
  • @PeterKrauss I think either way is fine, although possibly using `/usr/bin/env` is bit more portable, since `bash` is not always under `/bin` ; only relevant if you are writing scripts that have to work on any sort of system. – Darren Smith Apr 23 '22 at 09:49
0

You can create a cron job under /etc/cron.d like this:

SHELL=/usr/local/bin
PATH=/usr/local/sbin:/usr/local/bin/ <continues your PATH>
30 12,15,18 * * 1,2,3,4,5 root export VAR1=value1 && export VAR2=value2 && export VAR3=value3 && export VAR4=value4 /usr/bin/python3 /path_to_script/Kwanty_bez_eng.py

This way, I think that it prevent reload environment variables from shell profile via cron job when you will make some configuration in your shell profile.

The shell script to fire the python script, the most elegant way, can export the variables that you need although make source all .bashrc.

If you prefer create via crontab -e, like appear, it the same, just without user ahead of commands.

Leandro Arruda
  • 436
  • 6
  • 15