Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to start emacs daemon through upstart. Here is my script

# emacs --daemon - Emacs daemon
#
# The Emacs daemon provides a server for Emacs clients.

description     "Emacs daemon"

start on runlevel[2345]
stop on runlevel[!2345]

respawn
respawn limit 10 5 # respawn up to 10 times, waiting 5 seconds each time

pre-start script
        echo "Starting emacs daemon..." > /home/eric/Desktop/emacs.log
end script

pre-stop script
        emacsclient -n -e '(save-persistent-scratch)'
end script

exec emacs --daemon

I can start this through sudo initctl start emacs just fine. However, this does not execute during startup (or emacs --daemon dies?). Calling sudo initctl list displays the job as emacs stop/waiting, which apparently means the job has not been started yet. However, calling runlevel returns N 2.

How do I fix this?

Edit: v2 of conf file (added expect fork and ran emacs under my account) Edit: v3 (corrected to expect daemon)

# emacs --daemon - Emacs daemon
# 
# The Emacs daemon provides a server for Emacs clients.

description     "Emacs daemon"

start on runlevel[2345]
stop on runlevel[!2345]

expect daemon
respawn
respawn limit 10 5 # respawn up to 10 times, waiting 5 seconds each time

pre-start script
        echo "Starting emacs daemon..." > /home/eric/Desktop/emacs.log
end script

pre-stop script
    emacsclient -n -e '(save-persistent-scratch)'
end script

exec start-stop-daemon --start --chuid eric --exec /usr/bin/emacs -- --daemon -u eric

Edit:

After digging around, I found out that upstart provides a tool called initctl check-config which can check for unreachable conditions.

Running the tool revealed that my conf file was missing a space at runlevel[2345] like so runlevel [2345]. The daemon now starts up correctly.

However, when I run sudo initctl stop emacs, it hangs and emacs daemon is not killed. I find in dmesg this and only this

[ 4378.169249] init: emacs goal changed from start to stop

But, if I kill emacs through kill or emacsclient -n -e '(kill-emacs)', this appears in dmesg

[ 4378.169286] init: emacs state changed from spawned to stopping
[ 4378.169314] init: event_new: Pending stopping event
[ 4378.169325] init: Handling stopping event
[ 4378.169392] init: event_finished: Finished stopping event
[ 4378.169399] init: emacs state changed from stopping to killed
[ 4378.169431] init: emacs state changed from killed to post-stop
[ 4378.169450] init: emacs state changed from post-stop to waiting
[ 4378.169473] init: event_new: Pending stopped event
[ 4378.169484] init: job_change_state: Destroyed inactive instance emacs
[ 4378.169542] init: Handling stopped event
[ 4378.169594] init: event_finished: Finished stopped event

The question now is, why doesn't initctl stop emacs work?

share|improve this question
    
Did you look at your /home/eric/Desktop/emacs.log? also, look in /var/log/upstart/emacs.log for more clues. –  roadmr Oct 3 '13 at 18:52
    
@roadmr The log does not get generated when i restart the computer. It seems upstart just doesn't start the job... –  Eric Huang Oct 3 '13 at 20:16
    
Could you separate that in two questions? And add as answer what solved your first problem (emacs don't start)? –  Braiam Oct 26 '13 at 21:24
add comment

1 Answer 1

Because 'emacs --daemon' detaches from the foreground, you need to let upstart know to expect this; otherwise when the parent emacs process exits, it assumes the service has terminated, leaving the service in a stopped state even though emacs is still running.

To let upstart know which process it needs to track (and kill when stopping the service), you will want to add 'expect fork' to the job definition.

share|improve this answer
    
I have expect daemon in the conf file, which does effectively the same thing as expect fork –  Eric Huang Oct 4 '13 at 6:23
    
expect fork expects the program to fork once, while expect daemon expects it to fork twice. See this section of the Upstart Cookbook for a chart on what sort of misbehavior can occur if you tell Upstart to expect an incorrect number of forks. As that chart says, if you use expect daemon when you have only one fork, the script will hang, which is the behavior you're seeing. –  spiffytech Feb 14 at 15:34
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.