LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2013, 11:04 PM   #1
MCgamer
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Rep: Reputation: Disabled
Trying to make a .sh file to loop a java startup with no luck...


I am trying to create a loop where if my Java process isn't running it will start back up. It works when I paste the commands directly into the shell but not when running from the .sh file. I have tried:

#!/bin/bash
while true; do
java -Xms6G -Xmx6G -jar /root/bukkit/craftbukkit.jar
done

and

#!/bin/bash
while true; do
java -Xms6G -Xmx6G -jar craftbukkit.jar
done

Saying different things every time I change it. Unexpected end of file, expected do, or expected done. Sometimes said cannot access the jar, or : not found start.sh

The .sh is in the same directory as the jar that I am trying to loop. My machine has enough memory to allocate 6GB to this. Works fine just not in a script. I've tried a lot of things; adding another 'done' to the end, adding #!/usr/bin/env python instead, chmod +x, putting the 'do' on the next line, etc. Could use some advice. Thanks in advance.

Last edited by MCgamer; 04-24-2013 at 11:06 PM.
 
Old 04-24-2013, 11:28 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
What's need of use of while condition when you can simply invoke the script with only java cmd in it, like:
Code:
#!/bin/bash 
java -Xms6G -Xmx6G -jar /root/bukkit/craftbukkit.jar
if [[ $? -eq 0 ]]; then
echo "Command run successfully".
else
echo "Command didn't run successfully".
fi
Also invoke you script with set -xv debugging option to check where it's going wrong:
Code:
#!/bin/bash
set -xv
while true; do 
java -Xms6G -Xmx6G -jar /root/bukkit/craftbukkit.jar
done
EDIT: Before invoking the java command, your script is not checking whether the process is already running or not. So add this condition also.

Last edited by shivaa; 04-24-2013 at 11:30 PM.
 
Old 04-24-2013, 11:49 PM   #3
MCgamer
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Those run with one of the same errors I had:
unable to access jarfile /root/bukkit/craftbukkit.jar.
start.sh: line 7: syntax error near unexpected token 'fi'

I don't know why it is telling me syntax error, cannot access, etc when from what I can tell there should be no problems. The reason I am using 'while' is because I never want the program to stop executing. Again, this works fine when pasting my script into the shell manually but I can't get it to execute from a script.
 
Old 04-25-2013, 12:04 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Are you sure that file /root/bukkit/craftbukkit.jar is there?
Code:
~$ ls -l /root/bukkit/craftbukkit.jar
Also make sure that you've bash shell in your system and it's under /bin/bash. You can also check by changing the shebang to /bin/sh.
Code:
#!/bin/sh 
java -Xms6G -Xmx6G -jar /root/bukkit/craftbukkit.jar
if [ $? -eq 0 ]; then
echo "Command run successfully".
else
echo "Command didn't run successfully".
fi
 
Old 04-25-2013, 02:33 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,294

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
Try the 2nd soln in post #2 and show the exact code used and msgs seen.
 
Old 04-25-2013, 09:24 AM   #6
MCgamer
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
ls -l /root/bukkit/craftbukkit.jar finds the file, it is there.
/bin/bash exists as does /bin/sh .

Using the second solution in the second post in a file called start.sh with the

following:

#!/bin/bash
set -xv
while true; do
java -Xms6G -Xmx6G -jar /root/bukkit/craftbukkit.jar
done

Gives me this error in an endless loop when running "bash start.sh":

Error: Unable to access jarfile /root/bukkit/craftbukkit.jar
start.sh: line 3: $'\r': command not found
Error: Unable to access jarfile /root/bukkit/craftbukkit.jar
start.sh: line 3: $'\r': command not found
Error: Unable to access jarfile /root/bukkit/craftbukkit.jar
start.sh: line 3: $'\r': command not found
Error: Unable to access jarfile /root/bukkit/craftbukkit.jar
and so on.......

and using:
"sh start.sh"
gives me:
start.sh: 2: set: Illegal option -
root@ubuntu:~/bukkit#

I am creating the file in notepad and placing in my linux /root/bukkit directory via ftp each time.
 
