I am trying to create a shell script that executes some python scripts in order. I have never done something like this and i don´t know if what i am doing is correct. Before executing the last file i need to activate an enviroment so it has the necesary imports to so it functions correctly. This is the script i have created (execute.sh):
#!/bin/sh
python path/file_1.py
python path/file_2.py
python path/file_3.py
# activate a virtual enviroment
source /path/env/bin/activate
python path/file_4.py
After this what sould I do? Just write this line in the terminal?
./execute.sh
CodePudding user response:
That should work. Usually I always do this when running a bash script with Python scripts inside.
#!/bin/bash
. /home/user/.bashrc # Load basic env as we're coming from cron
# Cd into our working directory in case we're not into it already
cd "$(dirname "$0")";
echo "----------------"
echo "Starting processing `date`"
echo "----------------"
conda activate models # Activate my conda environment
# Execute Python script
python script.py
if [[ $? = 0 ]]; then
echo "Succesfull execution!"
else
echo "Something wrong"
exit 1
fi
This is in execute.run, which you then make executable chmod x executable.run
, and run. This will work wherever you are because it will always try to cd into the directory where the script resides, so you can also call it from crontab.