Reputation: 1
Im trying to record docker stats for every file in the mydata
directory. For example if one of the files is names piano.txt
I would like the output file to be piano_stuff.txt
. This is what I have so far:
import subprocess
import signal
import os
for file_name in os.listdir('mydata'):
data_txt = "./" + file_name.split(".")[0] + "_stuff.txt"
dockerStats = subprocess.Popen("docker stats --format {{.MemUsage}} >> ${data_txt}", shell=True)
os.killpg(os.getpgid(dockerStats.pid), signal.SIGTERM)
Upvotes: 0
Views: 59
Reputation: 532043
Don't use shell=True
. Open the file locally, and pass the file object as the stdout
argument. You can also use the --no-stream
option to have the command exit after producing one line of output, rather than asynchronously trying to kill the process as soon as possible. (You might get multiple lines of output, or you might get none, depending on when the OS schedules the Docker process to run.)
with open(data_txt, "a") as f:
subprocess.run(["docker", "stats", "--format", "{{.MemUsage}}", "--no-stream"], stdout=f)
Upvotes: 1