How to set environment variable in pre-launch in upstart script?

We have a custom C ++ daemon application that deploys once. So, we did this in our Upstart script on Ubuntu 12.04, and it works great:

expect fork exec /path/to/the/app 

However, now we need to pass an argument to our application, which contains the number of processors on the machine on which it runs:

 cat /proc/cpuinfo | grep processor | wc -l 

Our first attempt was as follows:

 expect fork exec /path/to/the/app -t `cat /proc/cpuinfo | grep processor | wc -l` 

While this is the beginning of our application with the correct -t value, Upstart keeps track of the incorrect pid value, I assume that these cats, grep and wc start all startup processes in exec before our application.

I also tried this, and even this did not work, I think, because installing env var starts the process? Upstart is still tracking the wrong pid:

 expect fork script NUM_CORES=32 /path/to/the/app -t $NUM_CORES end script 

I also tried doing this in the env stanza, but apparently they do not run commands:

 env num_cores=`cat /proc/cpuinfo | grep processor | wc -l` 

Also tried to do this on pre-launch, but the env vars installed there has no value in exec exec:

 pre-start NUM_CORES=32 end script 

Any idea how to properly set this NUM_CORES, and still get Upstart to track the correct pid for our application that deploys once.

+8
environment-variables upstart
source share
3 answers

It is not comfortable. The recommended method is to write the env file in a pre-executable stanza, and then specify it in the script stanza. This is ridiculous, I know.

 expect fork pre-start script exec >"/tmp/$UPSTART_JOB" echo "NUM_CORES=$(cat /proc/cpuinfo | grep processor | wc -l)" end script script . "/tmp/$UPSTART_JOB" /path/to/app -t "$NUM_CORES" end script post-start script rm -f "/tmp/$UPSTART_JOB" end script 

I use the exec line in the pre-launch because I usually have several env variables and I don’t want to repeat the redirection code.

This only works because '.' Is embedded in the dash, and therefore the process is not created.

+16
source share

As per upstart zram-config configuration:

 script NUM_CORES=$(grep -c ^processor /proc/cpuinfo | sed 's/^0$/1/') /path/to/the/app -t $NUM_CORES end script 
+2
source share

I would add

 export NUM_CORES 

after assigning it a value in the "script". I remember that a / bin / sh, symbolically associated with a non-Bash shell, can run scripts, so I would avoid Bash-based constructions.

Re: Using the env stanza, it passes values ​​literally and does not process them using shell conventions.

0
source share

All Articles