Starting zero alpha and ratel in a single command e.g. in MacOSX and other environments - dgraph

https://dgraph.io/tour/intro/2/
asks for three docker commands being run in different terminals:
zero
alpha
ratel
I'd rather start things from a single script dgraph within Mac OSX 10.3.6 and other environments and use e.g. the terminal app to call the different scripts along the lines of Running a command in a new Mac OS X Terminal window
How can the script below adapted for other environments like Unix and Windows?
dgraph
#!/bin/bash
# WF 2020-08-05
# see https://dgraph.io/tour/intro/2/
version=v20.03.0
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show the given error message on stderr and exit
#
# params:
# 1: l_msg - the error message to display
#
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error:" 1>&2
color_msg $red "\t$l_msg" 1>&2
exit 1
}
# show usage
#
usage() {
echo "$0 [-h|--help|-k|--kill"
echo ""
echo "-b | --bash: start a bash terminal shell within the currently running container"
echo "-h | --help: show this usage"
echo "-k | --kill: stop the docker image"
exit 1
}
#
# stop the docker image
#
stopImage() {
color_msg $blue "stopping and removing dgraph image ..."
docker stop dgraph
docker rm dgraph
color_msg $green "...done"
}
#
# start a bash shell within the currently running container
#
bashInto() {
sudo docker exec -it dgraph bash
}
#
# dgraph zero
#
zero() {
docker run -it -p 5080:5080 -p 6080:6080 -p 8080:8080 \
-p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph \
dgraph/dgraph:$version dgraph zero
}
#
# dgraph alpha
#
alpha() {
docker exec -it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0
}
#
# dgraph ratel
#
ratel() {
docker exec -it dgraph dgraph-ratel
}
me=$0
dir=$(dirname $0)
base=$(basename $0)
if [ $# -lt 1 ]
then
case $base in
dgraph)
# Run Dgraph zero
# And in another, run ratel (Dgraph UI)
# In another terminal, now run Dgraph alpha
for option in zero ratel alpha
do
#echo $dir $option
open -a terminal.app $dir/$option
# wait a bit
sleep 2
done
;;
alpha) alpha;;
ratel) ratel;;
zero) zero;;
esac
fi
# commandline option
while [ "$1" != "" ]
do
option="$1"
case $option in
alpha) alpha;;
ratel) ratel;;
zero) zero;;
-b|--bash) bashInto;;
-k|--kill) stopImage;;
-h|--help) usage;;
esac
shift
done

https://github.com/WolfgangFahl/DgraphAndWeaviateTest/blob/master/scripts/dgraph now has a script working on Linux (tested with travis/Ubuntu 18.04 LTS - bionic).
See also https://travis-ci.org/github/WolfgangFahl/DgraphAndWeaviateTest/jobs/715131236
usage
scripts/dgraph -h
scripts/dgraph [-b|--bash|-h|--help|-k|--kill|-p|--pull]
-b | --bash: start a bash terminal shell within the currently running container
-h | --help: show this usage
-k | --kill: stop the docker image
-p | --pull: pull the docker image
dgraph
#!/bin/bash
# WF 2020-08-05
#
# Starts zero alpha and ratel docker based with one script
#
# see https://dgraph.io/tour/intro/2/
# see https://stackoverflow.com/questions/63260073/starting-zero-alpha-and-ratel-in-a-single-command-e-g-in-macosx-and-other-envir
# see https://discuss.dgraph.io/t/dgraph-start-script/9231
version=v20.03.0
# interative tty option for docker
it="-it"
os=$(uname)
case $os in
Linux)
docker="docker"
it="";;
#docker="sudo docker";;
Darwin)
docker="docker";;
*)
docker="docker";;
esac
#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'
#
# a colored message
# params:
# 1: l_color - the color of the message
# 2: l_msg - the message to display
#
color_msg() {
local l_color="$1"
local l_msg="$2"
echo -e "${l_color}$l_msg${endColor}"
}
#
# error
#
# show the given error message on stderr and exit
#
# params:
# 1: l_msg - the error message to display
#
error() {
local l_msg="$1"
# use ansi red for error
color_msg $red "Error:" 1>&2
color_msg $red "\t$l_msg" 1>&2
exit 1
}
# show usage
#
usage() {
echo "$0 [-h|--help|-k|--kill"
echo ""
echo "-b | --bash: start a bash terminal shell within the currently running container"
echo "-h | --help: show this usage"
echo "-k | --kill: stop the docker image"
echo "-p | --pull: pull the docker image"
exit 1
}
#
# stop the docker image
#
stopImage() {
color_msg $blue "stopping and removing dgraph image ..."
$docker stop dgraph
$docker rm dgraph
color_msg $green "...done"
}
#
# pull the docker image
#
pullImage() {
color_msg $blue "pulling dgraph image $version ..."
$docker pull dgraph/dgraph:$version
color_msg $green "...done"
}
#
# start a bash shell within the currently running container
#
bashInto() {
$docker exec -it dgraph bash
}
#
# dgraph zero
#
zero() {
# Let’s create a folder for storing Dgraph data outside of the container:
mkdir -p ~/dgraph
$docker run $it -p 5080:5080 -p 6080:6080 -p 8080:8080 \
-p 9080:9080 -p 8000:8000 -v ~/dgraph:/dgraph --name dgraph \
dgraph/dgraph:$version dgraph zero
}
#
# dgraph alpha
#
alpha() {
$docker exec $it dgraph dgraph alpha --lru_mb 2048 --zero localhost:5080 --whitelist 0.0.0.0/0
}
#
# dgraph ratel
#
ratel() {
$docker exec $it dgraph dgraph-ratel
}
me=$0
dir=$(dirname $0)
base=$(basename $0)
if [ $# -lt 1 ]
then
case $base in
dgraph)
# Run Dgraph zero
# And in another, run ratel (Dgraph UI)
# In another terminal, now run Dgraph alpha
for option in zero ratel alpha
do
# make sure linked versions of command are available
if [ ! -f $dir/$option ]
then
color_msg $blue "creating link $dir/$option"
ln $dir/dgraph $dir/$option
fi
#echo $dir $option
color_msg $blue "starting dgraph $option ..."
case $os in
Darwin)
open -a terminal.app $dir/$option
;;
# https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the
Linux)
#for terminal in gnome-terminal xterm konsole
#do
# which $terminal > /dev/null
# if [ $? -eq 0 ]
# then
# $terminal -e $dir/$option
# break
# fi
#done
nohup $dir/$option > /tmp/$option.log 2>&1 &
sleep 2
tail /tmp/$option.log
;;
*)
error "unsupported operating system $os"
esac
# wait a bit
sleep 2
done
;;
alpha) alpha;;
ratel) ratel;;
zero) zero;;
esac
fi
# commandline option
while [ "$1" != "" ]
do
option="$1"
case $option in
alpha) alpha;;
ratel) ratel;;
zero) zero;;
-p|--pull) pullImage;;
-b|--bash) bashInto;;
-k|--kill) stopImage;;
-h|--help) usage;;
esac
shift
done

Related

Capturing multiple line output into a Bash variable with busybox sh

I'm trying to convert a Debian Bash script into a linux Busybox sh script. I'm stuck trying to convert the following command:
read -r -d '' MESSAGE << EOM
Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
EOM
The problem is with the -d option of read that is not available with Busybox. How can I set a variable ($MESSAGE in this case) to a string with multiple lines that includes values from other variables?
The output MESSAGE is going in a log file and in a message sent by sendmail:
echo "RESULTS: $MESSAGE" >> $LOGFILE
sendmail -S smtp.server.com -f "$FROM" "$RECIPIENTS" <<EOF
subject:$SUBJECT
from:$FROM
$MESSAGE
EOF
Simplest answer is not to use read.
MESSAGE=$(cat <<EOM
Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
EOM
)
MESSAGE=$( printf "%s\n%s\n%s\n%s\n" \
"Return code: $retn_code" \
"Start of backup: $DATESTART" \
"End of backup: $DATEEND" \
"$(df -h | grep '/share/USB')" \
)
You don't need a special command in any shell; just a regular assignment.
message="Return code: $retn_code
Start of backup: $DATESTART
End of backup: $DATEEND
$(df -h | grep '/share/USB')
"

