gpt4 book ai didi

node.js - 在 Ubuntu 中将 Node.js 脚本作为服务运行的最佳方式是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 02:48:59 34 4
gpt4 key购买 nike

我有一个 Node.js 脚本来保存我的 MongoDB数据库和 CRM数据库实时同步。

我想在我的 Ubuntu 服务器上运行此脚本作为后台任务。我发现this solution ,但这对我不起作用。还有其他方法可以达到这个目的吗?

最佳答案

如果您只想启动应用程序,可以使用ForeverPM2用于运行和崩溃时自动重新启动。 但是,这不是后台任务

对于在服务器重新启动时启动的后台任务,您链接的帖子是正确的方法。如果它不起作用,也许这篇文章会对您有所帮助。这是来自 Express 官方网站: Process managers for Express apps

基本上,你创建

[Unit]
Description="My Express App"

[Service]
ExecStart=/usr/bin/node server.js
WorkingDirectory=/project/absolute/path
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyApp
Environment=NODE_ENV=production PORT=8080

[Install]
WantedBy=multi-user.target

进入 /etc/systemd/system/my-app.service 文件,然后使用 systemctl启动它:

systemctl enable my-app.service
systemctl start my-app.service

现在假设您的 Linux 发行版可以使用 systemctl。如果您的 Linux 发行版适用于 upstart 或其他东西,那么您需要在 google 上搜索该进程管理器的说明。

关于node.js - 在 Ubuntu 中将 Node.js 脚本作为服务运行的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50472113/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com