Old 04-25-2013, 09:35 AM   #7
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
First thing is to not use while loop, because condition is always true and loop will run infinitely.

Second, it looks some issue with your craftbukkit.jar file, so better put the file in some other folder, say /path/to/craftbukkit.jar, make change in script as follow, and then invoke script again:
Code:
#!/bin/bash
java -Xms6G -Xmx6G -jar /path/to/craftbukkit.jar
if [[ $? -eq 0 ]]; then
echo "Command run successfully".
else
echo "Command didn't run successfully".
fi

Last edited by shivaa; 04-25-2013 at 09:36 AM.
 
Old 04-25-2013, 10:09 AM   #8
MCgamer
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Same errors as the first time when we ran with the full path. I just don't understand why I can paste the same exact commands and they run fine when it won't run from a script file which is doing the same thing. The while loop I had originally wanted to use works fine when simply pasted. It starts right up and when I issue 'stop' in the program it woill quit and start back up due to the loop which is the behavior I am looking to get from the script. I just can't get a file to convey these commands to the shell without me having to manually enter them.
 
Old 04-25-2013, 10:28 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Can you post output of script when you run the script mentioned in post #7 ? And which is your Linux distro and its version?
 
Old 04-25-2013, 10:34 AM   #10
MCgamer
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
unable to access jarfile /root/test/craftbukkit.jar
start.sh: line 7: syntax error near unexpected token 'fi'

I'm running Ubuntu server 3.2.0-40-generic #64. I have also noticed now that there are color codes for the files when using ls. my craftbukkit.jar is red, does this just signify that it is an executable rather than a regular file or folder?

here is ls -l for craftbukkit:
-rw-r--r-- 1 root root 12958013 Apr 24 13:48 craftbukkit.jar
 
Old 04-25-2013, 11:29 AM   #11
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Possible reasons could be:
- A corrupt craftbukkit.jar file.
- File permissions/ownership issue.

So try to put the file is some other folder and then run script. Or change the owner (user:user)/permission (755) of the file and then invoke the script again.

On the other hand, to avoid syntax error issue, try /bin/sh only as shebang.
 
Old 04-25-2013, 01:12 PM   #12
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
Quote:
start.sh: line 3: $'\r': command not found
[...]
I am creating the file in notepad [from Windows]
You can not use Windows "notepad" to write linux scripts. You need an editor which can write proper line endings (for example PsPad or Notepad++), wchich is LF in Linux and CR-LF on Windows and write script using LF (another called "\n") end of line character. Or after copying script by ftp, use a command on Linux to convert it:
Code:
dos2unix your_script_file.sh
Or use Linux editor to edit scripts, try "mc-edit" or "nano".

I do not recommend to start craftbukkit in infinite loop. When server goes down, your craftbukkit will be forced to close and you may lost your worlds or players inventories. If you want to rerun it after crash, check it status code. "0" means that minecraft server was closed properly. I write on one of the threads how I handle server, by hand, but it can be using from init.d to start and gracefully close automatically.
 
Old 04-25-2013, 01:49 PM   #13
MCgamer
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks for the advice and for the link to your thread. That helped me a lot. Problem solved. Thanks for your help guys.
 
Old 04-25-2013, 08:03 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,294

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
Having seen your response to my last, I was going to suggest that also.
Basically, save yourself a lot of grief/messing about and get used to editing program files on Linux directly.
 
Old 04-25-2013, 11:50 PM   #15
MCgamer
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yeah, I was apprehensive about editing in Linux shell because it's so easy to just use the old familiar gui, and since I really didn't like vi. I used pico this time and it actually worked very well. I'm only two days into learning Linux but I am actually having a lot of fun despite the difficulties. It's great to have a helpful and knowledgeable community here to help troubleshoot.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 08:47 PM
Annoying DHCP loop at startup orangesky Linux - Newbie 3 02-09-2010 09:23 PM
looking for a RH file with no luck rcallahan Linux - Newbie 1 02-01-2005 05:22 PM


All times are GMT -5. The time now is 02:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration