add startup service on ubuntu16.04--在ubuntu16.04添加服务

本文介绍了如何在Ubuntu 16.04上安装服务脚本,创建systemd服务文件,并设置开机启动。首先安装必要组件,然后将脚本保存到指定位置。接着创建并保存systemd服务文件,如`myservice.service`。通过命令启动服务并设置开机启动,可以使用`systemctl`检查服务状态。若要停止和禁用服务,也有相应命令。注意,service文件通常位于`/etc/systemd/system`目录下,可根据实际情况查找。参考链接提供了更多详细信息。

In the simplest for using systemd service:

  1. Install forever:

    [sudo] npm install forever -g
    
  2. Write and store the script to run in preferred location.

  3. Write the 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
    
  4. Save the systemd service file in /etc/systemd/system as myforever.service ( or with whatever name you like ).

  5. Start the service and enable at start up.

    sudo systemctl start myforever.service
    sudo systemctl enable myforever.service
    
  6. Check if it's running:

    sudo systemctl status myforever.service
    
  7. To stop and disable it any time:

    sudo systemctl stop myforever.service
    sudo systemctl disable myforever.service
    

NOTE:

  1. This is a simplified version of a systemd service many options are available
  2. The service can also be called myforever without the .service extension, systemd will pick the right file
  3. This /home/george/.npm-global/bin/forever is where my node modules are kept, yours will be different. Find it with which forever

Additional Information:

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

 

ref link: https://askubuntu.com/questions/894665/add-startup-service-on-16-04

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值