Mac Applescript to monitor shell script progress

markjulie

macrumors newbie
Original poster
May 13, 2009
2
0
North Carolina, USA
From https://www.jackenhack.com/mac-os-purgeable-remove-clear-space/ great advice, I want to make an Applescript app of the shell script command mentioned: [dd if=/dev/zero of=~/stupidfile.crap bs=20m] monitoring the size while it remains under 10gb and stopping the shell script at a predetermined size. I am a low intermediate user of Applescript and Terminal. Jack's instructions work well, but I am trying to automate his work to run regularly and manage four desktops.

tell application "Script Debugger" to activate
tell
application "Terminal" to activate
tell
application "Finder" to activate
do shell script
"touch ~/stupidfile.txt"
do shell script "cd $HOME"
set thePath to "dd if=/dev/zero of=$HOME/stupidfile.txt bs=20m"
--Up to this point, the commands work, but I don't know how to monitor the progress or set limits.


--From here on, I want to do something that monitors the file size (with or w/o the alerts)
do shell script thePath
set theSize to (do shell script "stat -f%z $HOME/stupidfile.txt")
theSizeMB is equal to theSize / (10^3)
display alert "The file is " & theSizeMB & "MB"
repeat while theSize is less than 10^7
theSizeMB is equal to theSize / (10 ^ 3)
display alert "The file is " & theSizeMB & "MB"
end repeat

--The code is incomplete, but could this be modified to work?

I ran this and bloated my HD to <1gb of free space, so it works to create the file.

Thanks,

Mark
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
I ran this and bloated my HD to <1gb of free space, so it works to create the file.
No need for me to warn you then.

Working with "System Events" example :

Code:
set documentsPath to path to documents folder as text
set aFile to documentsPath & "crapfile2.txt"
set dd_process_id to do shell script "dd if=/dev/zero of=" & quoted form of POSIX path of aFile & space & "bs=1b &> /dev/null & echo $!"
tell application "System Events" to set aFileSize to file aFile's size
repeat while (aFileSize as integer) is less than or equal to 1001390592
    tell application "System Events" to set aFileSize to file aFile's size
end repeat
do shell script "kill" & space & dd_process_id
Working with do shell script example :

Code:
set documentsPath to path to documents folder as text
set aFile to documentsPath & "crapfile2.txt"

do shell script "dd if=/dev/zero of=" & quoted form of POSIX path of aFile & space & "bs=1b &> /dev/null & dd_id=\"$!\"; let file_size=$(stat -f%z" & space & quoted form of POSIX path of aFile & space & "| awk '{ printf \"%u\", $1/1000/1000 }'); while (( $file_size <= 1000 )); do file_size=$(stat -f%z" & space & quoted form of POSIX path of aFile & space & "| awk '{ printf \"%u\", $1/1000/1000 }'); done; kill $dd_id"
Note: Tested on El Capitan, file created is 1GB. YMMV.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,423
4,425
Pale blue comma
After reading the linked article, it seems that the goal of using 'dd' is to make a large file roughly 10GB in size. While the article also meantions watching the file's size using another command, and terminating it when it's big enough, there's a far simpler solution:
Simply tell 'dd' when it should stop.

This is easily accomplished on the input side of 'dd', with the 'count=n' option. The value of n is given in units of input blocks, not MB or GB. Since the cited 'dd' command has 'bs=20m', that means both the input block and output block size are 20MB. The block size could also be 10m or 100m just as easily; there's nothing special about 20m.

Suppose the desired output file size is 1GB. That's the same as 1024MB. So calculate 1024 / 20, and that's the count of input blocks where 'dd' should stop. That number is 51 (or 52 if you round up), so this is the simple Terminal command:
Code:
dd if=/dev/zero bs=20m count=52 of=big
You could use a count of 520 (or bigger) if the goal is to make 10GB files. The key is to calculate the proper count using the desired file size divided by the input-block size.

A 1GB file takes about 10 secs to make on my test machine. Using 'cp' or the Finder to duplicate the resulting file takes 20 secs. So if the overall goal is to make multiple files of this size, it's faster to repeat the 'dd' command using a new output file name (of=big2), than it is to duplicate the 1st big file.

One could write an AppleScript to do the repetition, or a shell script could also be made and run in a single Automator action. The 'df' command could also be employed, so the repetition stops when a certain amount of disk free space remains.


The original post suffers from what is known as the XY problem.

In short, Y is presumed to be an effective solution for the problem X. One then asks how to accomplish Y (monitoring a 'dd' process and terminating it at a desired point), rather than how to solve X (creating a file of zeros with a certain large size). The discussion then gets mired down in how to accomplish Y, often with substantial complexity, rather than how to solve X, which may well have a simple or straightforward solution.

I had to read the linked article and figure out what it was trying to accomplish, in order to discern the actual X problem to solve.
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
This is easily accomplished on the input side of 'dd', with the 'count=n' option. The value of n is given in units of input blocks, not MB or GB. Since the cited 'dd' command has 'bs=20m', that means both the input block and output block size are 20MB. The block size could also be 10m or 100m just as easily; there's nothing special about 20m.
A case of not reading the man page. :rolleyes: