简体   繁体   English

如何通过 rc.local 在 Linux 中重新启动后台进程?

[英]How to restart a background process in Linux through rc.local?

I have a very peculiar case of ASP.NET Core APIs running on my Linux .我有一个非常特殊的ASP.NET Core APIs在我的Linux上运行的情况。 I have two environments我有两个environments

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

Both UIs are served by nginx on port 80 and 443 and their respective APIs are using nginx reverse proxy to port 5000 and 1880 since they are .NET Core API on the same AWS EC2 instance两个 UI 都由nginxport 80 and 443提供服务,它们各自的 API 使用nginx reverse proxy到端口50001880因为它们是同一AWS EC2 instance上的.NET Core API

Now I have all my command required to restart these two .NET Core APIs - DEV and PROD - in rc.local现在,我拥有在rc.local重新启动这两个.NET Core APIs (DEV 和 PROD)所需的所有命令

Following is my rc.local content:以下是我的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

When the system reboot I see the API running as a BG process but I get 400 Bad request当系统重新启动时,我看到 API 作为 BG 进程运行,但我收到400 Bad request

But When I start the same API from the terminal using the same command in the file ie但是当我使用文件中的相同命令从终端启动相同的 API 时,即

For PROD - 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 &对于 PROD - 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 &

For DEV - 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 &对于 DEV - 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 &

The APIs work fine the I get 200 API 工作正常,我得到200

I am not sure what I am missing here, if anyone can help.如果有人可以提供帮助,我不确定我在这里缺少什么。

Thanks谢谢

REM is the comment command for DOS. REM 是 DOS 的注释命令。 In Linux, you start a comment with # .在 Linux 中,注释以 # 开头。 In Linux, REM is an unknown command and will cause the script to abort.在 Linux 中,REM 是一个未知命令,会导致脚本中止。 Have you looked in the syslog for errors?您是否查看了系统日志中的错误?

rc.local runs as root. rc.local 以 root 身份运行。 None of those sudo s are necessary.这些sudo都不是必需的。 And rc.local doesn't run in a terminal, so the nohup lines are not needed.并且 rc.local 不在终端中运行,因此不需要nohup行。

Don't make text files executable.不要使文本文件可执行。 Do chmod 644, not chmod 777.执行 chmod 644,而不是 chmod 777。

There's no need to remove the previous log files.无需删除以前的日志文件。 Your redirects will overwrite them.您的重定向将覆盖它们。 Plus, your "rm" will fail if the files don't already exist.另外,如果文件不存在,您的“rm”将失败。

Don't start nginx that way.不要以这种方式启动 nginx。 I don't know what distribution you're using, but there are well-defined ways to say "nginx must start at boot time".我不知道您使用的是什么发行版,但是有明确的方法可以说“nginx 必须在启动时启动”。 That's what you should be using to start somesite_core_api_dev, but we can let that go.这就是你应该用来启动 somesite_core_api_dev 的东西,但我们可以放手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM