I have a script done in Bash which runs a file on the folder where the script is located, passes a few config files and outputs a log on the same folder. For example this would be the structure:
/home/user/application
And inside we have the following files:
script.sh # this would be the script to launch
executablefile # this would be the file to run, it's called by the script as "./executable", since it's not in $PATH
config.json # a config file for the executable that is passed by the script using arguments
logfile # A log file written by the script, redirecting all Standard and Error output to the log file using tee
So if I CD into the folder and run the script, all is OK. But if I call this script from another location, it cannot locate the executable file and would most likely search for the config and output the log file on the location it was called from. This is not what I want.
So what changes could I write to the sh script. Something that establishes the script directory as the working directory when the script starts?
My main idea is to write a systemd service file which will call this script in the future.
Thank you.
/path/to/script.sh
orbash /path/to/script.sh
? – terdon♦ Apr 18 '20 at 17:17/path/to/script.sh
I assume I would run it the same way from systemd. I think I found this solution:cd "${0%/*}"
– alb2001 Apr 18 '20 at 17:51