0

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.

2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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
   ...

2 Comments

if i specify close(fd) in my code, then would kill be unnecessary?
You will have to provide more details on how the code works, and where will the close be issue. But in general, a server side close will NOT guarantee that the server will terminate, unless the server is designed to terminate after one request (or a designated shutdown request).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.