SA-MP Forums Archive
[Tutorial] Linux Service - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Tools and Files (https://sampforum.blast.hk/forumdisplay.php?fid=82)
+---- Thread: [Tutorial] Linux Service (/showthread.php?tid=641745)



Linux Service - Faskis - 20.09.2017

Since NGRP has been shut down and I have this service script that I setup only two weeks ago that'll no longer be used, I figure I'll share it here for anyone to use. This was made for CentOS7 but could likely be modified to work on any Unix system. It's really just a rewrite of TeamSpeak's ts3server_startscript.sh script, but it works all the same. This guide will assume that you've already setup the SA-MP server.


The first thing you're going to want to do is make sure you have a samp user account setup on your server:

Code:
adduser samp
Next, you'll want to set a password to the account, preferably something complex. You will not need this password again later, so feel free to use a randomly generated password.

Code:
passwd samp
Now that we have a working user account, we'll need to setup the server script to start, stop, and restart the server. Create a new file in the root directory of your SA-MP server (I named mine samp-server.sh):

Code:
nano /home/samp/samp-server.sh
(Text in red should be whatever your SA-MP server's path is)


Paste the following into the text editor:

Code:
#!/bin/sh
# Copyright © 2017 Faskis
# (Also technically Copyright © 2010 TeamSpeak Systems GmbH as this is copied from ts3server_startscript.sh)
# All rights reserved

COMMANDLINE_PARAMETERS="&" #add any command line parameters you want to pass here
D1=$(readlink -f "$0")
BINARYPATH="$(dirname "${D1}")"
cd "${BINARYPATH}"
LIBRARYPATH="$(pwd)"
BINARYNAME="samp03svr"

case "$1" in
	start)
		if [ -e samp.pid ]; then
			if ( kill -0 $(cat samp.pid) 2> /dev/null ); then
				echo "The server is already running, try restart or stop"
				exit 1
			else
				echo "samp.pid found, but no server running. Possibly your previously started server crashed"
				echo "Please view server_log.txt for details."
				rm samp.pid
			fi
		fi
		if [ "${UID}" = "0" ]; then
			echo WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT
			c=1
			while [ "$c" -le 10 ]; do
				echo -n "!"
				sleep 1
				c=$(($c+1))
			done
			echo "!"
		fi
		echo "Starting the SA-MP server"
		if [ -e "$BINARYNAME" ]; then
			if [ ! -x "$BINARYNAME" ]; then
				echo "${BINARYNAME} is not executable, trying to set it"
				chmod u+x "${BINARYNAME}"
			fi
			if [ -x "$BINARYNAME" ]; then
				export LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}"					
				"./${BINARYNAME}" ${COMMANDLINE_PARAMETERS} > /dev/null &
 				PID=$!
				ps -p ${PID} > /dev/null 2>&1
				if [ "$?" -ne "0" ]; then
					echo "The SA-MP server could not start"
				else
					echo $PID > samp.pid
					echo "SA-MP server started, for details please view server_log.txt"
				fi
			else
				echo "${BINARNAME} is not exectuable, cannot start the SA-MP server"
			fi
		else
			echo "Could not find binary, aborting"
			exit 5
		fi
	;;
	stop)
		if [ -e samp.pid ]; then
			echo -n "Stopping the SA-MP server"
			if ( kill -TERM $(cat samp.pid) 2> /dev/null ); then
				c=1
				while [ "$c" -le 300 ]; do
					if ( kill -0 $(cat samp.pid) 2> /dev/null ); then
						echo -n "."
						sleep 1
					else
						break
					fi
					c=$(($c+1)) 
				done
			fi
			if ( kill -0 $(cat samp.pid) 2> /dev/null ); then
				echo "Server is not shutting down cleanly - killing"
				kill -KILL $(cat samp.pid)
			else
				echo "done"
			fi
			rm samp.pid
		else
			echo "No server running (samp.pid is missing)"
			exit 7
		fi
	;;
	restart)
		$0 stop && $0 start ${COMMANDLINE_PARAMETERS} || exit 1
	;;
	status)
		if [ -e samp.pid ]; then
			if ( kill -0 $(cat samp.pid) 2> /dev/null ); then
				echo "Server is running"
			else
				echo "Server seems to have died"
			fi
		else
			echo "No server running (samp.pid is missing)"
		fi
	;;
	*)
		echo "Usage: ${0} {start|stop|restart|status}"
		exit 2
esac
exit 0
Save the file (Ctrl+O) and exit the editor (Ctrl+W). A little note on how the script works, it starts the SA-MP process itself and creates a samp.pid file which stores the process ID (PID) for reference to check if the process is running. Now we can create the actual service file:

Code:
nano /lib/systemd/system/samp.service
And the contents of the file:

Code:
[Unit]
Description=SA-MP Server
After=network.target
[Service]
WorkingDirectory=/home/samp/
User=samp
Group=samp
Type=forking
ExecStart=/home/samp/samp-server.sh start
ExecStop=/home/samp/samp-server.sh stop
PIDFile=/home/samp/sampmain.pid
RestartSec=15
Restart=always
[Install]
WantedBy=multi-user.target
(Again, text in red should be whatever your SA-MP server's path is)


Save the file and exit the editor and now we can enable the service and start the server:

Code:
systemctl --system daemon-reload
systemctl start samp.service
systemctl enable samp.service
Everything should now be setup. You can either use 'systemctl [start/stop/restart/status] samp.service' or 'service samp [start/stop/restart/status]' to manage the server.


Re: Linux Service - Chaprnks - 20.09.2017

You have probably saved me countless hours of ps aux | grep samp & kill xxx.. Thank you! <3


Re: Linux Service - Faskis - 20.09.2017

Quote:
Originally Posted by Chaprnks
View Post
You have probably saved me countless hours of ps aux | grep samp & kill xxx.. Thank you! <3
No problem. I've been disappointed with the lack of any major documentation for running Unix-based servers on here, so happy to share anything that I can.