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.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
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

robsch
  • 9,358
  • 9
  • 63
  • 104
Niviral
  • 187
  • 1
  • 4
  • 14

3 Answers3

7

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
  • 2,261
  • 16
  • 16
  • 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
  • 466
  • 6
  • 16
0

If you are running a Debian/Ubuntu-like system, the per-default .bashrc content a test that prevents you to source the .bashrc in a non-interactive shell, which is the case for a cronjob.

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

Try to put your variables in a dedicated file (e.g. .bashrc_cron), and then source it through the BASH_ENV variable.

BASH_ENV=/home/m.bienias/.bashrc_cron

PS1: strictly speaking, this new file should not be called a bashrc file but a bash_profile one, since bashrc files should be loaded for interactive shells only.

PS2: if you name this new file as a bash_profile-like, call it with a prefix like .bash_profile_crontab. If you call it as a per-default .bash_profile, it might create other conflicts.

Covich
  • 2,544
  • 4
  • 26
  • 37