Starting multiple tomcat instances in one server with init.d script

I'm trying to configure tomcat init.d start script to work with multiple instances (at this time 2 instances)
I'm following below sample script to create init.d script
#!/bin/bash
#
# tomcat This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Short-Description: start and stop tomcat
### END INIT INFO
TOMCAT_USER=root
TOMCAT_HOME="/opt/tomcat7/node1"
SHUTDOWN_WAIT=45
tomcat_pid() {
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}
start() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is already running (pid: $pid)"
else
# Start tomcat
echo "Starting tomcat service"
/bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/startup.sh" $TOMCAT_USER
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stoping Tomcat"
/bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/shutdown.sh" $TOMCAT_USER
let kwait=$SHUTDOWN_WAIT
count=0
count_by=5
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
sleep $count_by
let count=$count+$count_by;
done
if [ $count -gt $kwait ]; then
echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo "Tomcat is not running"
fi
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
else
echo "Tomcat is not running"
fi
;;
esac
exit 0
problem is tomcat_pid() method returns process ids of all tomcat instances, because of that, the second instance cannot be started. Is there a better method to handle this?
found a workaround, but expecting better solution
using netstat we can find process id via running port number
echo `netstat -tlnp | awk '/:80 */ {split($NF,a,"/"); print a[1]}'`
So i modified the function tomcat_pid() as below
tomcat_pid() {
echo `netstat -tlnp | awk '/:<port> */ {split($NF,a,"/"); print a[1]}'`
}

socat blocking on stdin

I have a bash script as follow on an AIX host, myscript.sh:
MODE="$1"
if [ "$MODE" == "start" ]; then
socat -T100 -lf $LOGF -d -d -d -x TCP4-LISTEN:$LISTENINGPORT,bind=$LISTENINGADDR,reuseaddr,fork EXEC:"$0 proxy" &
PID=$!
echo $PID > $PIDFILE
echo "$0 $MODE started (pid=$PID)"
elif [ "$MODE" == "proxy" ]; then
cat - > $TMPFILE
# process $TMPFILE before the SSL connection.
cat $TMPFILE | socat -T 100 -lf $LOGF -d - OPENSSL:$HOST
rm -f $TMPFILE
Everything is fine when I run:
$ cat somefile | myscript.sh proxy | xxd
The problem raise when I connect to the socat listener with a test script:
my $file = $ARGV[0];
my $fsize = -s $file;
my $socket = IO::Socket::INET->new("127.0.0.1:$port")
or die "Couldn't connect to remote host: $!";
$socket->autoflush(1);
binmode($socket);
open (FILE,$file);
binmode(FILE);
my $buffer ;
while(sysread(FILE, $buffer, $blocksize)) {
print $socket $buffer ;
}
print "sent\n" ;
close (FILE) ;
my $answer = <$socket>;
if (defined($answer)) {
print $answer; # never reached
print "...\n" ;
} else {
die "connection reset by peer\n";
}
In myscript.sh, it blocks on the line:
cat - > $TMPFILE
In the test script, it blocks on the line:
my $answer = <$socket>;
At this point, the data has been received by the socat listener (checked with tcpdump).
However, when I Ctrl+c the test script before the socat timeout, the data goes through the pipe (i.e., the SSL server is eventually contacted).
What am I doing wrong?
Update:
Thanks for the tips about cat and EOF. For the time being, I have worked around the problem like so:
timeout 0.2 cat -u - > $TMPFILE 2>>/dev/null
# process $TMPFILE before the SSL connection.
cat $TMPFILE | socat -T 100 -lf $LOGF -d - OPENSSL:$HOST
It's ugly, and a waste 0.2 seconds, I hope to find a better solution.
But it does the job for now. The 2>>/dev/null part is because AIX complains about an invalid counter (related to the timeout command).
My first thought is that there is no linefeed in the data you're trying to receive with cat - or <STDIN> . Both commands in their default behavior will return data once they have a linefeed or their buffers of the file-descriptor is full (4KB by default in Linux).

