如何在Upstart脚本中预启动环境变量? [英] How to set environment variable in pre-start in Upstart script?

查看:170
本文介绍了如何在Upstart脚本中预启动环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个自定义C ++守护程序应用程序,一次分叉。所以我们一直在Ubuntu 12.04中的Upstart脚本中做到这一点,它的工作原理很好:

  expect fork 
exec / path / to / the / app

但是现在我们需要传递一个参数给我们的应用程序包含运行机器上的CPU数量:

  cat / proc / cpuinfo | grep处理器| wc -l 

我们第一次尝试的是:

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

当我们的应用程序启动正确的-t值时,Upstart跟踪错误的pid值得,因为那些猫,grep& wc命令在我们的应用程序之前执行exec的所有启动过程。



我也尝试过这个,即使它不起作用,我猜是因为设置一个env var运行一个进程? Upstart仍然跟踪错误的pid:

  expect fork 
script
NUM_CORES = 32
/路径/到/ the / app -t $ NUM_CORES
结束脚本

我也尝试在一个env节中这样做,但显然那些不运行命令:

  env num_cores =`cat / proc / cpuinfo | grep处理器| wc -l`

还尝试在预启动时这样做,但是env vars设置在那里在exec节中有任何值:

  pre-start 
NUM_CORES = 32
end script

任何想法如何正确设置这个NUM_CORES集,并且仍然可以让Upstart跟踪我们的应用程序的正确pid叉一次?

解决方案

这很尴尬。推荐的方法是在预先启动节中编写一个env文件,然后在脚本节中将其引用。这是可笑的,我知道。

  expect fork 

预启动脚本
exec> ;/ tmp / $ UPSTART_JOB
echoNUM_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
结束脚本

我在开始之前使用exec行因为我通常有多个env变量,我不想重复重定向代码。



这仅仅是因为'。 '命令是一个内置的破折号,因此没有生成进程。


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:

expect fork
exec /path/to/the/app

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:

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

Our first attempt was this:

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

While that starts our app with the correct -t value, Upstart tracks the wrong pid value, I'm assuming because those cat, grep & wc commands all launch processes in exec before our app.

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:

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

I've also tried doing this in an env stanza but apparently those don't run commands:

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

Also tried doing this in pre-start, but env vars set there don't have any values in the exec stanza:

pre-start
    NUM_CORES=32
end script

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?

解决方案

It's awkward. The recommended method is to write an env file in the pre-start stanza and then source it in the script stanza. It's 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-start because I usually have multiple env variables and I don't want to repeat the redirection code.

This only works because the '. ' command is a built-in in dash and thus no process is spawned.

这篇关于如何在Upstart脚本中预启动环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