I have a simple bash script that allows cron
to execute a series of Python scripts in a virtualenv
. The script keeps raising No such file or directory
errors.
~/nightly.sh
works fine:
#!/bin/bash
source virt_env/myproject/bin/activate
cd virt_env/myproject/main
python script1.py
python script2.py
I want to keep everything in ~/virt_env/myproject/main/
to simplify deployment. I thought I could call bash virt_env/myproject/main/nightly.sh
on this:
#!/bin/bash
MAINDIR=`dirname $0`
cd $MAINDIR
source ../bin/activate
python script1.py
python script2.py
but I get the No such file or directory
. If I manually cd
to ~/virt_env/myproject/main/
, then I can run the main commands no problem. Clearly I'm missing something about how dirname
and cd
work in this context.
How can I point bash at the right place?
As proposed in the accepted answer, it's best to avoid calling cd
from within the script and use an explicit path variable instead. Here's the working version of virt_env/myproject/main/nightly.sh
:
#!/bin/bash
MAINDIR=`dirname $0`
echo "The main directory is" $MAINDIR
# Activate virtual environment
source $MAINDIR/../bin/activate
# Run Python scripts
python $MAINDIR/python1.py
python $MAINDIR/python2.py
Because the Python scripts are now called from an arbitrary path, I needed to update the python scripts to be smarter about path awareness as well.
This code fails because os.path.basename
omits path information:
# works when called with "python python1.py"
# fails when called with "python $MAINDIR/python1.py"
CONFIG_FILE = os.path.basename(__file__)[:-3] + ".config"
f = open(CONFIG_FILE,"r")
Updating it to use os.path.abspath
fixes the problem:
# works regardless of how it is called
CONFIG_FILE = os.path.abspath(__file__)[:-3] + ".config"
f = open(CONFIG_FILE,"r")
Perhaps it would simply be better to eliminate the 'cd' command. Invoke everything from a full path specification. In your example add $MAINDIR/ to the executables.
Your bash script can then be in any directory where the executables are reachable. You are not exposed to the problems of what happens when cd fails.
Example:
cd yourdir
rm -f yourglob # oops things got removed from where you started if yourdir did not exist.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments