-2

I have a very peculiar case of ASP.NET Core APIs running on my Linux. I have two environments

  1. PROD - https://somesite.com - UI and it's API endpoint - https://somesite.com/api
  2. DEV - https://somesite-dev.com - UI and it's API endpoint - 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

Now I have all my command required to restart these two .NET Core APIs - DEV and PROD - in rc.local

Following is my rc.local content:

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

But When I start the same API from the terminal using the same command in the file i.e

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 &

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 &

The APIs work fine the I get 200

I am not sure what I am missing here, if anyone can help.

Thanks

yodellingbutters
  • 285
  • 6
  • 20

1 Answers1

0

REM is the comment command for DOS. In Linux, you start a comment with # . In Linux, REM is an unknown command and will cause the script to abort. Have you looked in the syslog for errors?

rc.local runs as root. None of those sudos are necessary. And rc.local doesn't run in a terminal, so the nohup lines are not needed.

Don't make text files executable. Do chmod 644, not 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.

Don't start nginx that way. I don't know what distribution you're using, but there are well-defined ways to say "nginx must start at boot time". That's what you should be using to start somesite_core_api_dev, but we can let that go.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • I am aware REM is comment command for DOS, I just put it here due to the Stack-overflow’s editor parsing the # as heading. The File output and error always exist. I tried writing service and running it as a service too, in that case it did not start at all. – yodellingbutters Sep 26 '20 at 06:31