Skip to content

Instantly share code, notes, and snippets.

@Programie
Last active August 16, 2021 19:39
Show Gist options
  • Select an option

  • Save Programie/ac254f1315b5cba9e168 to your computer and use it in GitHub Desktop.

Select an option

Save Programie/ac254f1315b5cba9e168 to your computer and use it in GitHub Desktop.
Init.d Script for SA-MP server
#! /bin/bash
# This is a simple daemon script for Linux to start you SAMP servers as daemons.
# So it is possible to auto start the SAMP servers if you restart you Linux server.
# It will also prevent you from multiple launchings of the same SAMP server.
#
# Login to you Linux server using SSH or whatever you want to use to get a terminal of you server.
# Create a directory "/opt/samp/port-of/your/samp-server".
# Download the SAMP server for Linux from the SAMP website (http://www.sa-mp.com) using wget (Example: "wget http://files.sa-mp.com/samp03bsvr_R2.tar.gz").
# Extract the archive using "tar -xf samp03bsvr_R2.tar.gz".
# Copy or move the content of the new directory into "/opt/samp/port-of-your-samp-server" using "cp -R . /opt/samp/port-of-your-samp-server/".
# Change your current directory to the servers directory using "cd /opt/samp/port-of-your-server".
# Rename your "samp03srv" into "samp-srv" using "mv samp03srv samp-srv".
# Copy this script into "/etc/init.d" and set it as executable by typing "chmod +x /etc/init.d/name-of-this-script".
# Set the port for you SAMP server by setting the "PORT" variable in the configuration section of this script.
# Add the daemon script to your daemon autostart by typing "update-rc.d name-of-this-script defaults".
# Start your SAMP server by typing "/etc/init.d/name-of-this-script start".
#
#
# Available script commands (To use with /etc/init.d/name-of-this-script command):
#
# restart Stop the SAMP server and start it again
# start Start the SAMP server
# status Check if the SAMP server is running or not
# stop Stop the SAMP server
# CONFIGURATION
PORT=7777// Port of you SAMP server
# END OF CONFIGURATION - DO NOT EDIT!
PATH=/bin:/usr/bin:/sbin:/usr/sbin
case "$1" in
start)
if [[ `sudo -u samp ps -ao pid,command | grep [/]opt/samp/$PORT/samp-srv` ]]
then
echo "SAMP-Server $PORT is already running"
else
echo -n "Starting SAMP-Server $PORT..."
cd /opt/samp/$PORT
sudo -u samp /opt/samp/$PORT/samp-srv &
echo "done"
fi
;;
stop)
if [[ `sudo -u samp ps -ao pid,command | grep [/]opt/samp/$PORT/samp-srv` ]]
then
echo -n "Stopping SAMP-Server $PORT..."
killall /opt/samp/$PORT/samp-srv &> /dev/null
echo " done."
else
echo "SAMP-Server $PORT is not running"
fi
;;
restart)
if [[ `sudo -u samp ps -ao pid,command | grep [/]opt/samp/$PORT/samp-srv` ]]
then
echo -n "Stopping SAMP-Server $PORT..."
killall /opt/samp/$PORT/samp-srv &> /dev/null
echo " done."
fi
echo -n "Starting SAMP-Server $PORT..."
cd /opt/samp/$PORT
sudo -u samp /opt/samp/$PORT/samp-srv &
echo "done"
;;
status)
if [[ `sudo -u samp ps -ao pid,command | grep [/]opt/samp/$PORT/samp-srv` ]]
then
echo "SAMP-Server $PORT is running"
else
echo "SAMP-Server $PORT is not running"
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0
@Sasino97
Copy link

Sasino97 commented Jul 4, 2019

The script is useful, however I have some critics:

  • /* */ and // do not work as comments (at least in my distro), I had to change all of them to #
  • In the info you don't specify that you need to create a "samp" user (since you are calling the commands with sudo -u samp). I added a $USER variable below PORT
  • There is no reason to rename samp03svr to samp-srv

I am using it now but I had to modify it.

@Programie
Copy link
Author

Programie commented Jul 4, 2019

Thanks for your feedback. I have written and used this script years ago (oh, it's already 5 years old). Today you might want to use something more modern like a systemd service which is ways easier to configure and manage. Or even put your SA-MP server in a Docker container.

Some advantages of a systemd service are:

  • You don't have to ensure that all processes started by the SA-MP server are also stopped when stopping the server. For example, what would happen with the samp-npc processes if the SA-MP server is being killed (e.g. out of memory killer)?
  • See the output of the SA-MP server binary in the systemd journal, syslog, etc.
  • Just a few lines of key-value pairs instead of almost 100 lines of a shell script

But in the end, it's the freedom of Linux and other Unix like systems to use whatever you want. ;-)

For your first point: You are right, I don't know why I used /* */ and // for comments. They should start with # which is AFAIK the only comment character supported by shell scripts.

Yes, this script should have used some $USER variable.

I've renamed samp03srv to samp-srv to make SA-MP updates easier (i.e. if development wouldn't have been stopped, there might be a samp04srv, then you would have to update the script to use samp04srv instead of samp03srv).

@AnonymousWebHacker
Copy link

I was making a daemon in /etc/systemc/system , but I didn't know how to pass it the parameter for the server to run, example /opt/sam-server/samp03svr

How could I do it?

@Programie
Copy link
Author

Programie commented Oct 16, 2020

In case of systemd, I would recommend not to use my script as systemd has it's own features of managing the processes of services.

In your case, create a .service file with the following content:

[Unit]
Description=San Andreas Multiplayer Server

[Service]
User=samp
ExecStart=/opt/samp-server/samp03svr
WorkingDirectory=/opt/samp-server

[Install]
WantedBy=multi-user.target

After that, save the file and trigger a reload of the systemd daemon: systemctl daemon-reload

Note: I haven't tested the service but AFAIK it should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment