Facing folder path issue in shell script - linux

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.

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.

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

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.

Run bash script using full path

Can someone explain me why my script can't be run from other directory than this one where it is created?
My script start.sh in directory /root/etlegacy/ :
#!/bin/bash
/usr/bin/screen -d -m -S etserver /root/etlegacy/etlded
Everything works fine when I am in /root/etlegacy/and run script through:
./start.sh
But It is not working when I am elsewhere in the file system, even If I am using full path to script i.e
/root/etlegacy/start.sh
There is no problem with your script as you wrote it. The problem is more likely being in /root/etlegacy/etlded which may require to be run into the /root/etlegacy directory. Try to change the code into this:
#!/bin/bash
pushd /root/etlegacy
/usr/bin/screen -d -m -S etserver /root/etlegacy/etlded
popd
Everything works fine when I am in /root/etlegacy/etlded [...] and run script through: ./start.sh
...
But It is not working when [...] I am using full path to script i.e /root/etlegacy/start.sh
Well, there's your problem. It sounds like the full path of your script is actually:
/root/etlegacy/etlded/start.sh

Execute bash script from one into another directory?

I have 3 directories:
/A/B/C and 1 bash script in C directory.
How can I execute this bash script from A into in C directory.
I understand from your comments that you want your script to have its current working directory to be in A/B/C when it executes. Ok, so you go into directory A:
cd A
and then execute script.sh from there:
(cd B/C; ./script.sh)
What this does is start a subshell in which you first change to the directory you want your script to execute in and then executes the script. Putting it as a subshell prevents it from messing up the current directory of your interactive session.
If it is a long running script that you want to keep in the background you can also just add & at the end like any other command.
Whenever I want to make sure that a script can access files in its local folder, I throw this in near the top of the script:
# Ensure working directory is local to this script
cd "$(dirname "$0")"
It sounds like this is exactly what you're looking for!
Assuming /A/B/C/script.sh is the script, if you're in /A, then you'd type ./B/C/script.sh or bash B/C/script.sh or a full path, /A/B/C/script.sh. If what you mean is that you want that script always to work, then you'll want to add it to your PATH variable.
Go to A, then run your script by providing the path to it:
cd /A
bash B/C/somescript.sh
You could also add C to your PATH variable, making it runnable from anywhere
(i.e. somescript.sh, without the path)
If you want to access the directory the script is stored in, within the script, you can use this one-liner:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
or simply dirname. Taken from this thread. There are many more suggestions there.
The easy way to make your script execute within C is to simply be in that folder.

Execute Script using an Alias

I am trying to create an alias that will execute a script. When i cd into the directory where the script is located... lets say /usr/local/bin/startscript then the script runs as expected and starts the application i want it to.
SO. i went into my bashrc file and added an alias
alias startscript='/usr/local/bin/startscript'
The goal is to be able to run the script by simply typing "startscript" from any directory.
However, when i try to use the alias to run the script, it does not work properly as the application that should start, does not.
My script starts with
#!/bin/sh
and then goes from there
any ideas? Thanks
SCRIPT:
#!/bin/sh
#- Check for user 'user'
if [[ "`whoami`" != "user" ]]; then
echo "This script can only be executed by user 'user'."; exit
fi
. /usr/local/bin/etctrx/startscriptdirectory/startscriptsetup
#- Kill manager to avoid multiple processes
pkill -f 'JavaApp.jar'
#- Start
nohup java -classpath /usr/local/bin/etctrx/startscriptdirectory/RequiredJars/ojdbc5.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/activation.jar:/usr/local/bin/etctrx/startscriptdirectory/RequiredJars/mail.jar -jar /usr/local/bin/etctrx/startscriptdirectory/JavaApp.jar > ${JAVAAPPLOGS}/startscript.log 2>&1 &
If the script runs as expected while in /usr/local/bin by simply typing startscript, but from another directory the script runs (does not return an error), but doesn't produce the desired results, then the issue is with how you reference the application from within the script.
As others have noted, you shouldn't need an alias for something in /usr/local/bin and if it runs from that directory, obviously your executable permissions are correct too. If the application you're trying to run is also in /usr/local/bin then your script probably assumes it's in the same directory, which wouldn't be the case elsewhere, so you would need to either ad a cd to /usr/local/bin within the script or specify the full application path.
I am able to call the script if i do this, but it still won't give me the
results I want,(application being started) like i do when I run the script from
the directory it lives in
It would appear that the "application" in question is in the same directory as the script, /usr/local/bin, which we have established is already on your PATH. For the script to run correctly but not the application means you might be calling the application wrong, for example
./application
This would fail unless you were calling from /usr/local/bin. Fix would be like this
application

Resources