如何缩短inittab进程条目,a.k.a。,在哪里放置将由init看到的环境变量?

时间:2022-05-14 23:13:59

I am setting up a Debian Etch server to host ruby and php applications with nginx. I have successfully configured inittab to start the php-cgi process on boot with the respawn action. After serving 1000 requests, the php-cgi worker processes die and are respawned by init. The inittab record looks like this:

我正在建立一个Debian Etch服务器来托管使用nginx的ruby和php应用程序。我已成功配置inittab以使用respawn操作启动启动php-cgi进程。在提供1000个请求之后,php-cgi worker处理死亡并由init重新生成。 inittab记录如下所示:

50:23:respawn:/usr/local/bin/spawn-fcgi -n -a 127.0.0.1 -p 8000 -C 3 -u someuser -- /usr/bin/php-cgi

I initially wrote the process entry (everything after the 3rd colon) in a separate script (simply because it was long) and put that script name in the inittab record, but because the script would run its single line and die, the syslog was filled with errors like this:

我最初在一个单独的脚本中编写了进程条目(第三次冒号之后的所有内容)(仅仅因为它很长)并将该脚本名称放在inittab记录中,但由于脚本将运行其单行而死亡,因此syslog已填充有这样的错误:

May  7 20:20:50 sb init: Id "50" respawning too fast: disabled for 5 minutes

Thus, I got rid of the script file and just put the whole line in the inittab. Henceforth, no errors show up in the syslog.

因此,我摆脱了脚本文件,只是将整行放在inittab中。此后,syslog中不会出现任何错误。

Now I'm attempting the same with thin to serve a rails application. I can successfully start the thin server by running this command:

现在我尝试使用thin来同样服务于rails应用程序。我可以通过运行此命令成功启动瘦服务器:

sudo thin -a 127.0.0.1 -e production -l /var/log/thin/thin.log -P /var/run/thin/thin.pid -c /path/to/rails/app -p 8010 -u someuser -g somegroup -s 2 -d start

It works apparently exactly the same whether I use the -d (daemonize) flag or not. Command line control comes immediately back (the processes have been daemonized) either way. If I put that whole command (minus the sudo and with absolute paths) into inittab, init complains (in syslog) that the process entry is too long, so I put the options into an exported environment variable in /etc/profile. Now I can successfully start the server with:

无论我是否使用-d(daemonize)标志,它的工作方式都完全相同。无论哪种方式,命令行控制都会立即返回(进程已被守护进程)。如果我把整个命令(减去sudo和绝对路径)放到inittab中,init会抱怨(在syslog中)进程条目太长,所以我将选项放入/ etc / profile中的导出环境变量中。现在我可以成功启动服务器:

sudo thin $THIN_OPTIONS start

But when I put this in an inittab record with the respawn action

但是当我把它放在带有respawn动作的inittab记录中时

51:23:respawn:/usr/local/bin/thin $THIN_OPTIONS start

the logs clearly indicate that the environment variable is not visible to init; it's as though the command were simply "thin start."

日志清楚地表明init不可见环境变量;就好像这个命令只是“薄的开始”。

How can I shorten the inittab process entry? Is there another file than /etc/profile where I could set the THIN_OPTIONS environment variable? My earlier experience with php-cgi tells me I can't just put the whole command in a separate script.

如何缩短inittab进程条目?是否有另一个文件而不是/ etc / profile我可以设置THIN_OPTIONS环境变量?我之前使用php-cgi的经验告诉我,我不能把整个命令放在一个单独的脚本中。

2 个解决方案

#1


init.d script

Use a script in

使用脚本

/etc/rc.d/init.d

and set the runlevel

并设置运行级别

Here are some examples with thin, ruby, apache

以下是一些薄,红宝石,apache的例子

http://articles.slicehost.com/2009/4/17/centos-apache-rails-and-thin

http://blog.fiveruns.com/2008/9/24/rails-automation-at-slicehost

http://elwoodicious.com/2008/07/15/nginx-haproxy-thin-fastcgi-php5-load-balanced-rails-with-php-support/

Which provide example initscripts to use.

其中提供了使用示例脚本。

edit: Asker pointed out this will not allow respawning. I suggested forking in the init script and disowning the process so init doesn't hang (it might fork() the script itself, will check). And then creating an infinite loop that waits on the server process to die and restarts it.

编辑:Asker指出这不会允许重生。我建议在init脚本中进行分叉并忽略该进程,这样init就不会挂起(它可能会fork()脚本本身,会检查)。然后创建一个无限循环,等待服务器进程死亡并重新启动它。

edit2: It seems init will fork the script. Just a loop should do it.

edit2:看来init会分叉脚本。只需一个循环即可。

#2


And why don't you call a wrapper who start thin whith your options?

start_thin.sh:
#!/bin/bash
/usr/local/bin/thin -a 127.0.0.1 -e production -l /var/log/thin/thin.log -P /var/run/thin/thin.pid -c /path/to/rails/app -p 8010 -u someuser -g somegroup -s 2 -d start

你为什么不打电话给你的选择开始瘦的包装? start_thin.sh:#!/ bin / bash / usr / local / bin / thin -a 127.0.0.1 -e production -l /var/log/thin/thin.log -P /var/run/thin/thin.pid -c / path / to / rails / app -p 8010 -u someuser -g somegroup -s 2 -d start

and then:
51:23:respawn:/usr/local/bin/start_thin

然后:51:23:respawn:/ usr / local / bin / start_thin

#1


init.d script

Use a script in

使用脚本

/etc/rc.d/init.d

and set the runlevel

并设置运行级别

Here are some examples with thin, ruby, apache

以下是一些薄,红宝石,apache的例子

http://articles.slicehost.com/2009/4/17/centos-apache-rails-and-thin

http://blog.fiveruns.com/2008/9/24/rails-automation-at-slicehost

http://elwoodicious.com/2008/07/15/nginx-haproxy-thin-fastcgi-php5-load-balanced-rails-with-php-support/

Which provide example initscripts to use.

其中提供了使用示例脚本。

edit: Asker pointed out this will not allow respawning. I suggested forking in the init script and disowning the process so init doesn't hang (it might fork() the script itself, will check). And then creating an infinite loop that waits on the server process to die and restarts it.

编辑:Asker指出这不会允许重生。我建议在init脚本中进行分叉并忽略该进程,这样init就不会挂起(它可能会fork()脚本本身,会检查)。然后创建一个无限循环,等待服务器进程死亡并重新启动它。

edit2: It seems init will fork the script. Just a loop should do it.

edit2:看来init会分叉脚本。只需一个循环即可。

#2


And why don't you call a wrapper who start thin whith your options?

start_thin.sh:
#!/bin/bash
/usr/local/bin/thin -a 127.0.0.1 -e production -l /var/log/thin/thin.log -P /var/run/thin/thin.pid -c /path/to/rails/app -p 8010 -u someuser -g somegroup -s 2 -d start

你为什么不打电话给你的选择开始瘦的包装? start_thin.sh:#!/ bin / bash / usr / local / bin / thin -a 127.0.0.1 -e production -l /var/log/thin/thin.log -P /var/run/thin/thin.pid -c / path / to / rails / app -p 8010 -u someuser -g somegroup -s 2 -d start

and then:
51:23:respawn:/usr/local/bin/start_thin

然后:51:23:respawn:/ usr / local / bin / start_thin