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
Related
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.
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.
I am building project source code in a SUSE server.
The project build.sh called "lzma" command to compress kernel.
The project build.sh need "sudo" to get access to some system command.
But I has tried to execute "sudo ./build.sh", and the shell always report error: "lzma: command not found."
I could execute "lzma" in shell with my user account. It works fine.
I also write a test shell script named "test.sh" which calls "lzma" command.
I found that it fails with same error message if I excute "test.sh" with "sudo" .
But if I execute "test.sh" without "sudo", it works fine.
Why ?
"Command not found" within sudo is almost invariably the result of an environment variable such as PATH, LD_LIBRARY_PATH (if what's missing is not the executable but a shared library it requires) or the like being altered.
You can pass working values through your environment variables through explicitly:
sudo PATH="$PATH" ./test.sh
Sudo uses a different Path then your user account.
EDIT (see comments)
Try and execute:
type lzma
Say the output reads something like '/usr/bin/lzma', then just copy that output into your sudo command like (for example):
sudo /usr/bin/lzma
That should do the trick. You should also write the full path of lzma into your shell script if you are to run it as root.
EDIT 2:
Or, as Charles Duffy mentioned in his answer, you could leave all things as is and simply use PATH="$PATH" in your command if you are trying to execute your file as SUDO or as a different user.
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
i have very simple shell script
#!/bin/bash
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
echo "----"
echo "done"
but seems cp command fails
if i execute
cp -rf /var/www/ksite/app2/* /var/www/ksite/app
from terminal everything work ok. Can someone tell me how to include cp in shell script?
Thanks
We seem to have doubt as to how this script fails. If there is no error message then this is a strange one. I suggest:
On the command line (which works), do a which cp
Whatever the reply, then copy that and use it as the cp in the script (e.g. /bin/cp)
Check the widcard expansion, run your script with bash -x script-name and see if you get what you expect.
echo $? after the copy in the script - if it is zero then it (thinks it) worked.
Do a ls -ld /var/www/ksite/app from your script, maybe someone set a symbolic link?
If it still fails, source the script from the command-line and see if that works . script-name
Double check that the copy did actually fail! (maybe that should be step 1.)
Make sure you really have bash at /bin/bash. I think a batter hash bang is:
#!/usr/bin/env bash
This uses the env command to locate the bash binary and set the environment.
I had similar problem. What helped me:
I used windows and putty to write script, so I had \r\n at the end of lines. Be sure, you have only \n symbol.
I copied files and the only way it worked for me at script was cp <source_dir>/fileName <dest_dir>/fileName whereas at command line cp <source_dir>/fileName <dest_dir> worked well too.
Just covering all the bases .. do the permissions vary between the excutions .. i.e. do you execute one with sudo/root privileges, the other as user (unlikely, but thought I'd ask since we don't know what the exact error is)
Similar issue to Vladmir where the script was created in Windows. I created a new file "my_bash_script.sh" in the linux environment using VIM, then read the contents of my script into the file:
:r file_made_in_windows.sh
Then I saved, closed, then set the file as executable:
chmod 744 my_bash_script.sh
From there, I ran the script:
./my_bash_script.sh
...and it worked. What a weird issue. I was confounded for a moment.