Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm new to upstart, and I would like to know if there's a way to tell upstart to execute a specific command only at system startup, and not when the application is subsequently restarted from the command line. For example, I have the following upstart configuration:

#!upstart
description "Hello World"
author      "me"

start on (local-filesystems and net-device-up)
stop on shutdown

script
    export HOME="/root"
    echo $$ > /var/run/hello-world.pid
    exec /usr/local/bin/hello-world
end script

pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] Hello World Starting" >> /var/log/hello-world.sys.log
end script

pre-stop script
    rm /var/run/hello-world.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] Hello World Stopping" >> /var/log/hello-world.sys.log
end script

That successfully starts the application on startup. If I execute:

/sbin/start hello-world

This starts the application again: I would like to execute a specific command only when the system is started up, and not when the application is started up, like:

First execution:

  1. The system starts up
  2. Upstart is executed
  3. A specific system startup script is executed
  4. The application, hello-world, is executed

Second execution using /sbin/start hello-world:

  1. Only execute application without executing the specific startup script

Is there a way for doing this?

share|improve this question
add comment

2 Answers

Use a 'cookie-file' below /var/run*. Execute the specific startup script only if the cookie-file does not exist and create it after successful execution.

*) Filesystem Hierarchy Standard 2.3

Files under this directory must be cleared (removed or truncated as appropriate) at the beginning of the boot process.
share|improve this answer
add comment

I've got one solution, but it's butt ugly.

So.. /proc/uptime contains the number of seconds since startup.

$ cat /proc/uptime
  350735.47 234388.90

The first number is number of seconds on, the second number is number of idle seconds..

SO..

$UPTIME=$(cut -f1 -d' '< /proc/uptime)
if [ $UPTIME -le 300 ] then
  /start/pre-run/script.sh
fi

Would run the pre-run script if the system has just booted (or hasn't been working for 300s). You could tune the 300s down to the actual time your system takes to get ready.

It's dirty as hell, but worth a try, right?

share|improve this answer
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.