This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
crontab.sh
50 lines (43 loc) · 1.41 KB
/
crontab.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
#This is for crontab to restart the servers
#This script will check if server is empty or not to restart it (By using http://insurgency.pro/api)
#Using screen to start the server up
#Make sure you put in the right path
#screen name
strSandstorm=("ss1" "ss2")
#path where sandstorm folders
strPath=("sandstorm" "sandstorm2")
#IP address of the servers
strIP="108.61.136.220"
#Port of the servers
strPort=(27102 27104)
bEmpty=()
#Check if there are players in the server
for port in "${strPort[@]}"; do
#if there's NO player
if curl -s http://insurgency.pro/api/$strIP:$port 2>&1 | grep -q "\"numplayers\": 0,"; then
bEmpty+=(true)
else
bEmpty+=(false)
fi
done
for((i = 0; i < ${#strSandstorm[@]}; i++)); do
#if screen exist
if screen -ls | grep -q ${strSandstorm[$i]}; then
if [ "${bEmpty[$i]}" = true ]; then
#kill the screen when there is no player
screen -X -S ${strSandstorm[$i]} kill
sleep 1
#kill running process if screen failed to kill
if ps -ef | grep -q -e "-multihome=$strIP" | grep "${strPort[$i]}" | grep -v 'grep'; then
kill -9 `ps -ef | grep -e "-multihome=$strIP" | grep "${strPort[$i]}" | grep -v 'grep' | awk '{print $2}'`
fi
fi
fi
#if screen doesn't exist then run the server
if ! screen -ls | grep -q ${strSandstorm[$i]}; then
#locate start up and start it with screen
cd /home/${strPath[$i]}
screen -S ${strSandstorm[$i]} -d -m ./start.sh
fi
done