introducing moxanix script for controling servers
This commit is contained in:
parent
d4452bd340
commit
418a8caadc
3 changed files with 59 additions and 4 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -4,3 +4,5 @@ moxerver
|
|||
# object files
|
||||
*.o
|
||||
|
||||
# logs directory during development
|
||||
logs
|
||||
|
|
55
moxanix.sh
Executable file
55
moxanix.sh
Executable file
|
@ -0,0 +1,55 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Main script for controlling servers.
|
||||
|
||||
|
||||
usage ()
|
||||
{
|
||||
echo "Usage: ./$(basename $0) COMMAND ID"
|
||||
echo " COMMAND: start, stop"
|
||||
echo " ID: 1-7"
|
||||
}
|
||||
|
||||
##########
|
||||
|
||||
# check parameter count
|
||||
if [ $# != 2 ]; then
|
||||
usage
|
||||
exit
|
||||
fi
|
||||
|
||||
# grab parameters
|
||||
COMMAND=$1
|
||||
ID=$2
|
||||
|
||||
# set variables
|
||||
TCP_PORT=$((4000 + $ID))
|
||||
TTY_PATH="/dev/ttyS$ID"
|
||||
|
||||
LOGDIR="./logs"
|
||||
LOGFILE="$LOGDIR/moxerver$ID.log"
|
||||
|
||||
# execute commands
|
||||
if [ "$COMMAND" = "start" ]; then
|
||||
# create log directory if it doesn't exist
|
||||
if [ ! -d $LOGDIR ]; then
|
||||
mkdir -p $LOGDIR
|
||||
fi
|
||||
# start moxerver, redirect stdout and stderr to logfile
|
||||
# nohup keeps it running when the script ends
|
||||
nohup ./moxerver -p $TCP_PORT -t $TTY_PATH > $LOGFILE 2>&1 &
|
||||
|
||||
elif [ "$COMMAND" = "stop" ]; then
|
||||
# ps axf -> list all processes with PID as first field
|
||||
# grep [m]oxerver... -> [] trick eliminates the actual grep process from results
|
||||
# awk '{print $1}' -> print the first field from the line, in this case PID
|
||||
pid=$(ps axf | grep "[m]oxerver -p $TCP_PORT" | awk '{print $1}')
|
||||
if [ $pid = "" ]; then
|
||||
echo "nothing to kill"
|
||||
fi
|
||||
kill -s SIGTERM $pid
|
||||
|
||||
else
|
||||
# unsupported command
|
||||
usage
|
||||
fi
|
|
@ -60,7 +60,7 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
pthread_t tty_thread;
|
||||
|
||||
/* catch and handle some quit signals, SIGKILL can't be caught */
|
||||
/* enable catching and handling some quit signals, SIGKILL can't be caught */
|
||||
signal(SIGTERM, quit_handler);
|
||||
signal(SIGQUIT, quit_handler);
|
||||
signal(SIGINT, quit_handler);
|
||||
|
@ -105,10 +105,8 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
}
|
||||
|
||||
/* introduction message */
|
||||
fprintf(stderr, "[%s] === MoxaNix ===\n", NAME);
|
||||
|
||||
/* initialize */
|
||||
fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_dev.path);
|
||||
if (server_setup(&server, tcp_port) < 0) return -1;
|
||||
client.socket = -1;
|
||||
tty_dev.fd = -1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue