3

In rc.local I added a line for my shell file I want to run on boot:

/direc/tory/<fileName>.sh

The script looks like this:

#!bin/sh
wget "http://somesite.org/someJava.jar" -O /direc/tory/someJavaFile.jar
java -d64 -Xincgc -Xmx512M -jar /direc/tory/someJavaFile.jar

and I would assume it would run and load in the /direc/tory/ folder but it keeps running in / and it saves all its files in there as well.

What do I have to do to force it to run in the /direc/tory/ folder?

1
  • Why would you assume that?
    – geekosaur
    May 11, 2012 at 21:05

2 Answers 2

4

Your script doesn't contain any command to set the current directory, so it runs in the same directory as the process that invoked it. When it's executed from rc.local, which is executed from init, the current directory is the root directory /.

Add cd /direc/tory/ to your script. By the way, note that it's #!/bin/sh (#!bin/sh happens to work here only because you're executing your script from the root directory; it's a very bad idea to rely on this.)

#!/bin/sh
set -e
cd /direc/tory
wget "http://somesite.org/someJava.jar" -O someJavaFile.jar
java -d64 -Xincgc -Xmx512M -jar someJavaFile.jar

I also added set -e in the script. This makes it stop if one of the commands fails. For example, if wget cannot download the jar, then java isn't executed.

2
  • are you saying that I should not be using the #!bin/sh in my shell script?
    – Fate
    May 11, 2012 at 21:33
  • @Fate Yes, always use #!/bin/sh (or #!/usr/bin/perl or #!/direc/tory/myinterpreter or whatever is appropriate), with a full path. May 11, 2012 at 21:35
1
#!/bin/sh
wget "http://somesite.org/someJava.jar" -O /direc/tory/someJavaFile.jar
cd /direc/tory
java -d64 -Xincgc -Xmx512M -jar /direc/tory/someJavaFile.jar
1
  • Thank you Gilles. I stumbled around trying to figure the formatting out, but nothing looked right. May 11, 2012 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .