zcooler

Untitled

Sep 11th, 2013
1,730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.44 KB | None | 0 0
  1. #!/bin/sh
  2. #Teamspeak 3 NOHUP startup script v0.95
  3. #Written by mancert
  4.  
  5. # chkconfig:   - 85 15
  6. # description:  teamspeak3 server
  7. #              
  8. # processname: teamspeak3
  9. # config:      /opt/soft/teamspeak3-server_linux-x86
  10. # pidfile:     /opt/soft/teamspeak3-server_linux-x86/ts3.pid
  11.  
  12.  
  13.  
  14. ###########################################################################################
  15. # Remember to start the teamspeak server manually the first time so you get the
  16. # server admin password and the server admin token.
  17. # If you want to use mysql database, remember to create a ts3db_mysql.ini file
  18. # in the folder ts3 in installed in. And make sure ts3server.ini is set right.
  19. # Changelog @ http://forum.teamspeak.com/showthread.php?t=46989
  20. #
  21. # The test start option will check if you are missing anything before you start the script
  22. #
  23. ###########################################################################################
  24.  
  25. ############################ OPTIONS ######################################################
  26. TITLE='Teamspeak 3 server'                          # Name for the server
  27. DAEMON='ts3server_linux_x86'                        # Server binary, here the 32bit
  28. TS3='/opt/soft/teamspeak3-server_linux-x86/'      # Path to ts3 binary
  29. USER='ts3user'                                            # User running server
  30. USERG='ts3user'                                          # Usergroup of user
  31. OPT='dbplugin=ts3db_mysql'   #inifile=ts3db_mysql.ini'                                      # inifile=server.inifile
  32. LOGFILE='/var/log/ts3.log'                              # Logfile location and file
  33. ############################ END OPTIONS ##################################################
  34.  
  35. ########################### NO NEED TO EDIT UNDER HERE ####################################
  36. precheck () {
  37. # Checking for sudo
  38. if [ ! -x /usr/bin/sudo ]; then
  39.   echo ""
  40.   echo "You do not have Sudo installed. Please install it and try again."
  41.   echo "$(date +"%b %a %d  %H:%M:%S"): You do not have Sudo installed." >> $LOGFILE
  42.   echo ""
  43.   exit 1
  44. fi
  45.  
  46. # Checking for awk
  47. if [ ! -x /usr/bin/awk ]; then
  48.   echo ""
  49.   echo "You do not have Awk installed. Please install it and try again."
  50.   echo "$(date +"%b %a %d  %H:%M:%S"): You do not have awk installed." >> $LOGFILE
  51.   echo ""
  52.   exit 1
  53. fi
  54.  
  55. # Checking for ts3 binary
  56. if [ ! -x $TS3/$DAEMON ]; then
  57.   echo "Can't find /"
  58.   echo "Is the configs done right? Is  installed?"
  59.   echo "$(date +"%b %a %d  %H:%M:%S"): Can't find /" >> $LOGFILE
  60.   exit 1
  61. fi
  62. echo
  63. echo "All seems fine, try /etc/init.d/ts3 start"
  64. echo
  65. }
  66.  
  67. service_start() {
  68. TEST=$(ps ax | grep $DAEMON | grep -v export | grep -v grep | wc -l)
  69.  
  70. # Server not running and no pid-file found
  71. if [ "$TEST" = "0" ] && [ ! -f $TS3/ts3.pid ]; then
  72.   echo
  73.   echo "Starting $TITLE"
  74.   echo "$(date +"%b %a %d  %H:%M:%S"): Starting $TITLE" >> $LOGFILE
  75.   cd $TS3
  76.   LIBPATH=$(pwd)
  77.   su $USER -c "export LD_LIBRARY_PATH=$LIBPATH:$LD_LIBRARY_PATH ; $TS3/$DAEMON $OPT" >> $LOGFILE 2>&1 &
  78.   sleep 1
  79.   sudo -u $USER ps ax | grep -v grep | grep $DAEMON | grep -v export | awk '{print $1}' > $TS3/ts3.pid
  80.   chown $USER:$USERG $TS3/ts3.pid
  81.   echo "$TITLE screen process ID written to $TS3/ts3.pid"
  82.   echo "$TITLE started."
  83.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE started" >> $LOGFILE
  84.   echo
  85.   exit 1
  86. fi
  87.  
  88. # Server not running and a pid-file is found
  89. if [ "$TEST" = "0" ] && [ -f $TS3/ts3.pid ]; then
  90.   echo
  91.   echo "Server not running but pid-file present"
  92.   echo "Removing pid-file"
  93.   echo "$(date +"%b %a %d  %H:%M:%S"): Server not running but pid-file present" >> $LOGFILE
  94.   echo "$(date +"%b %a %d  %H:%M:%S"): Removing pid-file" >> $LOGFILE
  95.   rm $TS3/ts3.pid
  96.   echo "Old pid file removed"
  97.   echo "$(date +"%b %a %d  %H:%M:%S"): Old pid file removed" >> $LOGFILE
  98.   echo
  99.   echo "Starting $TITLE"
  100.   echo "$(date +"%b %a %d  %H:%M:%S"): Starting $TITLE" >> $LOGFILE
  101.   cd $TS3
  102.   LIBPATH=$(pwd)
  103.   su $USER -c "export LD_LIBRARY_PATH=$LIBPATH:$LD_LIBRARY_PATH ; $TS3/$DAEMON $OPT" >> $LOGFILE 2>&1 &
  104.   sleep 1
  105.   sudo -u $USER ps ax | grep -v grep | grep $DAEMON | grep -v export | grep -v export | awk '{print $1}' > $TS3/ts3.pid
  106.   chown $USER:$USERG $TS3/ts3.pid
  107.   echo "$TITLE screen process ID written to $TS3/ts3.pid"
  108.   echo "$TITLE started."
  109.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE started" >> $LOGFILE
  110.   echo
  111. fi
  112.  
  113. # Server running and no pid file-found, creates a new one!
  114. if [ "$TEST" = "1" ] && [ ! -f $TS3/ts3.pid ]; then
  115.   echo
  116.   echo "Server is running but no pid file. Creating a new pid file!!"
  117.   echo "$(date +"%b %a %d  %H:%M:%S"): Server is running but no pid file. Creating a new pid file!!" >> $LOGFILE
  118.   sudo -u $USER ps ax | grep -v grep | grep $DAEMON | grep -v export | awk '{print $1'} > $TS3/ts3.pid
  119.   chown $USER:$USERG $TS3/ts3.pid
  120.   echo
  121.   echo "$TITLE is running and new pid-file created"
  122.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE is running and new pid-file created" >> $LOGFILE
  123.   echo
  124. fi
  125.  
  126. # Server running and pid-file found
  127. if [ "$TEST" = "1" ] && [ -f $TS3/ts3.pid ]; then
  128.   echo
  129.   echo "$TITLE is running!!"
  130.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE is running!!" >> $LOGFILE
  131.   echo
  132. fi
  133. }
  134.  
  135. service_stop() {
  136. TEST1=$(ps ax | grep -v grep | grep $DAEMON | grep -v export | wc -l)
  137.  
  138. # Server is not running and no pid-file found
  139. if [ "$TEST1" = "0" ] && [ ! -f $TS3/ts3.pid ]; then
  140.   echo
  141.   echo "$TITLE is not running!!"
  142.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE is not running!!" >> $LOGFILE
  143.   echo
  144. fi
  145.  
  146. # Server is not running and pid-file found
  147. if [ "$TEST1" = "0" ] && [ -f $TS3/ts3.pid ]; then
  148.   echo
  149.   echo "Server is not running but pid-file is present"
  150.   echo "Removing pid-file"
  151.   echo "$(date +"%b %a %d  %H:%M:%S"): Server is not running but pid-file is present" >> $LOGFILE
  152.   echo "$(date +"%b %a %d  %H:%M:%S"): Removing pid-file" >> $LOGFILE
  153.   rm $TS3/ts3.pid
  154.   echo
  155.   echo "Pid file removed"
  156.   echo "$(date +"%b %a %d  %H:%M:%S"): Pid file removed" >> $LOGFILE
  157.   echo
  158. fi
  159.  
  160. # Server is running but no pid-file found
  161. if [ "$TEST1" = "1" ] && [ ! -f $TS3/ts3.pid ]; then
  162.   echo
  163.   echo "$TITLE is running but no pid file found."
  164.   echo "Stopping $TITLE"
  165.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE is running but no pid file found." >> $LOGFILE
  166.   echo "$(date +"%b %a %d  %H:%M:%S"): Stopping $TITLE" >> $LOGFILE
  167.   sudo -u $USER ps ax | grep -v grep | grep $DAEMON | grep -v export | awk '{print $1'} > $TS3/$DAEMON.pid
  168.   chown $USER:$USERG $TS3/ts3.pid
  169.   for id in $(cat $TS3/ts3.pid)
  170.   do kill -TERM $id
  171.   echo "Killing process ID $id"
  172.   echo "Removing $TITLE pid file"
  173.   rm -rf $TS3/ts3.pid
  174.   break
  175.   done
  176.   echo "$TITLE stopped"
  177.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE stopped" >> $LOGFILE
  178.   echo
  179. fi
  180.  
  181. # Server running and pid-file found
  182. if [ "$TEST1" = "1" ] && [ -f $TS3/ts3.pid ]; then
  183.   echo
  184.   echo "Stopping $TITLE"
  185.   echo "$(date +"%b %a %d  %H:%M:%S"): Stopping $TITLE" >> $LOGFILE
  186.   for id in $(cat $TS3/ts3.pid)
  187.   do kill -TERM $id
  188.   echo "Killing process ID $id"
  189.   echo "Removing $TITLE pid file"
  190.   rm -rf $TS3/ts3.pid
  191.   break
  192.   done
  193.   echo "$TITLE stopped"
  194.   echo "$(date +"%b %a %d  %H:%M:%S"): $TITLE stopped" >> $LOGFILE
  195.   echo
  196. fi
  197. }
  198. case "$1" in
  199.         'start')
  200.         service_start
  201.         ;;
  202.         'stop')
  203.         service_stop
  204.         ;;
  205.         'restart')
  206.         service_stop
  207.         sleep 1
  208.         service_start
  209.         ;;
  210.         'test')
  211.         precheck
  212.         ;;
  213.         *)
  214.         echo "Usage ./ts3conf start|stop|restart|precheck"
  215. esac
Advertisement
Add Comment
Please, Sign In to add comment