[英]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
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 都由nginx
在port 80 and 443
提供服务,它们各自的 API 使用nginx reverse proxy
到端口5000
和1880
因为它们是同一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)所需的所有命令
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
400 Bad request
当系统重新启动时,我看到 API 作为 BG 进程运行,但我收到400 Bad request
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
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.