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.

Just a minor issue. I'm writing a simple bash script that starts and stops Jetty. When I execute it, the script immediately puts my cursor back on the bash prompt. However, as Jetty starts up and writes its initialization output back to stdout, it leaves the cursor on a line of its own (without a prompt) until I enter a command or hit enter. Nitpicking, I know, but I figure there's an easy way to avoid this that I'm missing.

Here's the script:

#!/bin/bash
cd /opt/jetty/jetty-distribution-7.4.5.v20110725/
if [ "$1" = "-stop" ]
then
    java -DSTOP.PORT=8079 -DSTOP.KEY=something -jar start.jar --stop
else
    java -DSTOP.PORT=8079 -DSTOP.KEY=something -jar start.jar &
fi

Here's the output:

[user@machine ~]# jetty
[user@machine ~]# 2011-08-11 14:47:34.818:INFO::jetty-7.4.5.v20110725
2011-08-11 14:47:34.866:INFO::Deployment monitor /opt/jetty/jetty-distribution-7.4.5.v20110725/webapps at interval 1
2011-08-11 14:47:34.878:INFO::Deployment monitor /opt/jetty/jetty-distribution-7.4.5.v20110725/contexts at interval 1
2011-08-11 14:47:34.883:INFO::Deployable added: /opt/jetty/jetty-distribution-7.4.5.v20110725/contexts/javadoc.xml
2011-08-11 14:47:34.934:INFO::started o.e.j.s.h.ContextHandler{/javadoc,file:/opt/jetty/jetty-distribution-7.4.5.v20110725/javadoc}
2011-08-11 14:47:34.935:INFO::Deployable added: /opt/jetty/jetty-distribution-7.4.5.v20110725/contexts/test.xml
2011-08-11 14:47:35.011:INFO::Extract jar:file:/opt/jetty/jetty-distribution-7.4.5.v20110725/webapps/test.war!/ to /tmp/jetty-0.0.0.0-8080-test.war-_-any-/webapp
2011-08-11 14:47:35.645:INFO::started o.e.j.w.WebAppContext{/,file:/tmp/jetty-0.0.0.0-8080-test.war-_-any-/webapp/},/opt/jetty/jetty-distribution-7.4.5.v20110725/webapps/test.war
2011-08-11 14:47:36.127:INFO:org.eclipse.jetty.servlets.TransparentProxy:TransparentProxy @ /javadoc to http://download.eclipse.org/jetty/stable-7/apidocs
2011-08-11 14:47:36.199:INFO::Started SelectChannelConnector@0.0.0.0:8080 STARTING

And the cursor ends up on a blank new line just below. Is there a way to prevent this? I'm pretty new to bash scripting, is there something I should be doing that I'm not?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

You can't prevent this easily – bash does not receive any signal at all when a background process writes to the terminal, only when it exits completely. (SIGTTOU is only delivered to the writing process, in this case Java, which doesn't have any control over when the prompt is displayed...)

share|improve this answer
    
Thank you, very informative. If I could upvote you, I would. :) –  B. Striegel Aug 11 '11 at 19:56
add comment

You could redirect output to a file for the java commands

share|improve this answer
    
I tried that at first, but changing those lines to, say: java -DSTOP.PORT=8079 -DSTOP.KEY=something -jar start.jar --stop 1> /root/jetty.log 2>&1 has no effect, it just creates a blank jetty.log file and still prints to stdout. –  B. Striegel Aug 11 '11 at 19:56
add comment

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.