node.js app的Upstart脚本

时间:2021-07-08 22:00:03

I'm having trouble starting an Upstart script.

我在启动Upstart脚本时遇到问题。

Here's the script (app.conf in /etc/init/)

这是脚本(/ etc / init /中的app.conf)

description "node.js server"
author      "kvz"

start on startup
stop on shutdown

script
   # We found $HOME is needed. Without it, we ran into problems
   export HOME="/root"

   exec sudo -u /usr/local/bin/node \
                /var/www/vhosts/travelseguro.com/node/app.js \
                2>&1 >> /var/log/node.log
end script

When I run sudo start app, I get:

当我运行sudo start app时,我得到:

start: Unknown job: app

开始:未知的工作:app

How can I make this work?

我怎样才能做到这一点?

2 个解决方案

#1


16  

I was having the same problem running on the latest Amazon (AWS) linux which is Redhat based.

我在基于Redhat的最新亚马逊(AWS)linux上运行时遇到了同样的问题。

I have my upstart file in /etc/init called node.conf and when I ran sudo start node I would get a similar error to you start: Unknown job: node.

我在/ etc / init中将我的upstart文件称为node.conf,当我运行sudo start node时,我会遇到类似的错误:unknown job:node。

It turns out that the job won't start if there's an error in your .conf file. So I started out by commenting out all the lines and slowly building up to find the error. The error message isn't very clear and makes it look like upstart can't find your conf file.

事实证明,如果.conf文件中存在错误,作业将无法启动。所以我开始评论所有的行,然后慢慢建立以找到错误。错误消息不是很清楚,并使它看起来像upstart找不到您的conf文件。

