rc.local runs .sh file in wrong directory

10,353

Solution 1

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.

Solution 2

#!/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:
10,353

Related videos on Youtube

Fate
Author by

Fate

Working as a Technical Analyst for a Major Retailer. Spend my time Gaming mostly & dabble in simple and basic coding.

Updated on September 18, 2022

Comments

  • Fate
    Fate over 1 year

    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?

  • Matt Barnes
    Matt Barnes almost 12 years
    Thank you Gilles. I stumbled around trying to figure the formatting out, but nothing looked right.
  • Fate
    Fate almost 12 years
    are you saying that I should not be using the #!bin/sh in my shell script?
  • h3.
    h3. almost 12 years
    @Fate Yes, always use #!/bin/sh (or #!/usr/bin/perl or #!/direc/tory/myinterpreter or whatever is appropriate), with a full path.