Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Simple bukkit startup script by Piffey
- # This is by no means a clean script, but it will get the job done.
- # Suggestions? Improvements? Let me know at Piffey.com
- # All of the screen stuff was adapted from ragon's <frederik.engels24 AT gmail DOT com> script for his Arch Linux package.
- # Reason: I don't use screen, but it's likely the best option for anyone looking for a simple script they can run and ignore.
- # His script worked perfect for my first server too. Thanks ragon.
- ## 03/27/11 Updates (ryunp)
- # Modified for tmux
- # Altered $dateformat into `_yymmdd-hhmmss` so you get: <world>_yymmdd-hhmmss
- # This allows unique inter-minute backups
- # Invocation variable ($bukkitinvoaction) instead of just RAM allocation
- # Added bukkitstatus() to check offline/online status
- # If online shows process activity/information
- # Updated startbukkit() & stopbukkit() to check java process status after invoked/stopped
- # Added friendlystop timing sequence to the stopbukkit() itself; checks for arg given in case statement
- # updatebukkit() will save current JAR as <JAR>.old and rename newly downloaded file
- # bukkitbackup() added zip + targz support by choosing, added customizable messaged for before/after
- # Added a second backupmsg and completion var, tar & zip integration (if zip is installed; checks before attempting)
- # Added status to argument case statement and catchall display, updated friendlystop
- ##
- # Set these values to match your server's settings.
- bukkitdir="~/bukkit/"
- backupdir="~/bukkit/backups/"
- bukkitfilename="craftbukkit.jar"
- bukkitupdate="craftbukkit-updater.jar"
- # Will omit backup messages if quotes left empty
- backupmsg="Backup in progress. Saving disabled for a moment."
- backupmsgdone="Level data successfully backed up!"
- dateformat="_$(date '+%y%m%d-%H%M%S')"
- # Determine additional archived level backups
- # Note: `zip` format is not native to most (all?) linux distro's, must install to work.
- backuplevelzip="true"
- backuplevelgz="false"
- # Custom java binary location. Do not change unless custom installation was done, ex: `/opt/java/bin/java`.
- # This assumes java is in the default location: /usr/bin/java.
- javaloc="java"
- # Invocation parameters. Set the amount of RAM you want to allocate, Java heap max and initial size.
- # Good practice is to have the numbers match.
- bukkitinvocation="$javaloc -Xmx2048M -Xms2048M -jar '$bukkitdir$bukkitfilename' nogui"
- # Make sure you change this to the name of your world folder!
- # Add additional worlds by separating them with a white space, ex: =(world nether pvp chaos).
- declare -a worlds=(world)
- numworlds=${#worlds[@]}
- # This currently points to the "preferred" release for bukkit which is kind of like a stable version.
- # Change this value if you want to use the snapshot release.
- bukkiturl=http://ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar
- bukkitstatus()
- {
- if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- echo "Bukkit server currently OFFLINE"
- exit 0
- else
- echo "Bukkit server currently ONLINE:"
- # Get the Process ID of running JAR file
- bukkitPID=`ps ax | grep -v grep | grep -v -i tmux | grep -v sh | grep "$bukkitfilename"`
- # Display process activity information of bukkit (5 character length pID)
- top -n 1 -p ${bukkitPID:0:5} | grep ${bukkitPID:0:5}
- # Display prcoess infomation of bukkit
- echo "$bukkitPID"
- fi
- }
- startbukkit()
- {
- if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- echo "Starting Bukkit server..."
- cd $bukkitdir
- # Check for a tmux minecraft session already active; if so create a new window within, otherwise new session
- # (Creating a new window in a minecraft tmux session will cause the session to stay active even after this scripts stopbukkit function is called)
- if ! tmux ls | grep minecraft > /dev/null; then
- tmux new -d -n bukkit -s minecraft "$bukkitinvocation"
- else
- echo "tmux minecraft sessions detected: creating window within!"
- tmux neww -d -n bukkit -t minecraft "$bukkitinvocation"
- fi
- # Check for process status, wait 7 seconds for plugin initialization
- sleep 7
- if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- echo "Bukkit server failed to start!"
- else
- echo "Bukkit server successfully started!"
- fi
- else
- echo "Bukkit server is already running."
- exit 0
- fi
- }
- stopbukkit()
- {
- if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- echo "Bukkit server is not running."
- exit 0
- else
- # Check for friendlystop trigger
- if [ "$1" == "friendly" ]; then
- echo "Sending 5 minute shutown warning"
- tmux send -t minecraft:bukkit "say Going down in 5 minutes." C-m
- sleep 240
- echo "Sending 1 minute shutdown warning"
- tmux send -t minecraft:bukkit "say Going down in 60 seconds." C-m
- sleep 30
- echo "Sending 30 second shutdown warning"
- tmux send -t minecraft:bukkit "say Going down in 30 seconds." C-m
- sleep 25
- echo "Sending 5 second shutdown warning"
- tmux send -t minecraft:bukkit "say Going down in 5 seconds." C-m
- sleep 5
- fi
- tmux send -t minecraft:bukkit 'stop' C-m
- # Check for process status, waiting 5 seconds for shutdown procedure to finish
- sleep 5
- if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- echo "Bukkit server successfully stopped!"
- else
- echo "Process failed to stop!"
- # Take more aggresive action against pID
- echo "Attempting to kill rogue $bukkitfilename process..."
- # Get the Process ID of running JAR file
- bukkitPID=`ps ax | grep -v grep | grep -v -i tmux | grep -v sh | grep "$bukkitfilename"`
- kill ${bukkitPID:0:5}
- sleep 1
- # Check for process status after pkill attempt
- if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- echo "$bukkitfilename process terminated!"
- else
- echo "$bukkitfilename could not be killed!"
- exit 1
- fi
- fi
- fi
- }
- updatebukkit()
- {
- # Alert players Bukkit server going down for repairs
- if ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- tmux send -t minecraft:bukkit "say Going down for an update in 60 seconds." C-m
- sleep 5
- tmux send -t minecraft:bukkit "say Going down for an update in 5 seconds." C-m
- sleep 5
- stopbukkit
- fi
- # Download new JAR
- wget -O "$bukkitdir$bukkitupdate" "$bukkiturl"
- sleep 1
- # Backup old JAR file and rename newly downloaded
- mv -v "$bukkitdir$bukkitfilename" "$bukkitdir$bukkitfilename.old"
- mv -v "$bukkitdir$bukkitupdate" "$bukkitdir$bukkitfilename"
- echo "You must now manually start the Bukkit server."
- }
- backupbukkit()
- {
- echo "Starting multiworld backup..."
- # Alert players backup is in progress
- if ! ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- echo "Bukkit server not running!"
- else
- if [ "$backupmsg" != "" ]; then tmux send -t minecraft:bukkit "say $backupmsg" C-m; fi
- if [ "$backupmsg2" != "" ]; then tmux send -t minecraft:bukkit "say $backupmsg2" C-m; fi
- tmux send -t minecraft:bukkit "save-all" C-m
- sleep 5
- tmux send -t minecraft:bukkit "save-off" C-m
- fi
- # Check for backup directory
- if ! [ -d $backupdir ];then mkdir -p $backupdir; fi
- # Determine if zip package is installed on host
- if ! dpkg-query -W 'zip' > /dev/null; then backuplevelzip="false"; fi
- # Iterate through level backups
- cd $backupdir
- for ((i=0;i<$numworlds;i++)); do
- echo "Copying '${worlds[$i]}' + '$bukkitfilename' to '$backupdir${worlds[$i]}$dateformat'..."
- cp -R $bukkitdir${worlds[$i]} $backupdir${worlds[$i]}$dateformat
- cp $bukkitdir$bukkitfilename $backupdir${worlds[$i]}$dateformat
- # TARgz backup sequence
- if [ "$backuplevelgz" == "true" ]; then
- echo "Creating tar archive file of '${worlds[$i]}$dateformat'..."
- tar zcf ${worlds[$i]}$dateformat.tar.gz ${worlds[$i]}$dateformat/
- fi
- # ZIP backup sequence
- if [ "$backuplevelzip" == "true" ]; then
- echo "Creating zip archive file of '${worlds[$i]}$dateformat'..."
- zip -rq ${worlds[$i]}$dateformat ${worlds[$i]}$dateformat
- fi
- done
- sleep 1
- # Alert players backup is finished
- if ps ax | grep -v grep | grep -v -i tmux | grep "$bukkitfilename" > /dev/null; then
- tmux send -t minecraft:bukkit "save-on" C-m
- if [ "$backupmsgdone" != "" ]; then tmux send -t minecraft:bukkit "say $backupmsgdone" C-m; fi
- fi
- echo "Backup complete."
- }
- case $1 in
- status)
- bukkitstatus
- ;;
- start)
- startbukkit
- ;;
- stop)
- stopbukkit
- ;;
- restart)
- stopbukkit
- sleep 5
- startbukkit
- ;;
- update)
- updatebukkit
- ;;
- backup)
- backupbukkit
- ;;
- friendlystop)
- stopbukkit friendly
- ;;
- *)
- echo "Usage: $0 {status|start|stop|restart|update|backup|friendlystop}"
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement