0

I have a script which logs certain data when a benchmark (some c code like matrix multiplication) runs.

I want to first start the log script when benchmark starts, this is easy since I can just start the binary from the log script and then proceed to log the info.

But the real question is when do I stop it? The benchmark can stop at anytime (The log script shouldn't stop the benchmark). How do I get the info/variable which can be used in the log script to stop it when benchmark program stops?

I was thinking if I can use PID of the benchmark, but then thought there should be a better solution than searching and using the PID.

Thanks!

Ujjwal
  • 102
  • 1
  • 2
  • 8
  • How does your log script get the data to log? Maybe you could use something like: _"if there's no more data to log, then exit script"_ – xav Mar 14 '14 at 23:35
  • That is a really good question. The log data is the temperature and other parameters which are always present, due to benchmark, they will change. I want to log this change when benchmark runs. Hope that clarifies. – Ujjwal Mar 14 '14 at 23:42
  • Have you tried this: [http://stackoverflow.com/questions/14254118/waiting-for-background-processes-to-finish-before-exiting-script](http://stackoverflow.com/questions/14254118/waiting-for-background-processes-to-finish-before-exiting-script) ? – xav Mar 14 '14 at 23:46
  • No, as soon as I go through that link, I will summarize it in this thread. Thank you. – Ujjwal Mar 15 '14 at 00:43

1 Answers1

1

Perhaps you could try something like this:

#!/bin/bash
#
# Your main script
#

# Run your log program in background
your_log_program &

# The PID of last background program
LOGPROGRAMPID=$!

# Install EXIT trap  (EXIT is a bash's special event)
trap 'kill -15 $LOGPROGRAMPID; exit 0' EXIT
# In foreground launch your benchmark program
run_your_benchmark_program

# When benchmark program ends your trap will be launched and it will kill your log
# program.
Gooseman
  • 2,102
  • 17
  • 16