Find the answer to your Linux question:
Page 1 of 4 1 2 3 4 LastLast
Results 1 to 10 of 36
Hello, There is any way to the server just restart a server if it crash? im thinking in TeamSpeak and MInecraft server's some times it crash and i wod like ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    May 2012
    Posts
    117

    Crash Server


    Hello,

    There is any way to the server just restart a server if it crash?

    im thinking in TeamSpeak and MInecraft server's some times it crash and i wod like to know if there is any way to the Linux(CentOS) when that appen restart automatly?

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    one way you could do this is to set up a cron job to monitor your process (minecrafter server, e.g.) and start it up, if for whatever reason it is found to be stopped.

    i have no idea how minecraft works, but let use say it is a binary, named "minecraft.bin", let us further say that it is started by a Bash script, called "/root/start_minecraft.sh".

    what you would do is write a simple Bash script, we'll call it "/root/cron_minecraft.sh". here's an example of the script, with comments to explain what it does:

    Code:
    #!/bin/bash
    
    # use pidof to see if minecraft process is running
    pid=$(/sbin/pidof minecraft.bin)
    
    # see if pid was found
    if [ -n "$pid" ]; then
      echo "Minecraft (pid $pid) is running"
      exit 0
    else
      echo "Minecraft is not running"
    fi
    
    # if we got here, we need to start it up
    printf "Starting Minecraft: "
    /root/start_minecraft.sh
    rc=$?
    if [ $? -eq 0 ]; then
      echo OK
    else
      echo FAILED
    fi
    
    # exit the script with the exit status from the start_minecraft.sh script
    exit $rc
    Make it executable:
    Code:
    chmod +x /root/cron_minecraft.sh
    You should execute it from the command line, to verify that it works properly. Obviously, you'll need to modify it a bit, at least to change the name of the minecraft binary/process and the name of the script that starts it.

    Now you need to add an entry to root's cron schedule. the cron daemon checks every minute for cron jobs scheduled to run, so you just need to supply your interval and script name to it. the directions below will use the system cron. if you want to do it as a regular user, just use crontab -e.

    1. cd into the system cron directory:
    Code:
    /etc/cron.d
    2. create a new file with an editor, call it "minecraft"

    3. in it, put the following:
    Code:
    # run minecraft cronjob every 5 min, send script output to log
    */5 * * * * root /root/cron_minecraft.sh > /tmp/minecraft-cronjob.log 2>&1
    Now every 5 minutes, the cron daemon will run your cronjob (as user root, denoted by the field preceding the path to the script), which will check for minecraft process, and restart it, if not running. any problems with the script should be caught in the log file cron creates in /tmp. also you can check /var/log/cron for possible errors.

    note that if you use the crontab -e method to create a cronjob as a regular user, make sure you leave out that field denoting the user (root in my example above), as the user will already be implied.

  3. #3
    Linux Newbie
    Join Date
    May 2012
    Posts
    117
    And in the case off teamspeak server?

    confirm if is this please:

    1- create a file "/root/joaogl/Documents/ts3/ts3/cron_ts3.sh"

    Code:
    #!/bin/bash
    
    pid=$(/sbin/pidof ts3s.bin)
    
    if [ -n "$pid" ]; then
      echo "TeamSpeak (pid $pid) is running"
      exit 0
    else
      echo "TeamSpeak is not running"
    fi
    
    printf "Starting TeamSpeak: "
    /root/joaogl/Documents/ts3/ts3/ts3s.sh
    rc=$?
    if [ $? -eq 0 ]; then
      echo OK
    else
      echo FAILED
    fi
    
    exit $rc

    after that type this in the terminal:

    Code:
    chmod +x /root/joaogl/Documents/ts3/ts3/cron_ts3.sh
    Than create a file : "/root/joaogl/Documents/ts3/ts3/teamspeak"
    Code:
    */1 * * * * root /root/cron_minecraft.sh > /tmp/minecraft-cronjob.log 2>&1


    just a question the last file "/root/joaogl/Documents/ts3/ts3/teamspeak" is only teamspeak or is teamspeak.sh or something like that?

    secound how can i check pid process of teamspeak?

    and just a simple question normally the pid's are number no?

    and everything is now OK?

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by Joaogl View Post
    And in the case off teamspeak server?

    confirm if is this please:

    1- create a file "/root/joaogl/Documents/ts3/ts3/cron_ts3.sh"

    Code:
    #!/bin/bash
    
    pid=$(/sbin/pidof ts3s.bin)
    
    if [ -n "$pid" ]; then
      echo "TeamSpeak (pid $pid) is running"
      exit 0
    else
      echo "TeamSpeak is not running"
    fi
    
    printf "Starting TeamSpeak: "
    /root/joaogl/Documents/ts3/ts3/ts3s.sh
    rc=$?
    if [ $? -eq 0 ]; then
      echo OK
    else
      echo FAILED
    fi
    
    exit $rc

    after that type this in the terminal:

    Code:
    chmod +x /root/joaogl/Documents/ts3/ts3/cron_ts3.sh
    Than create a file : "/root/joaogl/Documents/ts3/ts3/teamspeak"
    Code:
    */1 * * * * root /root/cron_minecraft.sh > /tmp/minecraft-cronjob.log 2>&1


    just a question the last file "/root/joaogl/Documents/ts3/ts3/teamspeak" is only teamspeak or is teamspeak.sh or something like that?

    secound how can i check pid process of teamspeak?

    and just a simple question normally the pid's are number no?

    and everything is now OK?
    i think you better focus on just getting minecraft working as a cronjob first. or is it already? i'm guessing it isn't, otherwise you wouldn't be asking these questions.

    i will start over, by asking you some questions.

    1. how EXACTLY do you start your Minecraft server? show the exact command(s) including full paths to files, and also include who is running it.

    2. be sure to indicate if it is a script or a binary that starts the server. for example, if you execute "/root/start_mc" to start it, then you could use the file program to tell you what that file is, e.g.:

    Code:
    file /root/start_mc
    it will likely report that it is either an ELF executable or ASCII text executable.

    3. once you have started your minecraft server, look for it in the output of the ps command. you'll want to pipe the output of ps to grep and search for the name of the program or binary that is your minecraft server. you'll have to tell us what that is. for starters, try something like:

    Code:
    ps auxww|grep -i minecraft
    to sum it up, to monitor your either your minecraft server or teamspeak server, you'll need:

    1. to know the binary program (if any) that IS your server program.

    2. the name of whatever script you are supposed to use (if any) to start the aforementioned server. if one doesn't exist, you can write one.

    3. to write your own cronjob bash script, which will look for the pid of the program (from 1. above) and call the starter script (from 2. above) if not running.

    4. add the cronjob bash script (from 3. above) to the system or user cron schedule.

    once you have the script from step 2 and the script from step 3 in place, you should run them from the command line to make sure they work. have you done this yet for the minecraft server?

    and yes, a pid is a number. every process running on your system (starting with init, which is pid 1), will get a pid (Process ID).

  6. #5
    Linux Newbie
    Join Date
    May 2012
    Posts
    117
    I just used the minecraft as an example becouse the minecraft crash but there is a reson : VPS Memery : but when the minecraft crash it didnt close, we have to enter on is console and stop it than turn it on again.

    I want this to understand, to use in the TeamSpeak and if is necessery to another server i know how to do it....

    to start the minecraft:

    "cd /home/joaogl/Documents/mine"
    "sh craftbukkit.sh"
    than it opens the server and the minecraft server console.


    the mineCraft server is a .jar file and to open it we have to create thios file "Minecraft.sh" with the code

    Code:
     #!/bin/sh
     BINDIR=/home/joaogl/Documents/mine
     cd ""
     java -Xmx1024M -Xms1024M -jar craftbukkit.jar
     chmod +x craftbukkit.sh
    to start the TeamSpeak Server:

    "cd /home/joaogl/Documents/ts3/ts3"
    "sh ts3s.sh start"
    than its says:

    Code:
    Starting the TeamSpeak 3 server
    TeamSpeak 3 server started, for details please view the log file
    and its done:


    with the code:
    Code:
    ps auxww|grep -i minecraft
    it says:

    Code:
    joaogl    7062  0.0  0.0   4328   760 pts/4    S+   07:48   0:00 grep -i minecraft

    once you have the script from step 2 and the script from step 3 in place, you should run them from the command line to make sure they work. have you done this yet for the minecraft server?
    no i didn't.

    to sum it up, to monitor your either your minecraft server or teamspeak server, you'll need:

    1. to know the binary program (if any) that IS your server program.

    2. the name of whatever script you are supposed to use (if any) to start the aforementioned server. if one doesn't exist, you can write one.

    3. to write your own cronjob bash script, which will look for the pid of the program (from 1. above) and call the starter script (from 2. above) if not running.

    4. add the cronjob bash script (from 3. above) to the system or user cron schedule.
    one word: What?
    i didn't understand this part...


    THANK YOUU

  7. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    okay, i didn't quite understand your minecraft script stuff. but anyway, i'll forge ahead.

    first of all, i take it your minecraft server is a java application called "craftbukkit.jar". go ahead and start it up (if not already running), and make sure it is running, however you do that. then look for its pid in the output of ps, e.g.:

    Code:
    ps auxww|grep craftbukkit.jar
    if that one doesn't work, get a list of java pids, then look at those. try this simple script:

    Code:
    #!/bin/bash
    java_pids=$(pidof java)
    [ -z "$java_pids" ] && echo java not running && exit 1
    pids=$(echo $java_pids|sed -e 's| |,|g')
    ps -p $pids -o pid,user,cmd
    hopefully you'll see a line (or two) referring to it. post the output here, so we can determine if this is a good way to see if the server is running. once we've got that down, we can finish writing your cron monitoring script.

    along the same lines, we need to know what program(s) to monitor for Teamspeak. post the script that starts that ("ts3s.sh"), if you don't mind.

  8. #7
    Linux Newbie
    Join Date
    May 2012
    Posts
    117
    Hello,

    For minecraft i dont need this just becouse the server crash and reason low RAM in the VPS so the server is open and on but every one see it off....

    so i need this to teamSpeak and its really important...

    Thank's

  9. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    post the output of your teamspeak script, so we can figure out what processes are started up that we can monitor.

    for example, based upon what you've described regarding your teamspeak installation, try these commands:
    Code:
    cd /home/joaogl/Documents/ts3/ts3
    cat ts3s.sh

  10. #9
    Linux Newbie
    Join Date
    May 2012
    Posts
    117
    Code:
    [joaogl@localhost ~]$ cd /home/joaogl/Documents/ts3/ts3
    [joaogl@localhost ts3]$ cat ts3s.sh
    #!/bin/sh
    # Copyright (c) 2010 TeamSpeak Systems GmbH
    # All rights reserved
    
    COMMANDLINE_PARAMETERS="${2}" #add any command line parameters you want to pass here
    D1=$(readlink -f "$0")
    BINARYPATH="$(dirname "${D1}")"
    cd "${BINARYPATH}"
    LIBRARYPATH="$(pwd)"
    
    if [ -e "ts3server_linux_x86" ]; then
    	BINARYNAME="ts3server_linux_x86"
    elif [ -e "ts3server_linux_amd64" ]; then
    	BINARYNAME="ts3server_linux_amd64"
    elif [ -e "ts3server_freebsd_x86" ]; then
    	BINARYNAME="ts3server_freebsd_x86"
    elif [ -e "ts3server_freebsd_amd64" ]; then
    	BINARYNAME="ts3server_freebsd_amd64"
    else
    	echo "Could not locate binary file, aborting"
    	exit 5
    fi
    
    case "$1" in
    	start)
    		if [ -e ts3server.pid ]; then
    			if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    				echo "The server is already running, try restart or stop"
    				exit 1
    			else
    				echo "ts3server.pid found, but no server running. Possibly your previously started server crashed"
    				echo "Please view the logfile for details."
    				rm ts3server.pid
    			fi
    		fi
    		if [ "${UID}" = "0" ]; then
    			echo WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT
    			c=1
    			while [ "$c" -le 10 ]; do
    				echo -n "!"
    				sleep 1
    				c=$((++c))
    			done
    			echo "!"
    		fi
    		echo "Starting the TeamSpeak 3 server"
    		if [ -e "$BINARYNAME" ]; then
    			if [ ! -x "$BINARYNAME" ]; then
    				echo "${BINARYNAME} is not executable, trying to set it"
    				chmod u+x "${BINARYNAME}"
    			fi
    			if [ -x "$BINARYNAME" ]; then
    				export LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}"					
    				"./${BINARYNAME}" ${COMMANDLINE_PARAMETERS} > /dev/null & 
    				echo $! > ts3server.pid
    				echo "TeamSpeak 3 server started, for details please view the log file"
    			else
    				echo "${BINARNAME} is not exectuable, cannot start TeamSpeak 3 server"
    			fi
    		else
    			echo "Could not find binary, aborting"
    			exit 5
    		fi
    	;;
    	stop)
    		if [ -e ts3server.pid ]; then
    			echo -n "Stopping the TeamSpeak 3 server"
    			if ( kill -TERM $(cat ts3server.pid) 2> /dev/null ); then
    				c=1
    				while [ "$c" -le 300 ]; do
    					if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    						echo -n "."
    						sleep 1
    					else
    						break
    					fi
    					c=$((++c)) 
    				done
    			fi
    			if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    				echo "Server is not shutting down cleanly - killing"
    				kill -KILL $(cat ts3server.pid)
    			else
    				echo "done"
    			fi
    			rm ts3server.pid
    		else
    			echo "No server running (ts3server.pid is missing)"
    			exit 7
    		fi
    	;;
    	restart)
    		$0 stop && $0 start || exit 1
    	;;
    	status)
    		if [ -e ts3server.pid ]; then
    			if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    				echo "Server is running"
    			else
    				echo "Server seems to have died"
    			fi
    		else
    			echo "No server running (ts3server.pid is missing)"
    		fi
    	;;
    	*)
    		echo "Usage: ${0} {start|stop|restart|status}"
    		exit 2
    esac
    exit 0

  11. #10
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by Joaogl View Post
    Code:
    [joaogl@localhost ~]$ cd /home/joaogl/Documents/ts3/ts3
    [joaogl@localhost ts3]$ cat ts3s.sh
    #!/bin/sh
    # Copyright (c) 2010 TeamSpeak Systems GmbH
    # All rights reserved
    
    COMMANDLINE_PARAMETERS="${2}" #add any command line parameters you want to pass here
    D1=$(readlink -f "$0")
    BINARYPATH="$(dirname "${D1}")"
    cd "${BINARYPATH}"
    LIBRARYPATH="$(pwd)"
    
    if [ -e "ts3server_linux_x86" ]; then
    	BINARYNAME="ts3server_linux_x86"
    elif [ -e "ts3server_linux_amd64" ]; then
    	BINARYNAME="ts3server_linux_amd64"
    elif [ -e "ts3server_freebsd_x86" ]; then
    	BINARYNAME="ts3server_freebsd_x86"
    elif [ -e "ts3server_freebsd_amd64" ]; then
    	BINARYNAME="ts3server_freebsd_amd64"
    else
    	echo "Could not locate binary file, aborting"
    	exit 5
    fi
    
    case "$1" in
    	start)
    		if [ -e ts3server.pid ]; then
    			if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    				echo "The server is already running, try restart or stop"
    				exit 1
    			else
    				echo "ts3server.pid found, but no server running. Possibly your previously started server crashed"
    				echo "Please view the logfile for details."
    				rm ts3server.pid
    			fi
    		fi
    		if [ "${UID}" = "0" ]; then
    			echo WARNING ! For security reasons we advise: DO NOT RUN THE SERVER AS ROOT
    			c=1
    			while [ "$c" -le 10 ]; do
    				echo -n "!"
    				sleep 1
    				c=$((++c))
    			done
    			echo "!"
    		fi
    		echo "Starting the TeamSpeak 3 server"
    		if [ -e "$BINARYNAME" ]; then
    			if [ ! -x "$BINARYNAME" ]; then
    				echo "${BINARYNAME} is not executable, trying to set it"
    				chmod u+x "${BINARYNAME}"
    			fi
    			if [ -x "$BINARYNAME" ]; then
    				export LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}"					
    				"./${BINARYNAME}" ${COMMANDLINE_PARAMETERS} > /dev/null & 
    				echo $! > ts3server.pid
    				echo "TeamSpeak 3 server started, for details please view the log file"
    			else
    				echo "${BINARNAME} is not exectuable, cannot start TeamSpeak 3 server"
    			fi
    		else
    			echo "Could not find binary, aborting"
    			exit 5
    		fi
    	;;
    	stop)
    		if [ -e ts3server.pid ]; then
    			echo -n "Stopping the TeamSpeak 3 server"
    			if ( kill -TERM $(cat ts3server.pid) 2> /dev/null ); then
    				c=1
    				while [ "$c" -le 300 ]; do
    					if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    						echo -n "."
    						sleep 1
    					else
    						break
    					fi
    					c=$((++c)) 
    				done
    			fi
    			if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    				echo "Server is not shutting down cleanly - killing"
    				kill -KILL $(cat ts3server.pid)
    			else
    				echo "done"
    			fi
    			rm ts3server.pid
    		else
    			echo "No server running (ts3server.pid is missing)"
    			exit 7
    		fi
    	;;
    	restart)
    		$0 stop && $0 start || exit 1
    	;;
    	status)
    		if [ -e ts3server.pid ]; then
    			if ( kill -0 $(cat ts3server.pid) 2> /dev/null ); then
    				echo "Server is running"
    			else
    				echo "Server seems to have died"
    			fi
    		else
    			echo "No server running (ts3server.pid is missing)"
    		fi
    	;;
    	*)
    		echo "Usage: ${0} {start|stop|restart|status}"
    		exit 2
    esac
    exit 0
    okay, so figure out what the value of BINARYNAME is when you run the above script. that is the binary program you'll want to monitor.

    then write a simple script that looks for that process running in the output of the pidof program, e.g.:
    Code:
    pid=$(pidof $BINARYNAME)
    then the rest of my code from the minecraft monitor script that i previously posted can be re-used.

Page 1 of 4 1 2 3 4 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •