bash script tracking resources of python (or any other) process
Here some example script, which might come handy. This has the advantage, that you do not need to modify your python code. Disadvantage: you need a bash. Of course you can also run a python script from a file. Having the python code in the bash script is just for demonstration purposes.
script
#!/bash/bin
set -e
LOG_OUTPUT="output.log"
LOG_RESOURCES="resources.log"
PYTHON_CODE="
import numpy as np
from datetime import datetime
import time
arr = np.zeros(1000)
print('start', datetime.now(), flush=True)
for ii in range(5000000): # work
arr += 1
print('break start', datetime.now(), flush=True)
time.sleep(3) # break
print('break end', datetime.now(), flush=True)
for ii in range(5000000): # work
arr += 1
print('end', datetime.now(), flush=True)
"
rm -f ${LOG_OUTPUT}
rm -f ${LOG_RESOURCES}
log_process () {
while ps -p $1 > /dev/null
do
top -p $1 -b -d 1 -n 1 >> $LOG_RESOURCES
printf "\n\n" >> $LOG_RESOURCES
sleep 1.
done
}
python -c "${PYTHON_CODE}" >> ${LOG_OUTPUT} 2>&1 & # run python in background
PID_PYTHON=$! # python process id
echo "PID_PYTHON=${PID_PYTHON}"
tail -f ${LOG_OUTPUT} & # show output on terminal
log_process $PID_PYTHON # log needed resources to file
terminal output
$ bash monitor_python_script.sh
PID_PYTHON=6807
start 2023-05-04 10:08:56.510581
end 2023-05-04 10:09:01.770897
break start 2023-05-04 10:09:04.280212
break end 2023-05-04 10:09:07.283365
end 2023-05-04 10:09:14.875994
log file output: output.log
start 2023-05-04 10:08:56.510581
break start 2023-05-04 10:09:04.280212
break end 2023-05-04 10:09:07.283365
end 2023-05-04 10:09:14.875994
log file resources: resources.log
top - 10:08:56 up 1:47, 0 users, load average: 0.08, 0.10, 0.09
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 16.1 us, 10.4 sy, 0.0 ni, 73.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29084.9 free, 3086.8 used, 2072.7 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30686.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:00.38 python
top - 10:08:57 up 1:47, 0 users, load average: 0.08, 0.10, 0.09
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 14.6 us, 0.5 sy, 0.0 ni, 84.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29083.8 free, 3087.9 used, 2072.7 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30685.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:01.53 python
top - 10:08:58 up 1:47, 0 users, load average: 0.23, 0.13, 0.10
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 15.2 us, 0.0 sy, 0.0 ni, 84.4 id, 0.5 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29083.9 free, 3087.8 used, 2072.8 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30685.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:02.69 python
top - 10:09:00 up 1:47, 0 users, load average: 0.23, 0.13, 0.10
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 14.3 us, 0.0 sy, 0.0 ni, 85.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29083.1 free, 3088.5 used, 2072.8 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30684.9 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:03.85 python
top - 10:09:01 up 1:47, 0 users, load average: 0.23, 0.13, 0.10
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 14.3 us, 0.0 sy, 0.0 ni, 85.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29083.3 free, 3088.3 used, 2072.8 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30685.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:05.01 python
top - 10:09:02 up 1:47, 0 users, load average: 0.23, 0.13, 0.10
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 7.1 us, 0.5 sy, 0.0 ni, 92.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.5 st
MiB Mem : 34244.4 total, 29096.4 free, 3075.2 used, 2072.8 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30698.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:06.17 python
top - 10:09:03 up 1:47, 0 users, load average: 0.29, 0.14, 0.11
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 7.1 us, 0.0 sy, 0.0 ni, 92.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29096.4 free, 3075.2 used, 2072.8 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30698.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:07.32 python
top - 10:09:04 up 1:47, 0 users, load average: 0.29, 0.14, 0.11
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29096.9 free, 3074.7 used, 2072.8 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30698.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 S 0.0 0.1 0:08.05 python
top - 10:09:05 up 1:47, 0 users, load average: 0.29, 0.14, 0.11
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.5 us, 0.0 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29096.6 free, 3074.9 used, 2073.0 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30698.3 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 S 0.0 0.1 0:08.05 python
top - 10:09:07 up 1:47, 0 users, load average: 0.29, 0.14, 0.11
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.4 us, 0.5 sy, 0.0 ni, 98.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29087.0 free, 3084.6 used, 2072.8 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30688.8 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 S 0.0 0.1 0:08.05 python
top - 10:09:08 up 1:47, 0 users, load average: 0.29, 0.14, 0.11
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 7.6 us, 0.0 sy, 0.0 ni, 92.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29086.5 free, 3085.1 used, 2072.9 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30688.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:08.95 python
top - 10:09:09 up 1:47, 0 users, load average: 0.35, 0.16, 0.11
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.1 us, 0.0 sy, 0.0 ni, 91.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29086.7 free, 3084.9 used, 2072.9 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30688.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:10.11 python
top - 10:09:10 up 1:47, 0 users, load average: 0.35, 0.16, 0.11
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.0 us, 0.0 sy, 0.0 ni, 92.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29087.0 free, 3084.5 used, 2072.9 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30688.9 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:11.27 python
top - 10:09:11 up 1:47, 0 users, load average: 0.35, 0.16, 0.11
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.1 us, 0.0 sy, 0.0 ni, 91.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29086.5 free, 3085.1 used, 2072.9 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30688.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:12.43 python
top - 10:09:12 up 1:47, 0 users, load average: 0.35, 0.16, 0.11
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 7.1 us, 0.0 sy, 0.0 ni, 92.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29086.3 free, 3085.3 used, 2072.9 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30688.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:13.59 python
top - 10:09:13 up 1:47, 0 users, load average: 0.40, 0.17, 0.12
Tasks: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.1 us, 0.0 sy, 0.0 ni, 91.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 34244.4 total, 29086.2 free, 3085.3 used, 2072.9 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 30688.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
6807 user 20 0 532700 28592 14444 R 100.0 0.1 0:14.74 python