How to restart a background process in Linux through rc.local? - linux

I have a very peculiar case of ASP.NET Core APIs running on my Linux.
I have two environments
PROD - https://somesite.com - UI and it's API endpoint - https://somesite.com/api
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

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.

Related

Why use sleep after chmod in a Dockerfile

The Azure documentation gives instructions for how to enable SSH in a custom container. They suggest to add these commands in my Dockerfile:
# Install OpenSSH and set the password for root to "Docker!". In this example, "apk add" is the install instruction for an Alpine Linux-based image.
RUN apk add openssh \
&& echo "root:Docker!" | chpasswd
# Copy the sshd_config file to the /etc/ssh/ directory
COPY sshd_config /etc/ssh/
# Copy and configure the ssh_setup file
RUN mkdir -p /tmp
COPY ssh_setup.sh /tmp
RUN chmod +x /tmp/ssh_setup.sh \
&& (sleep 1;/tmp/ssh_setup.sh 2>&1 > /dev/null)
# Open port 2222 for SSH access
EXPOSE 80 2222
Why is there a sleep 1 after the chmod +x command? I know it's not harmful, but I'd really like to understand why it's there.
The sleep 1 command is there to pause the script for 1 second before continuing. It is often used as a way to give the system time to complete a task or stabilize before the script continues.
In this case, the chmod +x command is used to make the ssh_setup.sh script executable. It is likely that the sleep 1 command is included to give the system time to complete this task before the script is run.
Keep in mind that this is just a suggestion and the use of the sleep 1 command may not be necessary in all cases. It is included as a way to potentially avoid any issues that may arise if the script continues before the system has had a chance to fully process the previous command.

Ubuntu Script run a series of script one line by one line automatically

I am a new Ubuntu user.
Recently, I try to set up a server on Ubuntu.
I am wondering how to write a automatically script to run a series of script one by one.
For example, I need to install squid first, after that I need to make a copy of config file then modify the file. The following are the steps that I write in the command console. I wonder how to make a script to run that automatically.
sudo apt-get install squid -y;
cd /etc/squid3;
sudo cp squid.conf squid.conf.bak;
sudo rm -rf squid.conf;
sudo nano squid.conf
Just add a shebang, place everything in a ".sh" file, make the file executable, and run it...
Save this as test.sh
#!/bin/bash
sudo apt-get install squid -y;
cd /etc/squid3;
sudo cp squid.conf squid.conf.bak;
sudo rm -rf squid.conf;
sudo nano squid.conf
Make it executable chmod +x test.sh
Run it... ./test.sh
To edit the file from a terminal
Get a terminal on the box where you want the script to live. Probably you will SSH into it.
Then just cd to the path you want the script to live and do the following...
nano test.sh This opens the nano terminal text editor.
Copy the above test.sh commands, make sure to get the shebang (#!/bin/bash).
Paste the script into the nano editor, you'll need to use ctrl+v or cmd+v.
Hit the key combination of ctrl + o, hit the enter key.
Hit the key combination of ctrl + w. This exits nano. Proceed with the abov instructions.
I suggest you read up on nano so you can get more familiar with its abilities as it can save a lot of time!
I have wrote some script for my VPS and this is a example for Squid3
#!/bin/bash
function add_user () {
while true; do
echo -e "\nInsert a name for the Squid3 user (0=exit): \c"
read utente
case "$utente" in
0)
echo -e "\nGoodbye $USER!\n"
exit 0
;;
*\ *)
echo -e "\nYou can't use spaces in the name!"
sleep 2
continue
;;
*)
break
;;
esac
done
if [ ! -e '/etc/squid3/.passwd' ]; then
sudo htpasswd -c /etc/squid3/.passwd $utente
else
sudo htpasswd /etc/squid3/.passwd $utente
fi
}
function installer () {
sudo apt-get install squid3 apache2-utils -y
sudo bash -c "echo 'here
you
must
paste
your
configuration
file' > /etc/squid3/squid.conf"
sudo service squid3 restart
}
if ! [ "$(sudo which squid3)" ]; then
installer
add_user
else
add_user
fi
First run it install squid3 and apache2-utils (for htpasswd) and after make a new user.
If you run it again you can add more users.

How can I run this script automatically on startup

