title: How to set environment variable in pre-start in Upstart script?
body: <p>We have a custom C++ daemon application that forks once. So we've been doing this in our Upstart script on Ubuntu 12.04 and it works perfectly:</p>
  
  <pre><code>expect fork
  exec /path/to/the/app
  </code></pre>
  
  <p>However now we need to pass in an argument to our app which contains the number of CPUs on the machine on which it runs:</p>
  
  <pre><code>cat /proc/cpuinfo | grep processor | wc -l
  </code></pre>
  
  <p>Our first attempt was this:</p>
  
  <pre><code>expect fork
  exec /path/to/the/app -t `cat /proc/cpuinfo | grep processor | wc -l`
  </code></pre>
  
  <p>While that starts our app with the correct -t value, Upstart tracks the wrong pid value, I'm assuming because those cat, grep &amp; wc commands all launch processes in exec before our app.</p>
  
  <p>I also tried this, and even it doesn't work, I guess because setting an env var runs a process? Upstart still tracks the wrong pid:</p>
  
  <pre><code>expect fork
  script
      NUM_CORES=32
      /path/to/the/app -t $NUM_CORES
  end script
  </code></pre>
  
  <p>I've also tried doing this in an env stanza but apparently those don't run commands:</p>
  
  <pre><code>env num_cores=`cat /proc/cpuinfo | grep processor | wc -l`
  </code></pre>
  
  <p>Also tried doing this in pre-start, but env vars set there don't have any values in the exec stanza:</p>
  
  <pre><code>pre-start
      NUM_CORES=32
  end script
  </code></pre>
  
  <p>Any idea how to get this NUM_CORES set properly, and still get Upstart to track the correct pid for our app that forks once?</p>