Modern shells are programming languages, so you can use arithmetic to check for dates condition. However,
top in batch mode runs until the iteration limit is reached, and once you start
top, you can't check for dates condition. So you have to run
top with iteration limit set to 1 and restart it in while loop:
date1=`date +%s` #we initialize date1 with current unix time
while ((1)); do
if (($time1 <= $date1 && $date1 <= $time2)); then
top -o %MEM -b -n 1 >> $output_file
# something should update date1, time1 or time2 here...?
To use this script, copy/paste it to for example mytop.sh file, set executable on it:
chmod +x mytop.sh
and then you can start it as usual, by:
To cancel, press ctrl+c.
There is no visual indicator it's running and some people feel better when there is some output. If you want, you can add for example:
line in the script after
sleep line, to print new dot on every iteration.