0

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
4
  • 3
    What does "not working" look like? What is the failure pattern? What does the system say when you attempt to run with the absolute path? Commented Oct 20, 2017 at 16:04
  • 1
    If the script is written with assumptions about its current working directory, that's arguably a bug/limitation in the script itself, not an issue with how you're calling it. But if you wanted to work around that -- cd /root/etlegacy && exec /usr/bin/screen -d -m -S etserver /root/etlegacy/etlded, or whatnot. Commented Oct 20, 2017 at 16:05
  • 1
    (That said, if your goal here is to invoke a long-running service, consider using a proper process supervision system such as systemd instead; that will take care of starting the process on boot, restarting it if it fails [if you want that], ensuring that logs are archived on disk, etc.) Commented Oct 20, 2017 at 16:07
  • In fact I don't receive any statement from system (The same as I would run It by ./start.sh). It looks like script was executed properly but when I look for "screen" to detach there is no one. Commented Oct 20, 2017 at 16:24

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

A few notes: First, if the cd fails (or in your case the pushd), it's generally a good idea to abort, not to run the rest of your code in the wrong directory. Thus, cd ... || exit or cd ... && thing-that-should-be-run-in-other-directory is best-practice. See also BashPitfalls #19.
Second: I'd strongly advise avoiding pushd here. Running popd at the end serves no purpose (your script's working directory disappears with the rest of its state when it exits), but prevents the shell from being able to exec the copy of screen (thus letting its PID be replaced, signals be directly delivered, etc). Moreover, the use of pushd and popd is presently the only thing that makes this script outside of baseline POSIX sh (and thus able to run with any compliant #!/bin/sh).
This solution make script working. So I can omit the popd at the end of script?
it depends on how you invoke it. If you do bash /path/of/script or just /path/of/script you can even replace the pushd with an ordinary cd and remove the popd, but if you invoke it in the same shell, such as source /path/of/script or . /path/of/script leaving it with pushd/popd will prevent the current directory of the calling shell to be changed.
0

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

4 Comments

Sorry I've made a mistake now update etlded is name of executable program. The directory is /root/etlegacy
@jadupl Can you update your question? Please copy-paste actual output straight from a terminal, don't try to type out your problem by hand. Also copy-paste all errors along with it.
That was the only one mistake in question. As I mentioned in comments above I don't receive any statement from system (script is simple just should open screen with executed program etlded) There is no return statement after running successfully either. When I run it by ./start.sh new screen is created and I can attach to it, but when runned by full path there is no screen created.
@jadupl Please update the question anyways. That way, when someone finds this question a year from now, they immediately see if this is their problem and can skip to the answer. Currently they have to read all the comments first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.