Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

SOLVED

I'm trying to make a shell script on my Linux Amazon-EC2 server that will start my Minecraft server but the cd command says it can't find the directory or file when I try to change to the directory the server executable is in. Please note I have next to no experience with shell script of any kind. Does it have to with permissions in the folder this file is located in or the file itself? The file is named "startServer".

#!/bin/bash
cd /home/ec2-user/Minecraft
java -Xmx2048M -Xms2048M -jar minecraft_server.jar nogui

*I did not save it with a file extension. Should I? Does it need to be?

share|improve this question

migrated from stackoverflow.com May 1 '12 at 1:46

This question came from our site for professional and enthusiast programmers.

1  
whats the output of: ls -l ~ –  sashang Apr 27 '12 at 20:09
    
Is there a MineCraft directory inside your home directory? That's what sashang is getting at. –  larsks Apr 27 '12 at 20:10
    
I copy and pasted this so I don't give you a lack of info: total 4 drwxrwxr-x 3 ec2-user ec2-user 4096 Apr 27 00:30 Minecraft –  user1251172 Apr 27 '12 at 20:11
    
Yes the the directory "Minecraft" does it exist. –  user1251172 Apr 27 '12 at 20:13
    
are you sure it's the cd command that's not working and not your java command complaining about a missing minecraft_server.jar. It would help if you posted the exact text of the error instead of describing it. –  sashang Apr 27 '12 at 20:17

3 Answers 3

up vote 2 down vote accepted

In order to make a script runnable you need to modify the permissions on the script so that it is executable.

The command to change permissions on Linux or UNIX is chmod.

chmod +x ./name_of_your_script

Will grant the current user permission to execute the script.

The #! at the start of your script indicates to the interactive shell that the script is to be interpreted by /bin/bash so you do not really need add an extention to the file name. However, it is a common convention to append .sh to shell scripts.

share|improve this answer
    
should I use script or the name of the file? –  user1251172 Apr 27 '12 at 20:26
    
chmod: cannot access `./script\r': No such file or directory : No such file or directoryome/ec2-user/Minecraft Unable to access jarfile minecraft_server.jar –  user1251172 Apr 27 '12 at 20:28
3  
It looks like you might be confusing the linux system with extraneous carriage return characters - it's very surprising that those are showing up inside the quoted portion of the error. There are tools to clean that up, but can you just re-enter the script by hand using vi (or whatever) running on the linux system itself? –  Chris Stratton Apr 27 '12 at 20:48
2  
There is a difference between how lines in a file are terminated on Windows and Linux. That's why you are getting the carriage return character in there. If you want to continur using WinVi you could check the settings of the editor so that you saved the file as a UNIX file with UNIX line termination. I'll look that up. –  Rob Apr 27 '12 at 20:49
1  
Yes! Thank you! I made a new file changed to UNIX settings and rewrote and it works just fine. I'd like to apologize to everyone I didnt mention the WinVI editing before, thank you Rob. –  user1251172 Apr 27 '12 at 20:58

The script will need execute permissions to be able to run it; to do this, cd to the directory is stored and use:

chmod u+rwx startServer 

I don't think this is your problem though. Try using the full path to the minecraft_server.jar file rather than using cd:

#!/bin/bash
java -Xmx2048M -Xms2048M -jar /full/path/to/Minecraft/minecraft_server.jar nogui
share|improve this answer
    
I tried that earlier and it worked but it ran the jar in the directory the shell script was in and I want the jar to run in the Minecraft directory. –  user1251172 Apr 27 '12 at 20:25
    
I changed permissions still doesnt work maybe I'm reading your instructions incorrectly –  user1251172 Apr 27 '12 at 20:35
    
How about changing where you cd to: cd /fullpath/to/Minecraft –  John Apr 27 '12 at 20:41
    
tried it, same error –  user1251172 Apr 27 '12 at 20:44
    
copy the cd command from your script and run it in the terminal. Does it give the same error? If so, the directory doesn't exist. I know it sounds stupid but is it definitely Minecraft and not minecraft? –  John Apr 27 '12 at 20:46

What exactly is the error message? My guess is that it is:

cd: /Minecraft: No such file or directory

This will happen if HOME is undefined, and ~ expands to the empty string.

share|improve this answer
    
: No such file or directoryome/ec2-user/Minecraft Unable to access jarfile minecraft_server.jar –  user1251172 Apr 27 '12 at 20:23
    
Try using the full path instead of the tilda, like cd /home/user/Minecraft. –  gpojd Apr 27 '12 at 20:33
    
It looks like $HOME is not set properly. As gpojd suggests, stop using the tilde. –  William Pursell Apr 27 '12 at 20:37
    
Didn't work same error: : No such file or directoryme/ec2-user/Minecraft Unable to access jarfile minecraft_server.jar –  user1251172 Apr 27 '12 at 20:38
    
cd .. doesn't work either –  user1251172 Apr 27 '12 at 20:40

Your Answer

 
discard

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