Why doesn't my crontab job find the correct file path? - cron

echo "Enter base root directory ..."
cd ./base
export PYTHONPATH=`pwd`
echo "Downloading resources ..."
python3 ./util/fetch.py -o ./ downloadResources
echo "Exit from base and enter the upper level directory ..."
cd ..
python3 ./test/data_poster.py
echo "done ..."
I have a crontab job as listed above(auto_run.sh). What it does:
In the root directory of the current project, to enter the base/ subdirectory
under base/, it download some resources files
The it exited from the base/ directory and in the root directory, to run 'data_poster.py'
The problem occurs. In the log of the crontab, it complains that:
python3: can't open file './util/fetch.py': [Errno 2] No such file or directory
python3: can't open file './test/data_poster.py
Why can't the cronjob execute the python script in both cases? The path to the two scripts are right. If I just run 'sh auto_run.sh' without using cronjob, it works all right.
So what's the problem?

You must explicitly set up your environment in the crontab script. That's because cron runs from a non-interactive, non-login shell.
When your cron script runs cd ./base, there is no "." (current directory) as there was no login shell to set one. Use full paths.
See This question on stackexchange for more info.

Related

Run bash script in current directory Linux

Why can't I run a bash script in the current directory I'm in?
Whenever I run the script the commands are executed in the home directory.
The only answers I found are included below.
I do use the zsh shell. I don't know if that changes anything.
Thanks in advance!
What I have tried so far:
#!/bin/bash
touch test.txt
#!/bin/bash
cd $PWD
touch test.txt
#!/bin/bash
variable = $PWD
cd $variable
touch test.txt
#!/bin/bash
variable= pwd
cd $variable
touch test.txt
#!/bin/bash
cd -
touch test.txt
If I run the script for example from /home/user/dir1/dir1.1 the test.txt file is created in the home directory (/home/user) and I get redirected to the home directory as well.
in bash there are two things to do:
ensure that the shell script file is saved properly and is chmod'd to be an executable.
To do so, save the file (e.g. script.sh) with the code you want, and then run chmod +x script.sh to make linux understand that this file is an executable.
call the executable properly using the ./script.sh command. alternatively, you can also call the script from remote folder by calling it using the absolute path the script is in (e.g. /folder/folder/folder/script.sh).
This should execute the file. from there, it's about your code and if you need help there, please update your question.

Facing folder path issue in shell script

I have written one script which is working fine when I execute it by ./script.sh.
Script path: /var/script.sh.
I need to do some action in /var/project. For that, I have written this script.
cd /
cd /var/project
echo "In folder..."
Now I need to run this file using sudo crontab -e.
Crontab -e code:
#reboot /var/script
I have also echo some message and it prints fine. But as in my above code,
it is not going to my path. It goes to this path.
/home/myuser
Instead of that path I need to go
/var/project
Thanks for the help. :)
Root will use /home because it's default path of root.
Give you default path before script executes.
add this code at top of the script.
cd /
cd /var
Now it'll use you path.

Jenkins builds failing with a missing file error even though the file is there

I have a Jenkins server running and have a couple of builds that are all working. However, this morning when I tried to add another one it kept failing with the error -
[test.gov] $ /bin/sh -xe /tmp/hudson7055290339554583413.sh
+ ./opt/jenkins/build.sh
/tmp/hudson7055290339554583413.sh: line 2: /opt/jenkins/build.sh: No such file or directory
Build step 'Execute shell' marked build as failure
The shell script is at that path and has that name. If I make it ./opt/jenkins/build.sh it still fails with the same error. The command I have in the "Execute Shell" section in the "Command" text box is /opt/jenkins/build.sh. I tried ./opt/jenkins/build.sh as well.
The script is on a CentOS system btw.
This is the script I am trying to run -
echo "git pull"
cd /var/www/path/for/my_website/docroot/
git checkout master
git pull
echo "change ownership to apache"
chown -R apache:apache *
echo "running drush commands"
drush updb -y
#drush fra -y
drush cc all
Often the "build.sh: No such file or directory" error actually means that the shebang line within build.sh does not point to an executable file. Check the contents of build.sh to make sure that path exists.
Sometimes for your path in the shell command, a "." may not work. Instead, use the macro expression to get the workspace.
So for example: %WORKSPACE%/opt/jenkins/usda_gov_build.sh
This will give the shell command the exact path of the file in your jobs workspace.

Shell script file (.sh) does not run, and throws an error

I am new to linux, and I have a shell script (.sh) file on my Desktop that I want to run.
These are the steps that I did:
This is the content of the test.sh file on my Desktop:
#!bin/bash
#test.sh
echo "test"
I want to run (Execute) test.sh through the terminal. These are the commands that I'm using:
cd Desktop
I give permission to run test.sh with:
chmod +x test.sh
and then try to open the file:
test.sh
But I get this error:
test.sh: command not found
What am I doing wrong?
Your shell will search the directories in your $PATH environment variable for executable files.
If the current directory is not in it (and your Desktop directory won't be, by default), you must specify the path explicitly.
./test.sh
bash: ./test.sh: bin/bash: bad interpreter: No such file or directory
Replace:
#!bin/bash
With:
#!/bin/bash
Source
try running the script like this: ./test.sh
Do like this:
FIRST WAY:
bash test.sh
or
sh test.sh

How do I execute an arbitrary script with a working directory of the directory its in?

I need to execute a groovy script file from bash, and I need the script to have a working directory of the directory it exists in.
That is, in my bash script, I'm doing this:
/opt/script/myscript.groovy &
But this seems to set the working directory to /etc/init.d, the directory I'm calling from. How do I change the working directory for that script to /opt/script?
If you are using start-stop-daemon inside your /etc/init.d script, you can take advantage of the -d parameter for achieving this:
-d, --chdir path
Chdir to path before starting the process. This is done after the chroot if the -r|--chroot option is set. When not specified, start-stop-daemon will chdir to the root directory before starting the process.
/etc/init.d
probably you are runnig (starting) that script from /etc/init.d?
Add cd /opt/script at the first line of the script
OR
...to keep it dynamic, add:
cd "$(dirname "$0")"
In bash putting that in the script works best:
HERE=$(cd -- $(dirname ${BASH_SOURCE[0]}) > /dev/null && pwd)
cd -- "$HERE"
This will succeed even with the following invocation (of /path/to/script.sh):
PATH="/path/to:$PATH" bash script.sh
where HERE=$(dirname $0) would fail.
Optionally you could also use pwd -P instead of just pwd, then $HERE will contain the realpath (canonicalized absolute pathname) as of man 3 realpath.
Something like this maybe:
SCRIPT=/opt/script/myscript.groovy
pushd `dirname $SCRIPT`
./`basename $SCRIPT`
popd

Resources