Forum

> > CS2D > Servers > Bash script - Checking server status
ForenübersichtCS2D-Übersicht Servers-ÜbersichtEinloggen, um zu antworten

Englisch Bash script - Checking server status

13 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Bash script - Checking server status

AtomKuh
User Off Offline

Zitieren
Hello,

I need a bash script that checks the cs2d server list webpage (http://www.cs2d.com/servers.php) if it contains the IP of my server.
I already have a script, but it seems to be not working as I want:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

webserv="http://www.cs2d.com/servers.php" 

Keyword="MY-SERVER-IP" # enter the keyword for test content


if curl -s "$webserv" | grep "$keyword"
then
    # if the keyword is in the conent
    echo " server online"
else
    echo "Error"
fi

I changed the Keyword text to my server IP, but always get the same echo " server online" even if the IP doesnt show up in the server list.

Can someone help me, please?
2× editiert, zuletzt 15.04.17 16:12:39

alt Re: Bash script - Checking server status

AtomKuh
User Off Offline

Zitieren
This new code works fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash

while [ 1 ];
do
    count=`curl -s "http://www.cs2d.com/servers.php" | grep -c ">>>>>MY-SERVER-IP<<<<<"`

    if [ "$count" != "0" ]
    then
       echo "Server is still running"
else
       echo "Server is frozen (DELETING ALL SCREENS)"
       killall screen
       screen -d -m /home/cs2d/cs2d_dedicated &
    fi
    sleep 300
done

alt Re: Bash script - Checking server status

VADemon
User Off Offline

Zitieren
"killall screen" is a very unfortunate choice. You will kill EVERY EXISTING screen session. What if you want to start 2 copies separately or are working inside screen yourself?
screen started in -dmS "socket name" mode ends automatically when the child process quits. So just kill the targeted cs2d server process.

alt Re: Bash script - Checking server status

AtomKuh
User Off Offline

Zitieren
@user _Yank: I'm working on another way of restarting automatically the server as soon as the server is down. I thought about a lua script that runs on the server which creates periodically changes in a text document. This text document should be also examined periodically by a bash script and if there aren't noted any changes, the server will be restarted.
I hope that is a good idea

@user VADemon: I am very inexperienced in this scripting stuff and the code for my bash file which I showed is also just an edited one from stackoverflow. I'm happy enough that my version works, but I agree with you^^ If I want to host more than one server, "killall screen" is out of place.
Could u please explain in more detail this "-dmS "socket name"? I dont know how to grab the screen ID of a particular started screen

alt Re: Bash script - Checking server status

_Yank
User Off Offline

Zitieren
@user _Yank: That idea isn't actually bad however there's definately a better approach for that, I just can't think of anything right now.
Anyway, here's a tip if you're going to use it, instead of actually checking if the text file content is different (if there isn't anything you need there for other stuff), just get its "last modified date" and wether that date implies that the server is running.

You should also consider using tmux over screen, it is lighter (from what I remember), can be configured to work like screen and there's also commands and stuff that to let you control your sessions from the outside.

alt Re: Bash script - Checking server status

AtomKuh
User Off Offline

Zitieren
I got this bash script from a user on stackoverflow:

Spoiler >


I can use this file to determine whether the log of the cs2d was edited or not. The only problem I have is that I always need to change the log that belongs to the current running server.
Can someone help me to edit the bash script that it is checking the logs folder on changes instead of a single file?

alt Re: Bash script - Checking server status

_Yank
User Off Offline

Zitieren
@user AtomKuh: You don't need to get the log file hash just for checking if it has been modified, just gets its last modified date and compare it to the previous one.

By the way, have you thought about what will happen if the server modifies the file and then crashes between the checking process ? (so, even though the file is different from the previous one which was checked some time ago, the server is not running and "passes the test" anyway?)

alt Re: Bash script - Checking server status

AtomKuh
User Off Offline

Zitieren
@user _Yank: If the file was modified and crashes shortly after between the checking process, the server will simply be noticed as down in the next process. That means I have to wait another 30 seconds but that is not a problem.

I almost know nothing about writing these script for checking the last modified date. I would be glad if someone could write this for me

alt Re: Bash script - Checking server status

VADemon
User Off Offline

Zitieren
1
echo "Time elapsed since last modification: "$(expr $(date +%s) - $(stat 'YOURFILENAME' --format %X))
Source:
date --help
and
stat --help


What it does:
echo - ok
$(expr ... ...) - calculate
$(date +%s) - current date/time in seconds since Unix Epoch
$(stat --format %X) - date/time of last file modification in seconds since Unix Epoch

You probably can use the whole log folder instead of a single file as well, unless there're stability issues with that.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Servers-ÜbersichtCS2D-ÜbersichtForenübersicht