Why do I need to hit enter to get my shell prompt after my init.d script completes?

I had to write my own CentOS init.d script for celery because it only ships with one for Debian. You can see the script I wrote when I answered my own stack overflow question 3989656.

But there’s a problem with this script. if I invoke it:

sudo service celeryd start

then I need to hit enter to get a shell prompt after it completes. This is a problem because I want to invoke it via ssh from another machine by doing:

ssh 192.168.2.3 sudo service celeryd start

and ssh never returns. (I use fabric to start and stop remote services, and this hangs it because it invokes the ssh command above).

What would cause this behavior, and how do I fix it in my script?

Note that if I do “sh -x /etc/init.d/celeryd” as recommended by Thomas Themel in the comments, the output is:

runuser -s /bin/bash - apache -c 'ulimit -S -c 0 >/dev/null 2>&1 ; /usr/local/django/portalapps/manage.py celeryd --pidfile /var/run/celery.pid -f /var/log/celeryd.log -l INFO'

I don’t understand how the daemon function in /etc/init.d/functions (which is what produces this series of bash commands) actually daemonizes the process.

Asked By: Lorin Hochstein

||

You might want to figure out what exactly eats that return, by first running the /etc/init.d script directly with (sudo as required) sh -x /etc/init.d/celeryd start. If it is in the shell script, you should see it this way. If daemon itself is waiting for a newline before returning, your answer will be celeryd specific and might benefit from running it through strace or simply with a < /dev/null appended so that it doesn’t have access to standard input through your terminal.

Another “grope in the dark” thing to try: run ssh with the -nt option to disable terminal allocation and standard input.

Answered By: Thomas Themel

You might actually have a shell prompt and not realize it. If you have a program that writes to the terminal while in the background, your prompt gets covered up, but it’s still ready for input.

test.sh:

#! /bin/sh
echo -e "This is a testnn"

interactive shell:

$sh test.sh &
$This is a test



_

In this example, the prompt is there. You see the “$” right before “This is a test” that is the prompt. At the bottom you can see the cursor waiting for input. If you type a command here and press enter, it will work as usual. Try running ls after starting your daemon but before pressing <enter>.

Answered By: Shawn J. Goff

Does the daemon command need a & at the end. I don’t think it does. This may be the source of your problem.

Answered By: Dennis Williamson

Had the exact same problem – your question and investigations actually helped me find the answer.

Start celery with celery_detach – and, poof! Things work!

ie. manage.py celery_detach --params --settings=foo

It returns immediately, with a proper line break, and the things that fabric is looking for.

Answered By: skoczen
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.