【问题标题】:How to restart a background process in Linux through rc.local?如何通过 rc.local 重启 Linux 中的后台进程?
【发布时间】:2021-01-12 04:14:05
【问题描述】:

我的Linux 上运行着一个非常特殊的ASP.NET Core APIs 案例。 我有两个environments

  1. PROD - https://somesite.com - UI 和它的 API 端点 - https://somesite.com/api
  2. DEV - https://somesite-dev.com - UI 和它的 API 端点 - https://somesite-dev.com/api

两个 UI 均由 port 80 and 443 上的 nginx 提供服务,并且它们各自的 API 使用 nginx reverse proxy 端口 50001880,因为它们是同一 .NET Core API 上的 AWS EC2 instance

现在我已经拥有了重新启动这两个 .NET Core APIs 所需的所有命令 - DEV 和 PROD - 在 rc.local

以下是我的rc.local内容:

#!/bin/sh

 REM This script will be executed *after* all the other init scripts.
 REM You can put your own initialization stuff in here if you don't
 REM want to do the full Sys V style init stuff.

 touch /var/lock/subsys/local

 sudo service nginx stop
 REM DEV -------------------------------------------
 REM Removing previous Error and Output files.
 sudo rm /etc/somesite/somesite_dev/Output2.out
 sudo rm /etc/somesite/somesite_dev/Error2.err

 REM Starting the BG process.
 sudo nohup /etc/somesite/somesite_dev/somesite_core_api_dev > 
 /etc/somesite/somesite_dev/Output2.out 2> /etc/somesite/somesite_dev/Error2.err < /dev/null &

REM Changing Error and Output files permission and Ownership.
sudo chmod 777 /etc/somesite/somesite_dev/Output2.out
sudo chmod 777 /etc/somesite/somesite_dev/Error2.err
sudo chown ec2-user:ec2-user /etc/somesite/somesite_dev/Output2.out
sudo chown ec2-user:ec2-user /etc/somesite/somesite_dev/Error2.err
REM ----------------------------------------------

REM PROD -----------------------------------------
REM Removing previous Error and Output files.
sudo rm /etc/somesite/somesite_prod/Output3.out
sudo rm /etc/somesite/somesite_prod/Error3.err

REM Starting the BG process.
sudo nohup /etc/somesite/somesite_prod/somesite_core_api_prod > 
/etc/somesite/somesite_prod/Output3.out 2> 
/etc/somesite/somesite_prod/Error3.err < /dev/null &

REM Changing Error and Output files permission and Ownership.
sudo chmod 777 /etc/somesite/somesite_prod/Output3.out
sudo chmod 777 /etc/somesite/somesite_prod/Error3.err
sudo chown ec2-user:ec2-user /etc/somesite/somesite_prod/Output3.out
sudo chown ec2-user:ec2-user /etc/somesite/somesite_prod/Error3.err
REM ----------------------------------------------

REM Force Stoping and Starting nginx
sudo service nginx start

当系统重新启动时,我看到 API 作为 BG 进程运行,但我得到 400 Bad request

但是当我使用文件中的相同命令从终端启动相同的 API 时,即

对于产品 -sudo nohup /etc/somesite/somesite_prod/somesite_core_api_prod &gt; /etc/somesite/somesite_prod/Output3.out 2&gt; /etc/somesite/somesite_prod/Error3.err &lt; /dev/null &amp;

对于开发人员 - sudo nohup /etc/somesite/somesite_dev/somesite_core_api_dev &gt; /etc/somesite/somesite_dev/Output2.out 2&gt; /etc/somesite/somesite_dev/Error2.err &lt; /dev/null &amp;

API 工作正常,我得到200

如果有人可以提供帮助,我不确定我在这里缺少什么。

谢谢

【问题讨论】:

  • 想解释一下否决票?

标签: linux amazon-ec2 background-process rc.d


【解决方案1】:

REM 是 DOS 的注释命令。在 Linux 中,您以 # 开始注释。在 Linux 中,REM 是一个未知命令,会导致脚本中止。您是否在 syslog 中查看过错误?

rc.local 以 root 身份运行。这些sudos 都不是必需的。并且 rc.local 不在终端中运行,因此不需要 nohup 行。

不要使文本文件可执行。执行 chmod 644,而不是 chmod 777。

无需删除以前的日志文件。您的重定向将覆盖它们。另外,如果文件不存在,您的“rm”将失败。

不要那样启动 nginx。我不知道您使用的是什么发行版,但是有明确定义的方式可以说“nginx 必须在启动时启动”。这就是你应该用来启动 somesite_core_api_dev 的东西,但我们可以放手。

【讨论】:

  • 我知道 REM 是 DOS 的注释命令,我只是把它放在这里,因为 Stack-overflow 的编辑器将 # 解析为标题。文件输出和错误始终存在。我尝试编写服务并将其作为服务运行,在这种情况下它根本没有启动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 2013-05-25
  • 2015-01-11
  • 2013-01-13
  • 1970-01-01
  • 2018-01-15
相关资源
最近更新 更多