4

Firstly, sorry for my English.

My situation is:

  • Have a Ubuntu 18.04
  • Want to execute with crontab python & sh scripts
  • Result: /usr/lib/oracle/12.2/client64/bin/sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory

The variables i want to add are defined like this:

export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export PATH=$PATH:$ORACLE_HOME/bin
export OCI_LIB_DIR=$ORACLE_HOME/lib
export OCI_INC_DIR=/usr/include/oracle/12.2/client64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:$ORACLE_HOME

I configure the environment variable in many ways to avoid this error:

  • ~/.bashrc
  • ~/.profile
  • /etc/environment
  • Creating myvars.sh file in /etc/profile.d

The problem is when i configure the variables for my user (in ~/.profile, for example)and I execute on the terminal one python or sh script that use SQL connection, ends well and doesn't give any error. But when the python is execute through crontab, appears in the log the error:

/usr/lib/oracle/12.2/client64/bin/sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory

i read in other forum threads that ~./bash_profile is used for define environment variables but don't have this file. thanks

2 Answers 2

3

cron runs jobs in its own minimal environment and doesn't itself read any of the shell startup files such as those in /etc/profile.d/ - by default it's just something like

LANGUAGE=en_CA:en
HOME=/home/steeldriver
LOGNAME=steeldriver
PATH=/usr/bin:/bin
LANG=en_CA.UTF-8
SHELL=/bin/sh
PWD=/home/steeldriver

You have a couple of options:

  1. set the variables in a script, and run that from cron. In the case of shell scripts, that's easy (just export them at the top of the script - or source an environment file if you prefer). For Python scripts you may find it easiest to wrap the python call inside a shell script where you can set up the environment first.

  2. define the environment as a sequence of name = value pairs inside the crontab like

    ORACLE_HOME = /usr/lib/oracle/12.2/client64
    * 5 * * * /path/to/some/executable
    

    (spaces are allowed around the = since it's not a script); however note that this method does not expand variables so you can't do stuff like PATH=$PATH:/whatever

See man 5 crontab for details.

0
0

You can define the PATH variable inside your script, and export them there.

In general, cronjobs run in a shell with very few folders in PATH.

Inside your script define the PATH variable and add what is shown when executing in the terminal echo $PATH.

You can also source your /home/your_user/.bashrc.

And after defining your PATH variable in your script, also define the variables that you put in your explanation, that is, you should have something like this:

#!/bin/bash

PATH=/usr/local/bin:/usr/bin:/bin
export ORACLE_HOME=/usr/lib/oracle/12.2/client64 
export PATH=$PATH:$ORACLE_HOME/bin 
export OCI_LIB_DIR=$ORACLE_HOME/lib
OCI_INC_DIR=/usr/include/oracle/12.2/client64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib:$ORACLE_HOME

python your_program.py

Files like .bashrc and .bash_profile, are files that are read when you start a terminal, but in the case of a cronjob, these are not read (unless you allow it with the source /home/your_user/.bashrc).

The purpose of these files is that, before being inside bash, you have the configurations set as environment variables, aliases, etc. In a script that runs inside a cronjob you can do the same, declare the variables manually or load them with "source".

7
  • why don't use a translator if you don't know how to write in english?
    – damadam
    Jul 25, 2018 at 14:22
  • 1
    @damadam I assumed that Juan Carlos Arqueros Vírseda spoke Spanish (my native language), so I tried to explain myself in that language to make the answer more fluid. I'm sorry if I offended you. I think I'll explain that in the next edition.
    – Cuauhtli
    Jul 25, 2018 at 14:26
  • exactly @mrc_es ;) Jul 25, 2018 at 14:28
  • SE websites must be used in English, so everybody can understand questions/answers and can improve answers (what will you do if you see a question wrote in chinese/german/..., so a language that you don't understand? it would be really hard to have some help)
    – damadam
    Jul 25, 2018 at 14:34
  • 1

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.