source: scripts/usr/local/bin/7dtd-common.sh@ 13

Last change on this file since 13 was 13, checked in by alloc, 11 years ago

Instances script updated, more info in list and optional command to show a single instace with details

File size: 2.0 KB
Line 
1#!/bin/bash
2# Version 2
3
4# Provides common functions for 7dtd-scripts. Not intended to be run directly.
5
6# Check if the script is run as root (exit otherwise) and load global config
7checkRootLoadConf() {
8 if [ `id -u` -ne 0 ]; then
9 echo "This script has to be run as root!"
10 exit 10
11 fi
12 . /etc/7dtd.conf
13}
14
15# Get the config path for the given instance
16# Params:
17# 1: Instance name
18# Returns:
19# Config path for instance
20getInstancePath() {
21 echo $SDTD_BASE/instances/$1
22}
23
24# Check if the given instance name is an existing instance
25# On failure exit the script!
26# Params:
27# 1: Instance name
28checkInstance() {
29 if [ -z $1 ]; then
30 echo "No instance given!"
31 exit 2
32 fi
33 if [ ! -d $(getInstancePath $1) ]; then
34 echo "Instance $1 does not exist!"
35 exit 3
36 fi
37}
38
39# Check if the given instance is currently running
40# Params:
41# 1: Instance name
42# Returns:
43# 0 = not running
44# 1 = running
45isRunning() {
46 start-stop-daemon --status --pidfile $(getInstancePath $1)/7dtd.pid
47 if [ $? -eq 0 ]; then
48 echo 1
49 else
50 echo 0
51 fi
52}
53
54# Get the PID of the instance if it is running, 0 otherwise
55# Params:
56# 1: Instance name
57# Returns:
58# 0 if not running
59# PID otherwise
60getInstancePID() {
61 run=$(isRunning $1)
62 if [ $run -eq 1 ]; then
63 cat $(getInstancePath $1)/7dtd.pid
64 else
65 echo 0
66 fi
67}
68
69# Get a single value from a serverconfig
70# Params:
71# 1: Instance name
72# 2: Property name
73# Returns:
74# Property value
75getConfigValue() {
76 CONF=$(getInstancePath $1)/serverconfig.xml
77 xmllint --xpath "string(/ServerSettings/property[@name='$2']/@value)" $CONF
78}
79
80# Send a single command to the telnet port
81# Params:
82# 1: Instance name
83# 2: Command
84# Returns:
85# String of telnet output
86telnetCommand() {
87 TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
88 TEL_PORT=$(getConfigValue $1 TelnetPort)
89 TEL_PASS=$(getConfigValue $1 TelnetPassword)
90 if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
91 echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT
92 else
93 echo "Telnet not enabled or no password set."
94 fi
95}
Note: See TracBrowser for help on using the repository browser.