Tailing your '/var/log/messages' will help you debug as Upstart logs to there (It may be somewhere different on Ubuntu. Mine said init: /etc/init/node-upstart.conf:8: Unknown stanza which helped me get to the bottom of it. In my particular case I was declaring variables incorrectly.

告诉你的'/ var / log / messages'将帮助你调试Upstart日志到那里(它可能在Ubuntu上有所不同。我说init:/etc/init/node-upstart.conf:8:未知的节帮助了我到底是它。在我的特殊情况下,我正确地声明了变量。

See on AskUbuntu for a similar thread.

请参阅AskUbuntu以获取类似的主题。

Here's my edited working script:

这是我编辑的工作脚本:

<!-- language: lang-sh -->
#!upstart
# using upstart http://upstart.ubuntu.com/getting-started.html and node forever  https://github.com/nodejitsu/forever/  
# to run server
# redhat has additional sudo restrictions, you must comment out 'Default requiretty' from /etc/sudoers
#startup monitoring script derived from http://*.com/questions/11084279/node-js-setup-for-easy-deployment-and-updating

description "node.js server"
author      "jujhar"
env PROGRAM_NAME="node"
env FULL_PATH="/home/httpd/buto-middleman/public"
env FILE_NAME="forever.js"
env NODE_PATH="/usr/local/bin/node"
env USERNAME="springloops"

start on startup
stop on shutdown

script
    export HOME="/root"  
    export NODE_ENV=staging #development/staging/production

    echo $$ > /var/run/$PROGRAM_NAME.pid
    cd $FULL_PATH        
    #exec sudo -u $USERNAME $NODE_PATH $FULL_PATH/$FILE_NAME >> /var/log/$PROGRAM_NAME.sys.log 2>&1
    exec $NODE_PATH $FULL_PATH/$FILE_NAME >> /var/log/$PROGRAM_NAME.sys.log 2>&1
end script

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

pre-stop script
    rm /var/run/$PROGRAM_NAME.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$PROGRAM_NAME.sys.log
end script

-- Edit 2013-06-01 --

- 编辑2013-06-01 -

If you're on Centos or Amazon Linux like me, take a look at this init.d script.

如果您在像我这样的Centos或Amazon Linux上,请查看此init.d脚本。

-- Edit 2013-10-14 --

- 编辑2013-10-14 -

Here's a link to a gist of an init.d script that I actually use in production on Amazon Linux(Redhat Based). I simply keep it in my project under an init.d folder and then symlink to it in the /etc/init.d folder and now it's a daemon/service!

这是我在Amazon Linux(Redhat Based)上实际使用的init.d脚本的要点的链接。我只是将它保存在我的项目中的init.d文件夹中,然后在/etc/init.d文件夹中对它进行符号链接,现在它是一个守护进程/服务!

-- Edit 2014-06-05 --

- 编辑2014-06-05 -

Check out this awesome blog artcile by Jeff Dickey on Node.js in production using systemd which is much cleaner and easier than all the stuff we've been doing here (IMHO). He also uses Ansible to control his cluster (which I love) but you don't have to go that far if you're not ready.

使用systemd查看Jeff Dickey在Node.js上制作的这个很棒的博客artcile,它比我们在这里做的所有东西(恕我直言)更清晰,更容易。他还使用Ansible来控制他的星团(我喜欢它),但是如果你还没准备好,你就不必走那么远。

#2


7  

After a few attempts I implemented working .conf file for upstart which works as a service with automatic start after reboot and restart (respawn) in case of crash. Also it can start my app with unprivileged user permissions. The name of the file is /etc/init/my-app.conf.

经过几次尝试后,我为upstart实现了工作.conf文件,该文件作为服务在重启后自动启动并在崩溃时重新启动(重新生成)。此外,它可以使用非特权用户权限启动我的应用程序。该文件的名称是/etc/init/my-app.conf。

To start / stop service please use

要开始/停止服务,请使用

sudo start my-app / sudo stop my-app

If you have an error like

如果你有错误的话

start: Unknown job: my-app

exec the following command

执行以下命令

sudo initctl reload-configuration

My /etc/init/my-app.conf file:

我的/etc/init/my-app.conf文件:

#my-app                                                                           
description "node.js my-app website"                                                  
env FULL_PATH="/home/myuser/app.prod/app"                                              
env NODE_PATH="/usr/bin/node"                                                     


start on filesystem or runlevel [2345]                                            
stop on [!2345]                                                                   

script                                                                            
    export HOME="/root"                                                           
    export NODE_ENV=production                                                    
    echo $$ > /var/run/my-app.pid                                                 
    cd $FULL_PATH        
    #Use exec below if you want to launch it under myuser, 
    #don't forget to create /var/log/my-app.sys.log with appropriate permissions
    #exec sudo -u myuser sh -c "$NODE_PATH server.js >> /var/log/my-app.sys.log 2>&1" 
    exec $NODE_PATH server.js >> /var/log/my-app.sys.log 2>&1                     
end script                                                                        

pre-start script                                                                  
    echo "[`date`] (sys) Starting" >> /var/log/my-app.sys.log                     
end script                                                                        

pre-stop script                                                                   
    rm /var/run/my-app.pid                                                        
    echo "[`date`] (sys) Stopping" >> /var/log/my-app.sys.log                     
end script                                                                        

#uncomment respawn if you want to restart your service in case of crash
#respawn                                                                          
#respawn limit 50 30   

I do recommend to uncomment respawn after you will make sure that everything works ok.

我建议您在确保一切正常后取消注释respawn。

UPDATE

UPDATE

I improved my script (please keep in mind that it works not under root but under regular user zn ):

我改进了我的脚本(请记住,它不是在root下工作,而是在常规用户zn下):

#znapi.conf                                                                    
description "node.js zn api"                                        

env FULL_PATH="/home/zn/app.prod"                          
env NODE_PATH="/usr/bin/node"                                  
env LOG="/var/log/znapi.log"
env PIDFILE="/var/run/znapi.pid"

# Start up when the system hits any normal runlevel, and 
#start on filesystem or runlevel [2345]     
#start when mongod started
start on started mongod

# shuts down when the system goes to shutdown or reboot.
stop on runlevel [06]                                                              

respawn
respawn limit 50 5

pre-start script                                                    
    # check that log file exist or create it
    test -f $LOG || { touch $LOG; chown zn:zn $LOG; }

    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date`] (sys) Starting" >> $LOG            
end script                                                          

script                                                                       
    export NODE_ENV=production
    exec start-stop-daemon --start -m -p $PIDFILE -c zn -d $FULL_PATH -x server.js >> $LOG 2>&1
end script                                                                   

pre-stop script                                                              
    rm $PIDFILE                                                   
    echo "[`date`] (sys) Stopping" >> $LOG                    
end script 

#1


16  

I was having the same problem running on the latest Amazon (AWS) linux which is Redhat based.

我在基于Redhat的最新亚马逊(AWS)linux上运行时遇到了同样的问题。

I have my upstart file in /etc/init called node.conf and when I ran sudo start node I would get a similar error to you start: Unknown job: node.

我在/ etc / init中将我的upstart文件称为node.conf,当我运行sudo start node时,我会遇到类似的错误:unknown job:node。

It turns out that the job won't start if there's an error in your .conf file. So I started out by commenting out all the lines and slowly building up to find the error. The error message isn't very clear and makes it look like upstart can't find your conf file.

事实证明,如果.conf文件中存在错误,作业将无法启动。所以我开始评论所有的行,然后慢慢建立以找到错误。错误消息不是很清楚,并使它看起来像upstart找不到您的conf文件。

Tailing your '/var/log/messages' will help you debug as Upstart logs to there (It may be somewhere different on Ubuntu. Mine said init: /etc/init/node-upstart.conf:8: Unknown stanza which helped me get to the bottom of it. In my particular case I was declaring variables incorrectly.

告诉你的'/ var / log / messages'将帮助你调试Upstart日志到那里(它可能在Ubuntu上有所不同。我说init:/etc/init/node-upstart.conf:8:未知的节帮助了我到底是它。在我的特殊情况下,我正确地声明了变量。

See on AskUbuntu for a similar thread.

请参阅AskUbuntu以获取类似的主题。

Here's my edited working script:

这是我编辑的工作脚本:

<!-- language: lang-sh -->
#!upstart
# using upstart http://upstart.ubuntu.com/getting-started.html and node forever  https://github.com/nodejitsu/forever/  
# to run server
# redhat has additional sudo restrictions, you must comment out 'Default requiretty' from /etc/sudoers
#startup monitoring script derived from http://*.com/questions/11084279/node-js-setup-for-easy-deployment-and-updating

description "node.js server"
author      "jujhar"
env PROGRAM_NAME="node"
env FULL_PATH="/home/httpd/buto-middleman/public"
env FILE_NAME="forever.js"
env NODE_PATH="/usr/local/bin/node"
env USERNAME="springloops"

start on startup
stop on shutdown

script
    export HOME="/root"  
    export NODE_ENV=staging #development/staging/production

    echo $$ > /var/run/$PROGRAM_NAME.pid
    cd $FULL_PATH        
    #exec sudo -u $USERNAME $NODE_PATH $FULL_PATH/$FILE_NAME >> /var/log/$PROGRAM_NAME.sys.log 2>&1
    exec $NODE_PATH $FULL_PATH/$FILE_NAME >> /var/log/$PROGRAM_NAME.sys.log 2>&1
end script

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

pre-stop script
    rm /var/run/$PROGRAM_NAME.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$PROGRAM_NAME.sys.log
end script

-- Edit 2013-06-01 --

- 编辑2013-06-01 -

If you're on Centos or Amazon Linux like me, take a look at this init.d script.

如果您在像我这样的Centos或Amazon Linux上,请查看此init.d脚本。

-- Edit 2013-10-14 --

- 编辑2013-10-14 -

Here's a link to a gist of an init.d script that I actually use in production on Amazon Linux(Redhat Based). I simply keep it in my project under an init.d folder and then symlink to it in the /etc/init.d folder and now it's a daemon/service!

这是我在Amazon Linux(Redhat Based)上实际使用的init.d脚本的要点的链接。我只是将它保存在我的项目中的init.d文件夹中,然后在/etc/init.d文件夹中对它进行符号链接,现在它是一个守护进程/服务!

-- Edit 2014-06-05 --

- 编辑2014-06-05 -

Check out this awesome blog artcile by Jeff Dickey on Node.js in production using systemd which is much cleaner and easier than all the stuff we've been doing here (IMHO). He also uses Ansible to control his cluster (which I love) but you don't have to go that far if you're not ready.

使用systemd查看Jeff Dickey在Node.js上制作的这个很棒的博客artcile,它比我们在这里做的所有东西(恕我直言)更清晰,更容易。他还使用Ansible来控制他的星团(我喜欢它),但是如果你还没准备好,你就不必走那么远。

#2


7  

After a few attempts I implemented working .conf file for upstart which works as a service with automatic start after reboot and restart (respawn) in case of crash. Also it can start my app with unprivileged user permissions. The name of the file is /etc/init/my-app.conf.

经过几次尝试后,我为upstart实现了工作.conf文件,该文件作为服务在重启后自动启动并在崩溃时重新启动(重新生成)。此外,它可以使用非特权用户权限启动我的应用程序。该文件的名称是/etc/init/my-app.conf。

To start / stop service please use

要开始/停止服务,请使用

sudo start my-app / sudo stop my-app

If you have an error like

如果你有错误的话

start: Unknown job: my-app

exec the following command

执行以下命令

sudo initctl reload-configuration

My /etc/init/my-app.conf file:

我的/etc/init/my-app.conf文件:

#my-app                                                                           
description "node.js my-app website"                                                  
env FULL_PATH="/home/myuser/app.prod/app"                                              
env NODE_PATH="/usr/bin/node"                                                     


start on filesystem or runlevel [2345]                                            
stop on [!2345]                                                                   

script                                                                            
    export HOME="/root"                                                           
    export NODE_ENV=production                                                    
    echo $$ > /var/run/my-app.pid                                                 
    cd $FULL_PATH        
    #Use exec below if you want to launch it under myuser, 
    #don't forget to create /var/log/my-app.sys.log with appropriate permissions
    #exec sudo -u myuser sh -c "$NODE_PATH server.js >> /var/log/my-app.sys.log 2>&1" 
    exec $NODE_PATH server.js >> /var/log/my-app.sys.log 2>&1                     
end script                                                                        

pre-start script                                                                  
    echo "[`date`] (sys) Starting" >> /var/log/my-app.sys.log                     
end script                                                                        

pre-stop script                                                                   
    rm /var/run/my-app.pid                                                        
    echo "[`date`] (sys) Stopping" >> /var/log/my-app.sys.log                     
end script                                                                        

#uncomment respawn if you want to restart your service in case of crash
#respawn                                                                          
#respawn limit 50 30   

I do recommend to uncomment respawn after you will make sure that everything works ok.

我建议您在确保一切正常后取消注释respawn。

UPDATE

UPDATE

I improved my script (please keep in mind that it works not under root but under regular user zn ):

我改进了我的脚本(请记住,它不是在root下工作,而是在常规用户zn下):

#znapi.conf                                                                    
description "node.js zn api"                                        

env FULL_PATH="/home/zn/app.prod"                          
env NODE_PATH="/usr/bin/node"                                  
env LOG="/var/log/znapi.log"
env PIDFILE="/var/run/znapi.pid"

# Start up when the system hits any normal runlevel, and 
#start on filesystem or runlevel [2345]     
#start when mongod started
start on started mongod

# shuts down when the system goes to shutdown or reboot.
stop on runlevel [06]                                                              

respawn
respawn limit 50 5

pre-start script                                                    
    # check that log file exist or create it
    test -f $LOG || { touch $LOG; chown zn:zn $LOG; }

    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date`] (sys) Starting" >> $LOG            
end script                                                          

script                                                                       
    export NODE_ENV=production
    exec start-stop-daemon --start -m -p $PIDFILE -c zn -d $FULL_PATH -x server.js >> $LOG 2>&1
end script                                                                   

pre-stop script                                                              
    rm $PIDFILE                                                   
    echo "[`date`] (sys) Stopping" >> $LOG                    
end script