Ar *_*kin 2 linux process-management node.js
我有一个javascript程序,它在nodejs上运行。如果我运行以下命令:
node app.js
Run Code Online (Sandbox Code Playgroud)
它继续运行,但有时会退出。但我想在它退出时自动重新启动它。
Linux系统有没有命令可以做到这一点?请注意,我不想使用 cron 作业。
快速而肮脏的方法:如果使用 bash 那么一个简单的 bash 脚本怎么样:
\n#!/usr/bin/bash\nwhile true\ndo\n node app.js\ndone\nRun Code Online (Sandbox Code Playgroud)\n顺便说一句,这是错误的行为,因为您没有考虑任务退出的原因,并且可能有很好的理由不重新启动它。不是说启动时也可能崩溃,被oomkilled\xe2\x80\xa6
\n在系统化的 Linux 下更规范的方式:(作为 @Kusalananda 注释的一部分建议,并受到The Ultimate Guide toDeploying your Node App on Linux 的启发):
\n假设 app.js 以#!/usr/bin/env node声明开头,并且文件 app.js 被标记为可执行,
设计新服务:/etc/systemd/system如果系统范围内需要,则在目录中创建一个 .service 文件~/.config/systemd/user,如果仅用户需要,则在目录中创建一个 .service 文件,如下所示:
[Unit]\nDescription= #(Whatever String You Want)\nAfter= #(Any service that should be started before, ex. network.target)\n[Service]\nExecStart= #(Key in the absolute path of app.js)\nRestart=always #(This will ensure the automatic restart)\nUser= #(TBD depending on your system & wishes)\nGroup= #(TBD depending on your system & wishes)\nEnvironment=PATH=/usr/bin:/usr/local/bin\nEnvironment=NODE_ENV=production\nWorkingDirectory= # (Absolute path of app.js working dir.)\n[Install]\nWantedBy= # multi-user.target or default.target according to your needs\nRun Code Online (Sandbox Code Playgroud)\n您现在应该可以启动它:systemctl --user start service_filename
| 归档时间: |
|
| 查看次数: |
3058 次 |
| 最近记录: |