Executing Command on child shell

Here is the updated question
Script:
open (my $pipe, "| ptsetenv.sh $ProductType $Release");
print $pipe "genidasack.py -v --alignment=mips64 -a -s $WORKSPACE/dbgen/ose_signals_ADA.sdt -o $WORKSPACE/$Product/\n";
close ($pipe);
Command 2:
ptsetenv.sh $ProductType $Release # Sets Environment Variables and creates a Child Shell
Command 3:
genidasack.py -v --alignment=mips64 -a -s $WORKSPACE/dbgen/ose_signals_ADA.sdt -o $WORKSPACE/$Product/\n #this has to be executed on child Shell created by Command 2
Currently when i run the script within a perl script after first line child shell is invoked and script remains at child shell when type exit from child shell subsequent script lines are executed which is not what is needed!
HERE ARE CONTENTS OF ptsetenv.sh
envsetup.py $*
./export_env.sh
export_env.sh is the script which basically creates the child shell
Let me know if you need contects of export_env.sh
HERE ARE CONTENTS OF export_env.sh
has_dir()
{
if [ -d $1 ]; then
return 0
else
return 1
fi
}
has_dir $DXENVROOT
if [ "$?" != "0" ]; then
echo "Directory $DXENVROOT does not exist. Exiting the script."
exit -1
fi
echo "Environment set to ${DXENVNAME} ${DXENVVERSTR}"
echo $SHELL
$SHELL
echo "Exiting ${DXENVNAME} ${DXENVVERSTR} shell"
This will run a sequence of commands in the child shell:
system("Command 1; python script; Command 2");
You can also do:
system("Command 1");
system("python script");
system("Command 2");
This avoids creating a shell process if there are no special shell characters in the commands.
UPDATE:
Now that you've clarified that the python script starts a subshell, here's how to send a command to it:
open (my $pipe, "| Command 1; python script");
print $pipe "Command 2\n";
close ($pipe);

NetworkManager dispatcher script

scripts in /etc/NetworkManager/dispatcher.d will got exec and parameters will be passed to the scripts by NetworkManager.
One of my laptop BIOS is malfunctioning, I have to manually sync the time, and do system upgrade BTW. I am working with a script to automate this task.
Here's the script:
#!/bin/sh
IF=$1
STATUS=$2
if [ "$STATUS"x != 'up'x -o "$(date +%Y)" -gt "2012" ] ;then
exit
fi
logger "==$0=="
wait_for_process(){
PNAME=$1
PID=`pgrep $PNAME`
while [ -z "$PID" ];do
logger "waiting $1 running for another 3 sec.."
sleep 3;
PID=`pgrep $PNAME`
done
logger "$1 is running!"
}
wait_for_process nm-applet
wait_for_process lxpanel
export DISPLAY=$(echo $DISPLAY | cut -c -2)
if [ -z $DISPLAY ];then
export DISPLAY=:0
fi
#below cmd will yield null string for $user
user=$(who | grep "$DISPLAY" | awk '{print $1}' | tail -n1)
#so I have to hardcode the user name:(
user=xxx
export XAUTHORITY="/home/$user/.Xauthority"
logger "Display $DISPLAY user $user"
su $user -c "xterm -e 'sudo /usr/bin/ntpd -qdg && sudo yaourt -Syua' &" || logger "cannot run xterm"
(the script is invoked before x window, run as root)
user=$(who | grep "$DISPLAY" | awk '{print $1}' | tail -n1) cannot find the login user name. But it works in xterm.
Can someone help?
I am using archlinux i686 + openbox + lxpanel
edit:
I want to find the real login user name, while the script is run by root.
Are you looking for the name of the user running the script? How about:
user=$( id -un )