I have a game server on my VPS, but i have a strong problem. When it reboots(for technical reasons or something) the game server doesn't start automatically. I use this script, which is located in /home/steam/csgo-ds:
#!/bin/sh
ln -s /home/steam/csgo-ds/csgo/*.dem /var/www/html/
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
cd /home/steam/csgo-ds
chmod 777 * -R
screen -S "CS:GO Server" ./srcds_run -game csgo -usercon +game_type 0 +game_mode 0 -tickrate 64 -maxplayers 24 -maxplayers_override 24 +ip 188.116.46.148 -port 27015 +sv_setsteamaccount "XXXXX" -exec server.cfg +tv_enable 1 +tv_maxclients 0 +tv_port 27020 +tv_advertise_watchable 0 +map jb_dust2_final2
I have tried adding it to crontab, startup files and a lot more and nothing worked.
Operating system on the VPS is Ubuntu Server 64-bit 14.04 upgraded to 16.04
Ubuntu 16.04 uses systemd as init system, Follow these steps:
chmod 744 /path/to/script
Now create a unit file:
vim /etc/systemd/system/csgo.service
[Unit]
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/path/to/script
[Install]
WantedBy=default.target
Set permissions:
chmod 664 /etc/systemd/system/csgo.service
Reload and enable the service:
systemctl daemon-reload
systemctl enable csgo.service
Now reboot and test it out.
there are different ways of doing this , the easiest way is to put 5 line of your code in :
/etc/rc.local
it will be executed automatically on each os boot
you should put your lines of code under this line:
exit 0

cgMiner Auto-Start on Raspbian

I am trying to make cgMiner auto-start when my Raspberry Pi (Raspbian Linux) starts.
Edited the rc.local file:
sudo nano /etc/rc.local
and added this line:
nohup ./cgminer-3.1.1/cgminer --config /home/pi/cgminer.conf -S /dev/ttyUSB0 -S /dev/ttyUSB1 >/dev/null 2>&1&
and cgMiner doesn't start. If I type in terminal the exact same line with sudo in front it works perfectly.
sudo nohup ./cgminer-3.1.1/cgminer --config /home/pi/cgminer.conf -S /dev/ttyUSB0 -S /dev/ttyUSB1 >/dev/null 2>&1&
What can I do?
I think it is related to the path or better the current working directory.
You are using ./cgminer and not a full path. So either use the full path or first cd to the directory containing the cgminer program.
Also have a look at the following page over at adafruit doing the exact same thing you are trying to accomplish
http://learn.adafruit.com/piminer-raspberry-pi-bitcoin-miner/configure-auto-start
Adafruit has the following code in /etc/rc.local
cd /home/pi/PiMiner
python PiMiner.py &
cd ..
nohup ./cgminer-3.1.1/cgminer --config /home/pi/cgminer.conf -S /dev/ttyUSB0 -S /dev/ttyUSB1 >/dev/null 2>&1&
The first and third line (the cd commands) ensure that the folder containing the folder containing the cgminer command is the current directory.
From the two cd commands adding the following command before the line containing the cgminer command would solve your issue
cd /home/pi
I seem to have found the solution for this problem, assuming it is the same as for bfgminer.
After spending a full day playing with init.d scripts, I found the simplest way is to make sure your cgminier.conf file has all the arguments in then add the following to the end of /etc/rc.local
cd /home/YOURNAME/bfgminer
sudo ./bfgminer
It ran without the sudo part, but didn't start up my block erupter without it.
I also managed to get it running from init.d, but had issues with it preventing system rebooting when done that way.
Hope that helps

Tomcat7 is tied to a shell

When my ubuntu server boots up, tomcat7 does not run (I cannot open localhost:8080)
When I ssh into my server, I am able to open localhost:8080
When I close my ssh connection, tomcat stops working again
I have this startup script in the init.d:
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export CATALINA_HOME=/home/knowroaming/apache-tomcat-7.0.34
/etc/init.d/tomcat7.sh start
I also have symbolic links to this script in the /etc/ (rc1.d to rc5.d) directories.
Any ideas?
The following is from howtogeek.com and relates to tomcat 6 but I've used the instructions with tomcat7
http://www.howtogeek.com/howto/linux/installing-tomcat-6-on-ubuntu/
Automatic Starting
To make tomcat automatically start when we boot up the computer, you can add a script to make it auto-start and shutdown.
sudo vi /etc/init.d/tomcat
Now paste in the following:
# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid
export JAVA_HOME=/usr/lib/jvm/java-6-sun
case $1 in
start)
sh /usr/local/tomcat/bin/startup.sh
;;
stop)
sh /usr/local/tomcat/bin/shutdown.sh
;;
restart)
sh /usr/local/tomcat/bin/shutdown.sh
sh /usr/local/tomcat/bin/startup.sh
;;
esac
exit 0
You’ll need to make the script executable by running the chmod command:
sudo chmod 755 /etc/init.d/tomcat
The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.
sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat
Tomcat should now be fully installed and operational.

Resources