在 16.04 添加启动服务

Hes*_*ian 10 startup services nodejs npm 16.04

我需要在 16.4 上永久运行“node js”项目

并使用永久包在 ubuntu 的后台运行

现在我想向 ubuntu 添加一个启动服务,但我搜索没有结果。

我创建了一个名为文件test.conf/etc/init.d

测试.conf:

start on startup
exec forever start /root/node/node_modules/.bin/www
Run Code Online (Sandbox Code Playgroud)

Geo*_*sen 13

在最简单的使用中systemd service

  1. 安装forever

    [sudo] npm install forever -g
    
    Run Code Online (Sandbox Code Playgroud)
  2. 编写并存储脚本以在首选位置运行。

  3. Systemd service

    [Unit]
    Description=forever service
    After=network.target
    
    
    [Service]
    ExecStart=/home/george/.npm-global/bin/forever start /root/node/node_modules/.bin/www
    ExecStop=/home/george/.npm-global/bin/forever stop /root/node/node_modules/.bin/www
    Restart=always
    RestartSec=10                       # Restart service after 10 seconds if node service crashes
    StandardOutput=syslog               # Output to syslog
    StandardError=syslog                # Output to syslog
    SyslogIdentifier=nodejs-example
    
    
    [Install]
    WantedBy=multi-user.target
    
    Run Code Online (Sandbox Code Playgroud)
  4. systemd service文件另存/etc/systemd/systemmyforever.service(或使用您喜欢的任何名称)。

  5. 启动服务并在启动时启用。

    sudo systemctl start myforever.service
    sudo systemctl enable myforever.service
    
    Run Code Online (Sandbox Code Playgroud)
  6. 检查它是否正在运行:

    sudo systemctl status myforever.service
    
    Run Code Online (Sandbox Code Playgroud)
  7. 要随时停止和禁用它:

    sudo systemctl stop myforever.service
    sudo systemctl disable myforever.service
    
    Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 这是systemd service许多可用选项的简化版本
  2. 该服务也可以在myforever没有.service扩展名的情况下调用,systemd会选择正确的文件
  3. /home/george/.npm-global/bin/forever是我的node模块保存的地方,你的会有所不同。找到它which forever

附加信息:

https://www.axllent.org/docs/view/nodejs-service-with-systemd/