如何通过rc.local重启Linux后台进程?

问题描述 投票:0回答:1

我的

ASP.NET Core APIs
上运行着一个非常特殊的
Linux
案例。 我有两个
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 均由

nginx
上的
port 80 and 443
提供服务,并且它们各自的 API 使用
nginx reverse proxy
来移植
5000
1880
,因为它们
.NET Core API
位于同一
AWS EC2 instance

现在我拥有重新启动这两个

.NET Core APIs
- DEV 和 PROD - 所需的所有命令 - 在
rc.local

以下是我的
rc.local
内容:

#!/bin/sh

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

 touch /var/lock/subsys/local

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

 # 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 &

# 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
# ----------------------------------------------

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

# 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 &

# 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
# ----------------------------------------------

# 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 > /etc/somesite/somesite_prod/Output3.out 2>  /etc/somesite/somesite_prod/Error3.err < /dev/null &

对于开发 -

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 &

API 工作正常,我明白

200

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

谢谢

linux amazon-ec2 background-process rc.d
1个回答
0
投票

REM 是 DOS 下的注释命令。在 Linux 中,您可以使用 # 开始注释。在 Linux 中,REM 是一个未知命令,会导致脚本中止。您是否在系统日志中查找过错误?

rc.local 以 root 身份运行。这些

sudo
都不是必要的。并且 rc.local 不在终端中运行,因此不需要
nohup
行。

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.