V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chenqh
V2EX  ›  Node.js

如何通过 systemctl 部署 eggjs?

  •  
  •   chenqh · 2018-08-06 11:13:02 +08:00 · 5288 次点击
    这是一个创建于 2335 天前的主题,其中的信息可能已经有所发展或是发生改变。

    通过命令行可以启动:

    cqh@cqh-MS-7A74:~/local_code/tscode/local_alarm$ npm run start
    
    > [email protected] start /home/cqh/local_code/tscode/local_alarm
    > egg-scripts start --daemon --title=egg-server-local_alarm
    
    [egg-scripts] Starting egg application at /home/cqh/local_code/tscode/local_alarm
    [egg-scripts] Run node /home/cqh/local_code/tscode/local_alarm/node_modules/egg-scripts/lib/start-cluster {"title":"egg-server-local_alarm","framework":"/home/cqh/local_code/tscode/local_alarm/node_modules/egg","baseDir":"/home/cqh/local_code/tscode/local_alarm"} --title=egg-server-local_alarm
    [egg-scripts] Save log file to /home/cqh/logs
    [egg-scripts] Wait Start: 1...
    [egg-scripts] egg started on http://127.0.0.1:7001
    
    

    service 文件

    [Unit]
    Description= local alarm service write by lua
    
    [Service]
    Type=simple
    WorkingDirectory=/home/cqh/local_code/tscode/local_alarm
    ExecStart=/home/cqh/git/nvm/versions/node/v10.3.0/bin/npm run start
    User=cqh
    Restart=on-failure
    ExecStop=/home/cqh/git/nvm/versions/node/v10.3.0/bin/npm run stop
    
    [Install]
    WantedBy=multi-user.target
    
    

    但是通过 systemctl 无法启动 journalctl -xe显示

    -- Subject: local_alarm_lua.service 单元已结束停止操作
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    -- 
    -- local_alarm_lua.service 单元已结束停止操作。
    8 月 06 10:51:01 cqh-MS-7A74 systemd[1]: Started local alarm service write by lua.
    -- Subject: local_alarm_lua.service 单元已结束启动
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    -- 
    -- local_alarm_lua.service 单元已结束启动。
    -- 
    -- 启动结果为“ done ”。
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]: /home/cqh/git/nvm/versions/node/v10.3.0/lib/node_modules/npm/bin/npm-cli.js:79
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:       let notifier = require('update-notifier')({pkg})
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:       ^^^
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]: SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at exports.runInThisContext (vm.js:53:16)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at Module._compile (module.js:374:25)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at Object.Module._extensions..js (module.js:417:10)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at Module.load (module.js:344:32)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at Function.Module._load (module.js:301:12)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at Function.Module.runMain (module.js:442:10)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at startup (node.js:136:18)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4792]:     at node.js:966:3
    8 月 06 10:51:01 cqh-MS-7A74 systemd[1]: local_alarm_lua.service: Main process exited, code=exited, status=1/FAILURE
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]: /home/cqh/git/nvm/versions/node/v10.3.0/lib/node_modules/npm/bin/npm-cli.js:79
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:       let notifier = require('update-notifier')({pkg})
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:       ^^^
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]: SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at exports.runInThisContext (vm.js:53:16)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at Module._compile (module.js:374:25)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at Object.Module._extensions..js (module.js:417:10)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at Module.load (module.js:344:32)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at Function.Module._load (module.js:301:12)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at Function.Module.runMain (module.js:442:10)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at startup (node.js:136:18)
    8 月 06 10:51:01 cqh-MS-7A74 npm[4808]:     at node.js:966:3
    8 月 06 10:51:01 cqh-MS-7A74 systemd[1]: local_alarm_lua.service: Control process exited, code=exited status=1
    8 月 06 10:51:01 cqh-MS-7A74 systemd[1]: local_alarm_lua.service: Unit entered failed state.
    8 月 06 10:51:01 cqh-MS-7A74 systemd[1]: local_alarm_lua.service: Failed with result 'exit-code'.
    8 月 06 10:51:01 cqh-MS-7A74 systemd[1]: local_alarm_lua.service: Service hold-off time over, scheduling restart.
    8 月 06 10:51:01 cqh-MS-7A74 systemd[1]: Stopped local alarm service write by lua.
    -- Subject: local_alarm_lua.service 单元已结束停止操作
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    -- 
    
    

    所以, 这个 service 到底该怎么写呢?

    第 1 条附言  ·  2018-08-07 09:00:04 +08:00
    新的配置
    ```
    [Unit]
    Description= local alarm service write by eggjs

    [Service]
    Type=forking
    WorkingDirectory=/home/cqh/local_code/tscode/local_alarm
    ExecStart=/home/cqh/git/nvm/versions/node/v10.3.0/bin/npm run start
    User=cqh
    Restart=on-failure
    ExecStop=/home/cqh/git/nvm/versions/node/v10.3.0/bin/npm run stop
    Environment=PATH=/home/cqh/git/nvm/versions/node/v10.3.0/bin/

    [Install]
    WantedBy=multi-user.target


    ```
    第 2 条附言  ·  2018-08-07 09:00:43 +08:00
    错误提示
    ```
    -- local_alarm_js.service 单元已开始启动。
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: > [email protected] start /home/cqh/local_code/tscode/local_alarm
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: > egg-scripts start --daemon --title=egg-server-local_alarm
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! file sh
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! path sh
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! code ELIFECYCLE
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! errno ENOENT
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! syscall spawn sh
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! [email protected] start: `egg-scripts start --daemon --title=egg-server-local_alarm`
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! spawn sh ENOENT
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR!
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! Failed at the [email protected] start script.
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! A complete log of this run can be found in:
    8 月 07 08:58:54 cqh-MS-7A74 npm[7537]: npm ERR! /home/cqh/.npm/_logs/2018-08-07T00_58_54_407Z-debug.log
    8 月 07 08:58:54 cqh-MS-7A74 systemd[1]: local_alarm_js.service: Control process exited, code=exited status=1
    8 月 07 08:58:54 cqh-MS-7A74 systemd[1]: Failed to start local alarm service write by eggjs.
    -- Subject: local_alarm_js.service 单元已失败
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel

    ```
    14 条回复    2018-08-07 16:25:10 +08:00
    zbinlin
        1
    zbinlin  
       2018-08-06 15:29:06 +08:00   ❤️ 1
    你的 service 应该是放到 system 里的,然而你系统里默认 node 版本比较低,而你通过 nvm 安装的最新版本的 node 无法直接在 systemctl 里被用到,所有出现报错了。
    解决方式,要么把最新版的 node 安装在全局里,要么把这个 service 放到 systemctl user 里,然后在设置正确的环境变量。

    BTW: 你最好把项目里 package.json 里的 `--daemon` 参数去掉,由 systemctl 来管理。
    chenqh
        2
    chenqh  
    OP
       2018-08-06 17:12:30 +08:00
    @zbinlin 如何吧 nvm 的 node 添加到环境变量里面呢?加 path 变量?但是我还是出错了。。
    zbinlin
        3
    zbinlin  
       2018-08-06 17:42:23 +08:00   ❤️ 1
    chenqh
        4
    chenqh  
    OP
       2018-08-06 17:54:46 +08:00
    @zbinlin 我现在已经加入了 PATH 变量, 但是还是出问题了, 大佬, 能给份 systemctl 启动 eggjs 的 demo 给我吗?
    zbinlin
        5
    zbinlin  
       2018-08-06 18:45:01 +08:00   ❤️ 1
    贴下最新的 service 和失败的日志
    Rocka
        6
    Rocka  
       2018-08-06 19:06:49 +08:00 via Android   ❤️ 1
    既然 npm 的路径都写死了,为什么不把 node 的路径一起写死呢 …

    ExecStart=/path/to/node /path/to/npm run start

    不过既然都用 systemd 了,不如直接把 ExecStart 的命令改成 npm script 中的 start 对应的命令
    zjp
        7
    zjp  
       2018-08-06 19:26:03 +08:00 via Android   ❤️ 1
    [Service]
    Type=fork

    推荐 Arch Wiki 和阮一峰的博客
    chenqh
        8
    chenqh  
    OP
       2018-08-07 09:01:21 +08:00
    @zbinlin 已添加新的配置和错误日志
    zbinlin
        9
    zbinlin  
       2018-08-07 13:06:55 +08:00   ❤️ 1
    ```
    [Unit]
    Description= local alarm service write by eggjs

    [Service]
    Type=simple
    WorkingDirectory=/home/cqh/local_code/tscode/local_alarm
    ExecStart=npm run start
    User=cqh
    Restart=on-failure
    ExecStop=npm run stop
    Environment=PATH=/home/cqh/git/nvm/versions/node/v10.3.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

    [Install]
    WantedBy=multi-user.target
    ```
    chenqh
        10
    chenqh  
    OP
       2018-08-07 14:09:26 +08:00
    @zbinlin 还是不行, 还是报,
    ```
    Executable path is not absolute, ignoring: npm run start

    ```
    zbinlin
        11
    zbinlin  
       2018-08-07 15:51:26 +08:00   ❤️ 1
    呃,/尴尬

    把 ExecStart 和 ExecStop 改回用绝对路径,PATH 里加上 `/bin`:

    ```
    ExecStart=/home/cqh/git/nvm/versions/node/v10.3.0/bin/npm run start
    ExecStop=/home/cqh/git/nvm/versions/node/v10.3.0/bin/npm run stop
    Environment=/home/cqh/git/nvm/versions/node/v10.3.0/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    ```
    chenqh
        12
    chenqh  
    OP
       2018-08-07 16:00:33 +08:00
    @zbinlin 知道是怎么回事了, 原来 path 要加 /bin/sh 的 /bin;
    zbinlin
        13
    zbinlin  
       2018-08-07 16:19:33 +08:00   ❤️ 1
    @chenqh 有些发行版不需要加,比如 Arch Linux,它所有 bin 都是在 /usr/bin 里,/bin 只是一个指向 /usr/bin 的链接而已。
    chenqh
        14
    chenqh  
    OP
       2018-08-07 16:25:10 +08:00
    @zbinlin 谢谢大佬指点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5317 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:45 · PVG 11:45 · LAX 19:45 · JFK 22:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.