Best way to get a bash script working in C


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Programming Best way to get a bash script working in C
# 1  
Best way to get a bash script working in C

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.


Thats what i have done, but its not working yet.


Code:
#include <stdio.h>

#define BUFSIZE 128


const char $USERNAME[]="root";




const char *identifier[5]={"ftb_continuum","ftb_continuum_last","lost+souls","final+frontier","sky_adventure"};



char OPTIONS[]="nogui";
char JAVA_PARAM[]="-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSIncrementalPacing -XX:+CMSClassUnloadingEnabled -XX:ParallelGCThreads=${CPU_COUNT} -XX:MinHeapFreeRatio=5 -XX:MaxHeapFreeRatio=10 -XX:+AggressiveOpts -Xms${Xms}";
char INVOCATION[]="java -jar -server -Xmx4096 ${JAVA_PARAM} -jar ${SERVICE} nogui";
char Backuppath[]="/var/www/repo.openknogle.eu/Minecraft/modded/";
char Password[]="FASkog4AmtO1XIvGbkh8sqppYs51Eo7E7drbVpCSXOIsX4yyaUsIXvVEnLGQELMh3n1eqI23QiOGVhnsnlCX7GGMQKVapKCfoqnei2u8Ssb7ZDgbbbfYgpoo0DT";
int HISTORY = 4096;
const char as_user[]="su - $USERNAME -c argv[1] ";



int send_get_bash(const char *cmd);
int start(const char *server);


}
int main( int argc, char *argv[] )  {

    if( argc == 2 ) //Valid options entered
    {
        if(argv[1] == "start")
        {
            if(argv[2] == "all")
            {
                for(int t;t<sizeof(identifier))
                {
                    run_server(identifier[t]);
                }
            }
            else
            {
                run_server(argv[2]);
            }
        }
        if(argv[1] == "stop")
        {
            if(argv[2] == "all")
            {
                for(int t;t<sizeof(identifier))
                {
                    stop_server(identifier[t]);
                }
            }
            else
            {
                stop_server(argv[2]);
            }
        }
        if(argv[1] == "restart")
        {
            if(argv[2] == "all")
            {
                for(int t;t<sizeof(identifier))
                {
                    restart_server(identifier[t]);
                }
            }
            else
            {
                restart_server(argv[2]);
            }
        }
        if(argv[1] == "backup")
        {
            if(argv[2] == "all")
            {
                for(int t;t<sizeof(identifier))
                {
                    backup_server(identifier[t]);
                }
            }
            else
            {
                backup_server(argv[2]);
            }
        }
        
        
        
        
    }


int restart_server(char *server)
    {
        stop_server(server);
        run_server(server);
    }

    else if( argc > 2 ) {
        printf("Too many arguments supplied.\n");
    }
    else {
        printf("One argument expected.\n");
    }
}


int run_server(char *server)
{
    char buffer[128];
    snprintf(buffer, sizeof buffer, "./start.sh %s %s %s %i",server,JAVA_PARAM,$USERNAME,HISTORY,INVOCATION);
    send_get_bash(buffer);
    return 1;
}



int send_get_bash(const char *cmd) 
{
    

    char buf[BUFSIZE];
    FILE *fp;

    if ((fp = popen(cmd, "r")) == NULL) {
        printf("Error opening pipe!\n");
        return -1;
    }

    while (fgets(buf, BUFSIZE, fp) != NULL) {
    
        printf("%s", buf);
    }

    if(pclose(fp))  {
        printf("Command not found or exited with error status\n");
        return -1;
    }

    return 0;
}


mc_start(const char *server_start) 
{
    for(new t;t<sizeof(identifier))
    {
        if(strcmp(server,identifier[k]);
                {
                    
                    goto start;
                    break;
                }
                else if k=sizeof(identifier)-1;
                {
                    printf("Invalid Service!");
                }
            }
        }

start:
        
        run_server(server_start);
    }

    mc_stop(const char *server_stop) 
    {
        for(new t;t<sizeof(identifier))
        {
            if(strcmp(server,identifier[k]);
            {
                
                goto stop;
                break;
            }
            else if k=sizeof(identifier)-1;
            {
                printf("Invalid Service!");
            }
        }
    }

stop:
    
    stop_server(server_stop);
}

mc_restart(const char *server_restart) 
{
    for(new t;t<sizeof(identifier))
    {
        if(strcmp(server,identifier[k]);
        {
            
            goto restart;
            break;
        }
        else if k=sizeof(identifier)-1;
        {
            printf("Invalid Service!");
        }
    }
}

restart:

restart_server(server_restart);
}

# 2  
Welcome to the forums Smilie

I must be a party breaker here, without dwelling into code..
Why do you think for security and efficiency reasons would one exchange a shell script with a c program to start a java program ?

Seems like an insane overhead to me, even for someone who will write a good c program to do that action.

Would it help to rewrite shell code perhaps ?
Some features or something you would like to improve ?

Regards
Peasant.
# 3  
This is premature optimization. Do you know what system() invokes? The shell. Running twenty shells isn't going to be more secure than running one shell. C isn't inherently more secure, besides -- used blindly, it's a whole lot less.

Exactly what efficiency and security problems do you wish to solve? I can think of a few ways to improve what I see but none of them require C yet.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #602
Difficulty: Medium
MySQL 8.0 incorporates a transactional data dictionary that stores information about database objects.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Aliases NOT working inside bash shell script

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)
Discussion started by: mohtashims
12 Replies

2. Shell Programming and Scripting

Linux/bash Script only working if executed from shell prompt

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)
Discussion started by: beislhur
3 Replies

3. Shell Programming and Scripting

Bash shell script not working-picking segment patterns from a file

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)
Discussion started by: Venkata Prasad
4 Replies

4. Shell Programming and Scripting

Expect not working inside my Bash script

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)
Discussion started by: John Wilson
3 Replies

5. Shell Programming and Scripting

sed not working in a bash script

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)
Discussion started by: jacobs.smith
1 Replies

6. Shell Programming and Scripting

Been working since 25+ hrs: Bash Script to rename files supposedly direct but difficult to execute

: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)
Discussion started by: wolf@=NK
3 Replies

7. Shell Programming and Scripting

Bash join script not working

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)
Discussion started by: Nostyx
2 Replies

8. Shell Programming and Scripting

bash script for ftp-upload is not working

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)
Discussion started by: le_mae
3 Replies

9. Shell Programming and Scripting

Moving old files bash script - not working properly

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)
Discussion started by: ugolee
1 Replies

10. Shell Programming and Scripting

Simple BASH script not working?

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)
Discussion started by: doubleminus
4 Replies

Featured Tech Videos