接受的答案建议使用 nohup。我宁愿建议使用pm2。使用 pm2 而不是 nohup 有很多优点,例如保持应用程序的活动性、维护应用程序的日志文件以及更多其他功能。更多详情check this out。
要安装 pm2,您需要下载 npm。对于基于 Debian 的系统
sudo apt-get install npm
对于 Redhat
sudo yum install npm
或者你可以关注these instruction。
安装 npm 后使用它来安装 pm2
npm install pm2@latest -g
完成后,您可以通过
启动您的应用程序
$ pm2 start app.js # Start, Daemonize and auto-restart application (Node)
$ pm2 start app.py # Start, Daemonize and auto-restart application (Python)
对于进程监控,使用以下命令:
$ pm2 list # List all processes started with PM2
$ pm2 monit # Display memory and cpu usage of each app
$ pm2 show [app-name] # Show all informations about application
使用应用名称或进程 ID 管理进程或一起管理所有进程:
$ pm2 stop <app_name|id|'all'|json_conf>
$ pm2 restart <app_name|id|'all'|json_conf>
$ pm2 delete <app_name|id|'all'|json_conf>
日志文件可以在
中找到
$HOME/.pm2/logs #contain all applications logs
二进制可执行文件也可以使用 pm2 运行。您必须对 jason 文件进行更改。将"exec_interpreter" : "node" 更改为"exec_interpreter" : "none".(参见attributes section)。
#include <stdio.h>
#include <unistd.h> //No standard C library
int main(void)
{
printf("Hello World\n");
sleep (100);
printf("Hello World\n");
return 0;
}
编译以上代码
gcc -o hello hello.c
并在后台使用 np2 运行它
pm2 start ./hello