Linux 服务器

From SA-MP Wiki

(Redirected from Linux Server ZH)
Jump to: navigation, search

Contents

需求

  • A Linux server with SSH access, libstdc++ 6 and an SSH client such as PuTTY
  • Internet access (a full 100 player server uses roughly 4Mbps upstream)
  • A forwarded UDP Port (7777 is default) | Guide

建立服务器

下载和解压文件

First off, go to the SA-MP download page and find the URL for the Linux Server. Then use wget followed by the URL to download the SA-MP Server to your server. Then, extract the files from the archive. You might want to cd to the directory where you want to install the server, e.g /home/user

wget http://files.sa-mp.com/samp03asvr_R4.tar.gz
tar -zxf samp03asvr_R4.tar.gz
cd samp03

配置你的服务器

Once you have the files downloaded and extracted browse to the folder where you just installed the server (usually by typing cd /home/user/samp03. Next use your favorite Linux text editor eg: Pico or Nano, edit server.cfg and change the hostname, RCON password, etc - more info is on the server.cfg help and on the Advanced Server Controls page.

添加游戏模式/附加脚本到你的服务器中

Maybe you want to run another gamemode than the example gamemodes shipped with SA-MP. In the Script showroom you can find tons of script you might want to run on your server. To add them to your server, just download the desired .amx file to your <server root>\gamemodes directory, and edit your server.cfg to run this gamemmode by changing one of those gamemode<number> lines with the files name, excluding the ".amx".

This works nearly the same way with filterscripts. You also can find them in the scripting showroom. To have them on your server just copy the filterscript file to the filterscripts directory and add an entry for the file without the ".amx" at the end of the line beginning with "filterscripts" in your server.cfg.

启动你的服务器

Once you have server.cfg configured the way you want it, simply run the server by using one of the three described ways below. Each way has a description below the picture. I've picked the three most popular ways of starting your server, a small SSH script to keep your server running can be found at the bottom of this page.

NOTE: Be sure you set permissions for samp03svr and announce if you want to run an internet server! If you don't do this, you'll get a permission denied error. To set permissions, execute chmod 700 * in the SA:MP server directory. It's NOT a good idea to run the server (or any other program for that matter) under root.

Image:Samp_ssh.jpg

Method one

The first method uses the nohup command to keep the server running. nohup is a Linux command that starts a program, which will ignore any SIGHUP signals - disconnect signals. This means that the server will continue running after you close the SSH window you've opened before. The ampersand (&) at the end of the command means that the server has to run in the background, so you can continue to execute commands, even after you've started the SA-MP server.

nohup ./samp03svr &

A detailed log can be found in the file nohup.out, which will be created in the directory where you execute the command (/home/gta in this case). nohup is installed on most Linux servers.


Method two

Method two allows the SA-MP Server process to run in the background. The advantage of this is that you can continue to work in the shell-environment, so you won't have to open another window. On some Linux servers, the process will continue to run after the SSH window is closed, but this is not always the case.

./samp03svr &

There are no log files or whatsoever created. The shell will return the PID of the process just started, 9251 in the example above. This method is supported by all Linux servers.


Method three

This method is the original described one, and most likely the easiest to understand. The server will be running on top, so all errors will be directly visible as output (e.g. segmentation faults). But keep in mind that normal output, as you have on the Windows Server, is not visible on the Linux server.

./samp03svr

The server will be shutdown after the window is closed. This method might be the best for scripting and/or debugging purposes, since it's very clear.


NOTE: If you get an error saying libstdc++ 6 cannot be found, then you have to download it on your server.

保持服务器在线

With limited anti-crash options available in the SA-MP Server, there is a large possibility of receiving Segmentation Faults or any other error generated by your (Pawn) gamemode. This will shutdown the server, and you'll have to start it up yourself again.

方法 1

All you need to use the program below is a Linux environment, and the possibility to use cron. This program will check if the samp03svr process is running, and if it isn't, start it up again. You'll need to install the following crontab:

*/1 * * * * /path/to/samp/checksamp.sh >/dev/null 2>&1

Help about using crontab can be found here.

checksamp.sh:

#!/bin/sh
 
PROCESS_COUNT=$(ps -fu root | grep samp03svr | grep -v grep | wc -l)
case $PROCESS_COUNT in
0) /path/to/samp03svr &
;;
1) #OK, program is already running once
;;
*) #OK, program is already multiple times
;;
esac

Make sure that you have to correct paths set up, the script won't work if you haven't. The script is currently unable to handle multiple SA-MP Servers on the same machine, but works perfectly when there is only one running/needed. You need to chmod the checksamp.sh file to 0755 (+x)!

方法 2

If you don't want to use crontab and if you want to have server log saved to a separate directory on server crash you can use this script.

To keep your server online using this script, first you need to stop your server and then execute it with this command:

nohup ./restart.sh &

restart.sh:

#!/bin/sh
log=samp.log
dat=`date`
samp="/path/to/samp/server/samp03svr"
cd /path/to/samp/server
 
echo "${dat} watchdog script starting." >>${log}
while true; do
        echo "${dat} Server exited, restarting..." >>${log}
        mv /path/to/samp/server/server_log.txt /path/to/samp/server/logs/server_log.`date '+%m%d%y%H%M%S'`
        ${samp} >> $log
	sleep 2
done

This method will bring your server back up whenever it crashes or when the RCON exit command is issued. It will also save the server log into a separate directory.

方法 3

You can also restart the server using this pair of shell scripts. Assumed is that the server is located in the /home/sampsvr directory.

Script number 1 startgtaserver.sh:

#!/bin/bash
export GTA_PATH=/home/sampsvr
cd $GTA_PATH
while [ true ]; do
cat {$GTA_PATH}/server_log.txt >> {$GTA_PATH}/full_server_log.txt
rm {$GTA_PATH}/server_log.txt
touch {$GTA_PATH}/server_log.txt
./samp03svr
done

This one goes near the server binary.

Second script, named gtaserver.sh:

#!/bin/bash
 
server_start() {
screen /home/sampsvr/startgtaserver.sh & # put in full path and name of startup script
} 
 
server_stop() {
killall startgtaserver.sh # your startup script name
killall samp03svr # need to put in path to killall if its not in $PATH
} 
 
server_restart() {
server_stop
sleep 1
server_start
}
case "$1" in
'start')
server_start
;;
'stop')
server_stop
;;
'restart')
server_restart
;;
*)
echo "usage $0 start|stop|restart"
esac

Place the script in /usr/local/bin/.

Just change paths in both scripts to yours, then you can start your server by typing gtaserver start, stop server by typing gtaserver stop, and finally restart it by typing gtaserver restart. Make sure the scripts are executable.

Personal tools
Navigation
Toolbox
In other languages