I'm running a Java server program that needs to be started with a shell script. The script needs to start the Java program again if it happens to crash or be stopped manually.

I found this script online for it

while true
   java -jar program.jar

But that didn't seem to work on my Debian 7 box; it just shows:

./start.sh: line 4: syntax error: unexpected end of file

That was just posted as a "Linux" script. How can I make it work in Debian? Oh yeah and it's important for the program to be started from the script because I need to access its console regularly, unless there's a way for it to not be a script but still let me access its console.

  • Perhaps your script has carriage-return line-endings (happens a lot with cut/paste). – Thomas Dickey Sep 24 '16 at 13:42

That is a perfectly valid script. It would work fine anywhere. If you're getting a syntax error, it's because either there was a syntax error introduced when you copy/pasted the script, or as Thomas theorized in the comments your file is using incorrect line terminators.

You can check line terminators using the od command, like this:

$ od -a foo.sh 
0000000   w   h   i   l   e  sp   t   r   u   e  nl   d   o  nl  sp  sp
0000020  sp   j   a   v   a  sp   -   j   a   r  sp   p   r   o   g   r
0000040   a   m   .   j   a   r  nl   d   o   n   e  nl

You can see here the script on my system is using newlines (that's the nl in the output, just after, t r u e, for example). If instead you see just carriage returns:

0000000   w   h   i   l   e  sp   t   r   u   e  cr   d   o  cr  sp  sp

Or carriage return/newline combinations:

0000000   w   h   i   l   e  sp   t   r   u   e  cr  nl   d   o  cr  nl

Then you will need to fix that.

Once you get things working, you may want to consider adding a sleep command to your script, like this:

while true
   java -jar program.jar
   sleep 1

In the event that program.jar crashes immediately, the tight loop with no pauses can consume a lot of CPU. Introducing a small pause largely resolves that particular concern.

|improve this answer|||||
  • I have no idea what line terminators or anything you've pasted there means – Tundral Sep 24 '16 at 19:26
  • Well, you can run the commands I suggested, look at the output and see if it matches the descriptions, and leave a comment here with the results, and we'll work from there. – larsks Sep 24 '16 at 19:39
  • I used the commands in the comment below and it started woking, thanks for your efforts anyway – Tundral Sep 24 '16 at 19:52

larsks' answer is both well written and extensive. Upvote has been given accordingly.

Just a quick note as to:

Then you will need to fix that.

There are a myriad ways to convert Windows CR to Linux/Unix LF. Here are a few:
Using tr:

tr -d '\r' < infile > outfile

Using sed:

sed 's/^M$//' infile > outfile

Lastly, since you are on Debian, you may have a package called dos2unix available. If you install it, it is as simple as:

dos2unix myfile
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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