Ahoy friends.
Currently i got a bash script running to manage my minecraft servers.
All of them are stored in /home/minecraft_servers directory.
Using my script im able to start a server (e.g. ./minecraft start ftb_continuum) because server name and server name are the same.(e.g. /home/minecraft_servers/ftb_continuum)
Because of security and efficiency reasons i want to get this script working in C but unfortunately i dont know how to begin, even using my rusty knowledge of my study.
Does someone have any advice how to begin?
Thats my bash script:
Code:
#!/bin/bash
# /etc/init.d/minecraft
# version 0.4.2 2016-02-09 (YYYY-MM-DD)
#
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs screen-cleanup
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft server
# Description: Starts the minecraft server
### END INIT INFO
#Settings
SERVICE="${2}.jar"
SCREENNAME=$2
OPTIONS='nogui'
USERNAME='root'
WORLD='world'
MCPATH="/home/minecraft_servers/${2}/"
MINHEAP=4096
HISTORY=4096
CPU_COUNT=16
Xms=1G
JAVACMD="java"
MAX_RAM="8192M" # -Xmx
JAVA_PARAMETERS="-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSIncrementalPacing -XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=${CPU_COUNT} -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:+AggressiveOpts -Xms${Xms}"
INVOCATION="${JAVACMD} -server -Xmx${MAX_RAM} ${JAVA_PARAMETERS} -jar ${SERVICE} nogui"
Backuppath="/var/www/backup_folder/Minecraft/modded/"
Zielordner="${Backuppath}Minecraft_Modded_Backup_${SERVICE}_$(date +%Y-%m-%d_%H-%M).7z"
Password="FASkog4AmtO1XIvGbkh8sqppYs51Eo7E7drbVpCSXOIsX4yyaUsIXvVEnLGQELMh3n1eqI23QiOGVhnsnlCX7GGMQKVapKCfoqnei2u8Ssb7ZDgbbbfYgpoo0DT"
ME=`whoami`
as_user() {
if [ "$ME" = "$USERNAME" ] ; then
bash -c "$1"
else
su - "$USERNAME" -c "$1"
fi
}
mc_start() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is already running!"
else
echo "Starting $SERVICE..."
cd $MCPATH
as_user "cd $MCPATH && screen -h $HISTORY -dmS ${SCREENNAME} $INVOCATION"
sleep 7
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is now running."
else
echo "Error! Could not start $SERVICE!"
fi
fi
}
mc_saveoff() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running... suspending saves"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly...\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-off\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-all\"\015'"
sync
sleep 10
else
echo "$SERVICE is not running. Not suspending saves."
fi
}
mc_port(){
grep -n 'server-port=' "$MCPATH/server.properties"
}
mc_saveon() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running... re-enabling saves"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-on\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
else
echo "$SERVICE is not running. Not resuming saves."
fi
}
mc_stop() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "Stopping $SERVICE"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"save-all\"\015'"
sleep 10
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"stop\"\015'"
sleep 7
else
echo "$SERVICE was not running."
fi
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "Error! $SERVICE could not be stopped."
else
echo "$SERVICE is stopped."
fi
}
mc_update() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running! Will not start update."
else
as_user "cd $MCPATH && wget -q -O $MCPATH/versions --no-check-certificate https://launchermeta.mojang.com/mc/game/version_manifest.json"
if [ "$1" == "snapshot" ] ; then
JSONVERSION=`cd $MCPATH && cat versions | python -c "exec(\"import json,sys\nobj=json.load(sys.stdin)\nversion=obj['latest']['snapshot']\nfor v in obj['versions']:\n if v['id']==version:\n print(v['url'])\")"`
else
JSONVERSION=`cd $MCPATH && cat versions | python -c "exec(\"import json,sys\nobj=json.load(sys.stdin)\nversion=obj['latest']['release']\nfor v in obj['versions']:\n if v['id']==version:\n print(v['url'])\")"`
fi
as_user "cd $MCPATH && wget -q -O $MCPATH/versions --no-check-certificate $JSONVERSION"
MC_SERVER_URL=`cd $MCPATH && cat versions | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["downloads"]["server"]["url"])'`
as_user "rm $MCPATH/versions"
as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update --no-check-certificate $MC_SERVER_URL"
if [ -f $MCPATH/minecraft_server.jar.update ] ; then
if `diff $MCPATH/$SERVICE $MCPATH/minecraft_server.jar.update >/dev/null` ; then
echo "You are already running the latest version of $SERVICE."
else
as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/$SERVICE"
echo "Minecraft successfully updated."
fi
else
echo "Minecraft update could not be downloaded."
fi
fi
}
mc_backup() {
mc_saveoff
if [ ! -d "${MCPATH}" ]; then
echo "MCPATH existiert nicht!"
exit 1
fi
if [ -d "${Zielordner}" ]; then
echo "Zielordner bereits vorhanden!"
exit 1
fi
7z a -t7z -mx=0 -m0=LZMA2 -mmt=${CPU_COUNT} -p"${Password}" "${Zielordner}" "${MCPATH}" >> "${Backuppath}placeholder.txt"
7z h -scrcsha256 "${Zielordner}" >> "${Backuppath}placeholder.txt"
mc_saveon
}
mc_command() {
command="$1";
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
pre_log_len=`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`
echo "$SERVICE is running... executing command"
as_user "screen -p 0 -S ${SCREENNAME} -X eval 'stuff \"$command\"\015'"
sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
# print output
tail -n $[`wc -l "$MCPATH/logs/latest.log" | awk '{print $1}'`-$pre_log_len] "$MCPATH/logs/latest.log"
fi
}
#Start-Stop here
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
mc_stop
mc_start
;;
update)
mc_stop
mc_backup
mc_update $2
mc_start
;;
backup)
mc_backup
;;
port)
mc_port
;;
status)
if pgrep -u $USERNAME -f $SERVICE > /dev/null ; then
echo "$SERVICE is running."
else
echo "$SERVICE is not running."
fi
;;
command)
if [ $# -gt 1 ] ; then
shift
mc_command "$*"
else
echo "Must specify server command (try 'help'?)"
fi
;;
*)
echo "Usage: $0 {start|stop|update|backup|status|restart|command|port \"server command\"}"
exit 1
;;
esac
exit 0
I started to create something this way, but now i dont know how to continue.
I have to use bash commands to start the servers, like java -jar (servername) so i have to use system() command.
But i also have to know if the service is already running.
So I need a script that does the following:
If a certain user is logged in
Run `command`
Else
Echo “incorrect user”
This is my first stab...which doesn't work:
#!/bin/bash
X="user=`ls -l /dev/console | cut -d " " -f 4`"
Y="foobar"
echo $X
echo $Y (4 Replies)
I'm trying to write a script that moves data that's older than 2 weeks to a different place.
It works well, EXCEPT, that when the script hits a file within a directory inside the working directory, it will move it to the root of the destination directory instead of putting it in the correct... (1 Reply)
Hello everyone,
sorry for the title, most of you must getting sick of reading something like
this, but I haven't found a solution, although I found many threads according
to it.
I'm working on a bash script that connects to a network printer with ftp
where I want to upload a pdf created... (3 Replies)
So i'm currently working on a project where I'm attempting to display information of users from the /etc/passwd file and also another information file holding addition information about users.
Problem is I've been trying to join the two files together and have all of the information about each... (2 Replies)
:wall::wall::wall:
Hi I have horrible script below, need help in renaming ls -l output into new filename format:
Desired output:
cp -pv original_path/.* newDirectory/owner_of_file.%dd%mm%y.file_extension.first_8_characters_of_original_filename
localuser@localuser:~ vi... (3 Replies)
Hi friends,
I have two files - input and commands
I want to read the input and replace a value in it with the contents in commands.
My script is like this.
Instead of printing the value in the commands file, it is simply printing $cmd in the output file.
Any pointers are highly... (1 Reply)
I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows:
#!/bin/bash
HOST=$1
/usr/bin/expect -c "
spawn ssh -i /root/.ssh/id_rsa root@$HOST
expect -exact "Enter... (3 Replies)
Hi All,
I have to pick particular segments from a file and I have prepared below shell script.But its not working and I am not able to find out whats the issue.could you guys pls help?
Sample file:
TS3*1451575*12*20151231*4*482.44
NM1*QC*1*CUTLER*BETTY
DTM*472*20150808... (4 Replies)
Hi,
maybe I'm asking a VERY dumb question, but would anybody out there tell me, why this f****** script won't work if executed as a cronjob, but works fine if executed from a shell prompt?
#! /bin/bash
set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
date >>... (3 Replies)
i have defined a function ln_s() for customizing the ln command in script1.sh.
more script1.sh
echo "Starting Execution"
./script2.sh
echo "End of Execution"
ln_s(){
] && return
ln -s "$1" "$2"
}
My script1.sh executes another script2.sh which has the following entry
more script2.sh... (12 Replies)