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_HOMEI 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 directoryi read in other forum threads that ~./bash_profile is used for define environment variables but don't have this file.
thanks
2 Answers
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/steeldriverYou have a couple of options:
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 thepythoncall inside a shell script where you can set up the environment first.define the environment as a sequence of
name = valuepairs inside the crontab likeORACLE_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 likePATH=$PATH:/whatever
See man 5 crontab for details.
Since I do not want to bother anyone with a language other than English and by suggestion in the comments, I will try to make two versions of the answer, one in English and another in Spanish. Spanish since I assume (and it seems to me correctly) is the native language of the questioner and I want to explain in a fluent way for him.
You can define the PATH variable inside your script, and export them there.
In general, cronjobs run in a shell with a very small PATH. Inside your script define the PATH variable and add what is shown when executing in the terminal echo $PATH
Or something you can do is, if you already have the necessary configuration in your ~ /.bashrc, inside your bash script use source /home/your_user/.bashrc
And after defining your PATH variable in your script that you will run, 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.pyFiles 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"
Now in Spanish.
No sé si te entendí bien pero, por lo que entendí, tienes un problema en el cual puedes ejecutar un script desde tu terminal, pero cuando tratas de ejecutarlo en un cronjob, te lanza el error descrito.
Puedes definir la variable PATH dentro de tu script, y exportarlas ahí mismo.
Por lo general los cronjobs se ejecutan en una shell con un ambiente muy reducido.
Dentro de tu script define la variable PATH y agrégale lo que se muestra al ejecutar en la terminal echo $PATH
O algo que puedes hacer es, si ya tienes la configuración necesaria en tu ~/.bashrc, dentro de tu script de bash usa source /home/tu_usuario/.bashrc
Y después de definir tu variable PATH en tu script que correras, define las variables que pusiste en tu caso, es decir, te tendría que quedar algo así
#!/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 tu_programa.pyLos archivos como .bashrc y .bash_profile, son archivos que se leen cuando inicias una terminal, pero en el caso de un cronjob, estos no se leen (a menos que lo permitas con el source /home/tu_usuario/.bashrc). El fin de estos archivos es que, antes de estar dentro de una terminal, tengas las configuraciones establecidas como variables de entorno, alias, etc. En un script que se corre dentro de un cronjob puedes hacer lo mismo, declarar las variables a mano o cargarlas con "source".
7