Hi, I need a script that i can run in a cron to see if the server is running if its not start it, i know there is one here already but i don't find it.
We are migrating towards a new forum system located at community.teamspeak.com, as such this forum will become read-only on January 29, 2020
Hi, I need a script that i can run in a cron to see if the server is running if its not start it, i know there is one here already but i don't find it.
http://forum.teamspeak.com/showthread.php/95336-
Not quite a "cron", but something much more useful.
thats not what i wanted... read please i thin i explain well
http://kolobok.us/smiles/standart/facepalm.gif
Read what Cacti is, before throwing your anger at someone or something you don't understand.
Hello,
I've developed a script, which is checking different states of the TeamSpeak server. If something is imperfect the script will fix this issue and restart the server.
Possible issues are following:
- If PID file of TeamSpeak server does not exist, start server
- If PID file of TeamSpeak server does not exist, but the server is running, restart the server
- If PID file of TeamSpeak server exists, but server is not running, delete PID file and start server
This scrip is just running as root, because as root you don't have to change the user each time for each server instance. root will work as that user, which owns the files of the TeamSpeak server instance. root will also start the instances as this user with the command "su".
The script will also send you an email, if your server was down. Please make sure, that you've installed the needed software "mutt" and that is it working correctly. At the start of the script you will find a variable called "EMAIL_OF_ADMIN". Please replace my email address "[email protected]" with your own.
You can use this script by using following syntax:
For example:Code:./startTeamSpeakServerIfDied.sh PATH_TO_TEAMSPEAK_ROOT_DIRECTORY [PATH_TO_OTHER_TEAMSPEAK_ROOT_DIRECTORY] [PATH_TO_OTHER_TEAMSPEAK_ROOT_DIRECTORY]
This will check the TeamSpeak server instances "/home/teamspeak" and "/home/support_teamspeak".Code:./startTeamSpeakServerIfDied.sh /home/teamspeak /home/support_teamspeak
If you want to automate this, you just have to create a cronjob file like this:
Following the code of the script, which checks the state of your TeamSpeak server:Code:$ cat /etc/cron.d/CheckTeamSpeakServerStatus PATH=/usr/local/bin:/usr/bin:/bin # Check each minute status of TeamSpeak 3 servers and re-/start them if needed */1 * * * * root /home/sebastian/startTeamSpeakServerIfDied.sh /home/teamspeak # ^ ^ ^ ^ ^ # | | | | | # | | | | |___ Weekday (0-7, Sunday is mostly 0) # | | | |_____ Month (1-12) # | | |_______ Day (1-31) # | |_________ Hour (0-23) # |___________ Minute (0-59)
Code:$ cat startTeamSpeakServerIfDied.sh #!/usr/bin/env bash # # About: This script checks, if your TeamSpeak 3 instance is still running or not. If your server seems like died, the script will try to start it. # # Author: Sebastian Kraetzig <[email protected]> # EMAIL_OF_ADMIN="[email protected]" SCRIPT_VERSION="1.0" LAST_EDIT_DATE="2014-04-12" # Clear shell display clear echo -e "\nAbout: This script checks, if your TeamSpeak 3 instance is still running or not. If your server seems like died, the script will try to start it."; echo "Author: Sebastian Kraetzig <[email protected]>"; echo -e "License: GNU GPLv3\n"; echo -e "Version: $SCRIPT_VERSION ($LAST_EDIT_DATE)\n"; echo -e "-------------------------------------------------\n"; # Make sure, that the user has root permissions if [[ "$(whoami)" != "root" ]]; then echo "[ ERROR ] This script needs root permissions."; exit 0 fi if [ -z "$1" ]; then echo "You have to provide one parameter (= path to TeamSpeak root directory) at least!"; exit 0; fi for TEAMSPEAK_DIRECTORY in $*; do # Change into TeamSpeak directory cd $TEAMSPEAK_DIRECTORY # Get owner and group of TeamSpeak 3 server files USER="$(stat --format='%U' ts3server_startscript.sh)" # Just check everything, if the file ts3server_startscript.sh exists if [ -f ts3server_startscript.sh ]; then # If PID file of TeamSpeak server does not exist, start server if [ ! -f ts3server.pid ]; then # Try to start the TeamSpeak server if [ "$(su -c './ts3server_startscript.sh start' $USER)" == "The server is already running, try restart or stop" ]; then # Try to restart the TeamSpeak server if [ $(su -c './ts3server_startscript.sh restart' $USER) ]; then echo "Your server was running, but the ts3server.pid was deleted. The server was restarted to prevent unusual issues." | mutt -s "TeamSpeak server $TEAMSPEAK_DIRECTORY was restarted" -- $EMAIL_OF_ADMIN fi else echo "Your server was not running. The server was started right now." | mutt -s "TeamSpeak server $TEAMSPEAK_DIRECTORY was started" -- $EMAIL_OF_ADMIN fi elif [ -f ts3server.pid ]; then # Is the TeamSpeak server really running? if [ "$(su -c './ts3server_startscript.sh status' $USER)" != "Server is running" ]; then # Delete PID file if [ $(rm ts3server.pid) ]; then if [ $(su -c './ts3server_startscript.sh start' $USER) ]; then echo "Your server was not running, but the ts3server.pid existed. The ts3server.pid was deleted and the server was started." | mutt -s "TeamSpeak server $TEAMSPEAK_DIRECTORY was down" -- $EMAIL_OF_ADMIN fi fi else echo "TeamSpeak server in '$TEAMSPEAK_DIRECTORY' is working. No issue or downtime." fi fi else echo "'$TEAMSPEAK_DIRECTORY' is no root directory of a TeamSpeak server!" | mutt -s "$TEAMSPEAK_DIRECTORY is no root directory" -- $EMAIL_OF_ADMIN fi # Go back to old directory cd - > /dev/null done exit 0;
This is a very dangerous automation. What if TS server was started as foreground process to debug certain issues? (Therefore no PID.)
Your script will kill the server, interrupting the work someone was doing.
Not to mention, if you restart the server a little too many times in a row, it may be blocked on the accounting server for as long as you're trying to restart it over and over again.
This is just a few examples.
The correct solution would be to alert the server administrator(s) if anything is amiss, rather than blindly doing something potentially intrusive, if not dangerous.
P.S.
*/1 is semantically equal to just *.
No, because it's just restarting the server, if it isn't working correct. I had one of this issues just two times in the last 6 months.
In this case a good administrator will stop the cronjob.
The script is not restarting the server each hour or day... It's just restarting the server, if one of the named issues occurs. This occurs very rare.
The user, which is using the script is able to edit this script. If he just want an alert, he just have to adjust the script for himself. But my target of this script was to automate this, because it is irritating to restart or start the server at 3 AM, when I'm sleeping or not at home.
Cool, nice to know.
Assuming he would know of such cronjob's existence. It looks like you are making ALOT of assumptions.
The truth is, good system administrator expect a system to behave in a predictable way.
When you make it to behave unpredictable, or even erratic, you are not solving any issues, but rather - creating even bigger problem.
If a service does not work right, you need to find out, why, and fix the problem, not just whack the button again and again in hope that something would happen differently on the next restart.
"Nothing changes" would be the best possible outcome. In worst case, you could damage the situation to complete irreparability. Good if you happen to have backups...
Your cronjob does exactly what it does - it attempt to restart the service every minute. Any "if's" not apply. When you evaluate the design of application, you MUST take worst possible use cases in account.
Seriously, before publishing some code, espeically something dangerous, do yourself a favor, and read a little about software testing in general.
There are currently 1 users browsing this thread. (0 members and 1 guests)