5 ms·
I know what I am about to say is an absolutely ugly hack, but... If you cannot install non-standard software (example: customer owner, company operated RHEL bo
by znpy 9y ago
I know what I am about to say is an absolutely ugly hack, but...
If you cannot install non-standard software (example: customer owner, company operated RHEL boxes) you can get an idea of the current progress of cp/tar and similar programs by inspecting file-descriptor files in /proc.
Assuming that you have a process (like cp) with pid 1234, then in /proc/1234/fd you will find files with a number (corresponding to the file descriptor number in the context of the process) that are either symbolic links to the actual file or to socket connection (or other data structures). Let's assume that source file has fd 3 (you get this information via ls -l /proc/1234/fd )
Then, after you can cat /proc/1234/fdinfo/3 and look at the pos value: that's the position of the cursor in the file.
It's ugly and unpractical, but it should work on every Linux systems featuring basic tools.
- arbie 9y agoThis is pretty intense, but applicable to many enterprise setups!
- anitil 9y agoThis is just fantastic. I had all the bits of information in my head to be able to do this, but never connected the dots. Not ugly at all!
- znpy 9y agoThanks mate :)
- Waterluvian 9y agoThis is what I call a "22/7" solution. Close enough to be useful but you'd never use it formally. This is really great to have in my back pocket the next time a long running copy process feels hanged (often via. Ansible) and I just want a sense of, "okay are we minutes, hours, or days away from being done?"
- progress-code 9y ago#!/bin/sh for pid in $@; do cd /proc/"$pid"/fd || continue for fd in *; do file=$(readlink "$fd"); if [ ! -z "$file" ]; then size=$( (du -b "$file" 2>/dev/null || echo 0) | awk '{print $1}') if [ "$size" = "0" ]; then continue fi pos=$(< ../fdinfo/"$fd" grep -F 'pos:' | grep -oP '\d+') percent=$(echo "100*${pos}/${size}" | bc -l | xargs printf "%2.2f") echo "${file}: ${percent}% (${pos}/${size})" fi done done
- PowerfulWizard 9y agoI ended up writing very similar code to do the same thing: https://gist.github.com/jhlb/83999422a8d7e8a6e28e https://gist.github.com/jhlb/83999422a8d7e8a6e28e EDIT: as mentioned elsewhere in the thread, the pv program has a way to watch a PID as well that makes this program unnecessary.
- glandium 9y agoThat's exactly what the tool in the link does.
- kjeetgill 9y agoI often use the basic: $ watch -n.2 "du -sb src dest" I suppose you could add awk to get a percentage.
- andrewmackrodt 9y agoThanks for the explanation, TIL :)