run Terminal script that executes one command and while that command is running, opens a new tab and runs another command - macos

At the moment I am making a java application. To test it I have to run a server and then a client.
So I want to run this using a bash script:
#!/bin/bash
clear
gradle runServer
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
gradle runClient
Problem: The server when run does not end until you close the game, so the next two commands will not execute. How can I run them concurrently/simultaneously ?

Run the server in the background, then kill it when the script is done.
Here’s an example with a simple HTTP server and client.
#!/bin/bash
date > foo.txt
python -m SimpleHTTPServer 1234 &
SERVER_PID="${!}"
# Automatically terminate server on script exit
trap 'kill "${SERVER_PID}"' 0 1 2 3 15
# Wait for server to start
while ! netstat -an -f inet | grep -q '\.1234 '; do
sleep 0.05
done
# Run client
curl -s http://localhost:1234/foo.txt
Running in another tab gets a lot trickier; this interleaves the output from the client and the server.
$ ./doit.sh
127.0.0.1 - - [19/Sep/2014 23:00:32] "GET /foo.txt HTTP/1.1" 200 -
Fri 19 Sep 2014 23:00:32 MDT
Note the log output from the HTTP server, and the output from the HTTP client. The server is automatically killed afterwards.

Related

How to execute an application in bash that is not a child and keeps running?

I am writing a bash script and am checking whether the application is running. If it is not running it should be started in a separate process (not a child process). If it is running, the window should be maximized. I kind of made it but the new process terminates shortly after being started, probably because the script process ends.
#!/bin/bash
if (ps aux | grep App1 | grep -v grep > /dev/null)
then
echo App1 is running
wmctrl -x -r WMClassOfApp1 -b "add,maximized_vert,maximized_horz"
else
echo App1 is not running
sh -c /usr/bin/app1 & disown # This app should be started in a separate process and not terminate
fi
I probably have to add that I am calling this script from a udev rule. When I execute it in a terminal, it works fine. When I call it from the udev rule, the app1 terminates.
A bash script is not the right solution for this: best is to add this to crontab of your system.

How to run 2 commands on bash concurrently

I want to test my server program,(let's call it A) i just made. So when A get executed by this command
$VALGRIND ./test/server_tests 2 >>./test/test.log
,it is blocked to listen for connection.After that, i want to connect to the server in A using
nc 127.0.0.1 1234 < ./test/server_file.txt
so A can be unblocked and continue. The problem is i have to manually type these commands in two different terminals, since both of them block. I have not figured out a way to automated this in a single shell script. Any help would be appreciated.
You can use & to run the process in the background and continue using the same shell.
$VALGRIND ./test/server_tests 2 >>./test/test.log &
nc 127.0.0.1 1234 < ./test/server_file.txt
If you want the server to continue running even after you close the terminal, you can use nohup:
nohup $VALGRIND ./test/server_tests 2 >>./test/test.log &
nc 127.0.0.1 1234 < ./test/server_file.txt
For further reference: https://www.computerhope.com/unix/unohup.htm
From the question, it looks if the goal is to build a test script for the server, that will also capture memory check.
For the specific case of building a test script, it make sense to extend the referenced question in the comment, and add some commands to make it unlikely for the test script to hang. The script will cap the time for executing the client, executing the server, and if the test complete ahead of the time, it will attempt to shutdown the server.
# Put the server to the background
(timeout 15 $VALGRIND ./test/server_tests 2 >>./test/test.log0 &
svc_pid=$!
# run the test cilent
timeout 5 nc 127.0.0.1 1234 < ./test/server_file.txt
.. Additional tests here
# Terminate the server, if still running. May use other commands/signals, based on server.
kill -0 $svc_id && kill $svc_pid
wait $svc_pid
# Check log file for error
...

Improving script efficiency and reliability

I have this sample code which basically repeats ~20k times. The only thing that changes is the id= in the address and in the echo command line. The id= in the address and the id= in the echo line always correspond. I am running this script on a MAC if that is of any importance.
I would like to improve this script if possible to make it more reliable. Sometimes I either lose connectivity or the session to the server is closed and I am required to log in again but the script keeps running oblivious of the situation. I would like the script to pause if for some reason that happens.
Also after the open command is called, sometimes the server takes longer to responds but the osascript command forces the tab to close after the sleep 2 command has elapsed. This puts me in a position where I am not sure if the server actually took into account the url. Increasing the sleep time is not very reliable. What could be done on that front?
[...]
open 'https://domaineName.com/admin/?adsess=dhnchf6ghd5shak4Dghtfffvw&app=core&module=members&controller=members&do=spam&id=1&status=1' -g
sleep 2
osascript -e 'tell window 1 of application "Safari"
close (tabs where index < (get index of current tab))
end'
echo "done id=1"
open 'https://domaineName.com/admin/?adsess=dhnchf6ghd5shak4Dghtfffvw&app=core&module=members&controller=members&do=spam&id=5&status=1' -g
sleep 2
osascript -e 'tell window 1 of application "Safari"
close (tabs where index < (get index of current tab))
end'
echo "done id=5"
[...]
Any help would be appreciated.
Thank you
You will have to write a script that checks if the main script (lets say main.sh) is currently running on the server (like ps -ef|grep main.sh|grep -v grep)
Check the above output and then execute main.sh inside this script.
This way there is no need for using delay/sleep commands.

Wait until a certain application has exited, then launch another one

On OS X I want to execute an osascript command that waits until a certain application specified by its full .app path has exited, then start another application, e.g. using /usr/bin/open -n /Applications/MyApp.app.
How to achieve the waiting until the application has exited?
A common approach is to perform a waiting loop, for example with pgrep:
while pgrep -f /Applications/TextEdit.app 2>/dev/null ; do sleep 1.0 ; done
Unfortunately, this will sleep too much and delay the start of the other application.
Alternatively, if you know that the application is running, you can use /usr/bin/open:
open -g -W /Applications/TextEdit.app
Unfortunately, this will open the application if it was not running. You could check it is running before calling /usr/bin/open, but this wouldn't be atomic: it could be closing and the open command could restart it.
Both can be encapsulated in osascript (although it probably doesn't make much sense).
osascript -e 'do shell script "while pgrep -f /Applications/TextEdit.app 2>/dev/null ; do sleep 1.0 ; done"'
osascript -e 'do shell script "open -g -W /Applications/TextEdit.app"'
As a side note: open -W actually performs a kqueue wait (non-polling wait) on the process. There might be other commands invoking kqueue and performing the same task without risking restarting the application. It is quite easy to implement in C.

Running Two C++ file in Bash script

I wrote a bash script to run both client and server.
The code is written in cpp and client and server are executable.
$port=8008
$pack_rate=16
echo "Starting server"
./server -p $port -n 512 -e 0.001
echo "Starting client"
./client -p $port -n 512 -l 16 -s localhost -r $pack_rate -d
echo "end"
In the above case, the client will send data packets to the server and the server will process it.
So, both the client and server should run at the same time.
I tried to run the script file, but as expected only
"Starting server"
is getting printed. So, server is running and server will not terminate until it receives 512 packets from the client. But client process can not start until server ends in the bash script.
So, is there any way by which I can run both the process simultaneously using single bash script?
add & add the end of the ./server line, it will run the process in batch mode, and keep executing the rest of the script
You need to add an &:
./server -p $port -n 512 -e 0.001 &
Thus, the script will not wait the end of the server program to continue.

Resources