Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
Why would you assume that? –  geekosaur May 11 '12 at 21:05

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
are you saying that I should not be using the #!bin/sh in my shell script? –  Fate May 11 '12 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. –  Gilles May 11 '12 at 21:35
#!/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
share|improve this answer
    
Thank you Gilles. I stumbled around trying to figure the formatting out, but nothing looked right. –  Matt Barnes May 11 '12 at 21:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.