How can I run this script automatically on startup - linux

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

Related

ubuntu 18 custom service does not load

ubuntu 18
I have created a custom service at /etc/systemd/system/mycustomservice.service
and enable it : sudo systemctl enable /etc/systemd/system/mycustomservice.service
but the service does not load at start up, the content is:
[Unit]
After=mysql.service
[Service]
ExecStart=/home/myuser/runupdate.sh
[Install]
WantedBy=default.target
I try to execute the file /home/myuser/runupdate.sh without any issue
the permission of
/home/myuser/runupdate.sh is -rwxr--r--
/etc/systemd/system/mycustomservice.service is -rw-rw-r--
Please advise, thank you!
Systemd will need to know how to execute the script and what shell to use, hence there are two options Add:
#!/bin/bash
or
#!/bin/sh
to the top line of the script depending on the shell you are using. Alternatively, you can use:
ExecStart=/bin/bash -c /home/myuser/runupdate.sh

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

How to run commands on CentOS reboot

I want to run the following commands one after the other upon reboot of the server after I do shutdown -r now but not sure how to do it:
getenforce
setenforce 0
systemctl start httpd.service
I'm running CentOS 7.x
Technically you can crontab it
sudo crontab -e
and add the line
#reboot /somewhere/myscript.sh
and put the 3 commands in the myscript.sh with the proper rights.
But it is a bad idea since you can you just make these changes permanent:
systemctl enable httpd
and
vim /etc/selinux/config
to set the SELINUX variable to permissive
This will make the configuration permanent across reboot.

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.

Beaglebone inittab issue

I am developing an application in beaglebone.
I want to add start up scripts to my Beaglebone but I can not find /etc/inittab.
I am using the image : Angstrom-Cloud9-IDE-GNOME-eglibc-ipk-v2012.05-beaglebone-2012.06.18.img.xz
I think in the previous versions of image there is /etc/initab but for the new distributions I could not find the inittab :/
I want to apply this : Automatic login on Angstrom Linux
but I can not because there is no /etc/inittab.
Where is the inittab in new distributions.
When I write uname -r it gives:
3.2.23
Regards
inittab has been replaced by systemd
This is how I did it for the serial console. You can probably adapt it easily for tty1 by replacing "serial-getty#..." by "getty#...", but I haven't tested it.
cp /lib/systemd/system/serial-getty#.service /etc/systemd/system/autologin#.service
rm /etc/systemd/system/getty.target.wants/serial-getty#ttyO0.service
ln -s /etc/systemd/system/autologin#.service /etc/systemd/system/getty.target.wants/serial-getty#ttyO0.service
Create the following script file in any location (/home/root/autologin.sh in my case)
#!/bin/sh
exec /bin/login -f root
Make it executable
chmod a+x autologin.sh
Edit /etc/systemd/system/autologin#.service and update the ExecStart command by adding the -n (Do not prompt the user for a login name) and -l (Invoke the specified login_program instead of /bin/login) options.
ExecStart=-/sbin/agetty -n -l /home/root/autologin.sh -s %I 115200

Resources