Bash Script Keeps Logging Data After Application Stops Running

68 views Asked by At

I'm working on a Linux script to log system utilization data while running scientific applications on a Jetson Nano. I'm using the jtop library for this purpose.

The Issue:
My current script keeps logging data even after the application has finished executing. I want it to start logging when the application starts and stop as soon as the application stops, to keep the data reliable.

Here's a snippet of the Python code I'm using with jtop:

from jtop import jtop, JtopException
import csv
import argparse


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Simple jtop logger')
    # Standard file to store the logs
    parser.add_argument('--file', action="store", dest="file", default="log.csv")
    args = parser.parse_args()

    print("Simple jtop logger")
    print("Saving log on {file}".format(file=args.file))

    try:
        with jtop() as jetson:
            # Make csv file and setup csv
            with open(args.file, 'w') as csvfile:
                stats = jetson.stats
                # Initialize cws writer
                writer = csv.DictWriter(csvfile, fieldnames=stats.keys())
                # Write header
                writer.writeheader()
                # Write first row
                writer.writerow(stats)
                # Start loop
                while jetson.ok():
                    stats = jetson.stats
                    # Write row
                    writer.writerow(stats)
                    print("Log at {time}".format(time=stats['time']))
    except JtopException as e:
        print(e)
    except KeyboardInterrupt:
        print("Closed with CTRL-C")
    except IOError:
        print("I/O error")
# EOF

The bash script I have written is as follows:

#!/bin/bash

echo "Starting jtop_logger.py..."

cd ~/Desktop && python3 jtop_logger.py &
PID1=$!

echo "jtop_logger.py started with PID: $PID1"

echo "Starting mpirun..."

cd ~/Desktop/vpic/build && mpirun -n 2 ./waveguide.Linux --tpp 4

echo "mpirun finished. Killing jtop_logger.py..."

kill $PID1

# Adding a sleep to observe the process list
sleep 5

echo "Checking if jtop_logger.py is still running..."
if ps -p $PID1 > /dev/null; then
   echo "jtop_logger.py is still running."
else
   echo "jtop_logger.py has been terminated."
fi

echo "Script finished."

This is the output I am getting right now. The jtop_logger.py will regenerate itself after the shell script will finish executing

1

There are 1 answers

0
alhernau On

Disclaimer: not using jetson myself - not enough reputation to write a comment

Here is my guess: jtop nodule (jetson client) doesn't detect the app was closed, thus jtop.ok() still returns true.

Jetson API documentaton doesn't mention any exception raised in case of disconnection, thus the only workaround I see for now is to test for jtop.stats() length.

If empty it means the server didn't sent info, so probably server is down or client disconnected.

            # Start loop
            while jetson.ok():
                stats = jetson.stats
                if not len(jetson.stats):
                    # workaround: we've detected a probable disconnection
                    raise Exception("Jetson disconnection - no data received")
                # Write row
                writer.writerow(stats)
                print("Log at {time}".format(time=stats['time']))