4

OS: Ubuntu 12.04

Task: run script at certain start/stop conditions. No long-running service/daemon is launched, only short-term actions.

Upstart script test.conf:

description     "test script"

start on local-filesystems or runlevel [2345]
stop on runlevel [!2345]

task

pre-start exec /full/path/to/script.sh start
pre-stop exec /full/path/to/script.sh stop

Problems:

  1. Running 'start test' never ends. If I Ctrl-C hanged upstart, furtehr 'status' calls show the service is running.'
  2. 'stop test' doesn't result in running 'pre-stop' stanza at all.

Is the original task solvable using upstart scripts?

Thanks.

Update. "the fact that your script.sh has both start and stop actions, which does suggest a long-running daemon"

No contradiction. There's startup action and cleanup action, without long-running process(es) in between.

When I add 'exec' stanza, i.e.

exec /full/path/to/script.sh idle

the result is this:

$ start test
test stop/waiting

$ status test
test stop/waiting

$ stop test
stop: Unknown instance:

Correction: The script mentioned in stanzas is executed with 'start' and 'idle' parameters, but not with 'stop'.

3

Your upstart config file lacks an exec or script section, and you seem to be wanting to start things with the pre-start command only, which is not correct. Per the upstart documentation (http://upstart.ubuntu.com/getting-started.html):

All job files must have either an exec or script stanza. This specifies what will be run for the job.

Additional shell code can be given to be run before or after the binary or script specified with exec or script. These are not expected to start the process, in fact, they can't. They are intended for preparing the environment and cleaning up afterwards.

Also, what your original task requests ("No long-running service/daemon is launched, only short-term actions.") seems to contradict the fact that your script.sh has both start and stop actions, which does suggest a long-running daemon.

description     "test script"

start on local-filesystems or runlevel [2345]
stop on runlevel [!2345]

task

# Assuming script.sh start starts, performs a process, and then exits,
# this should work
exec /full/path/to/script.sh start

# No need for a pre-stop

The upstart document I linked to is supposedly deprecated, you can also have a look at http://upstart.ubuntu.com/cookbook/ which is a good reference and has useful examples, so I suggest you read it if things are still unclear.

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.