Applescript中的磁盘活动
时间:2020-03-05 18:53:58 来源:igfitidea点击:
如何在Applescript中轮询磁盘活动?每隔N秒检查一下磁盘X是否正在读取,写入或者空闲,然后执行某些操作。
解决方案
回答
通常,轮询比发生事件时通知通知的效率低。另外,如果我们要检查是否正在从磁盘读取某些内容,则可能是我们自己访问了该磁盘,可能会影响我们尝试观察的内容。
从10.5开始,OSX包含一个称为"文件系统事件"框架的文件,该框架提供有关文件系统更改的详细信息。我们遇到的问题是,这仅是Objective-C。苹果公司提供了有关此API的一些不错的文档。
幸运的是,还有" call method" AppleScript命令。这使我们可以在AppleScript中使用Objective-C对象。这是关于此的文档。
我没有任何经验,因此参考文档。希望这可以前进。
回答
我们可以定期运行终端命令iostat。我们必须将结果解析为可以消化的形式。
如果我们对各种UNIX命令行工具了解得足够多,我建议iostat将输出通过管道传输到awk或者sed,以仅提取所需的信息。
回答
我们应该真正看一下Dtrace。它具有执行此类操作的能力。
#!/usr/sbin/dtrace -s /* * bitesize.d - analyse disk I/O size by process. * Written using DTrace (Solaris 10 build 63). * * This produces a report for the size of disk events caused by * processes. These are the disk events sent by the block I/O driver. * * If applications must use the disks, we generally prefer they do so * sequentially with large I/O sizes. * * 15-Jun-2005, ver 1.00 * * USAGE: bitesize.d # wait several seconds, then hit Ctrl-C * * FIELDS: * PID process ID * CMD command and argument list * value size in bytes * count number of I/O operations * * NOTES: * The application may be requesting smaller sized operations, which * are being rounded up to the nearest sector size or UFS block size. * To analyse what the application is requesting, DTraceToolkit programs * such as Proc/fddist may help. * * SEE ALSO: seeksize.d, iosnoop * * Standard Disclaimer: This is freeware, use at your own risk. * * 31-Mar-2004 Brendan Gregg Created this, build 51. * 10-Oct-2004 " " Rewrote to use the io provider, build 63. */ #pragma D option quiet /* * Print header */ dtrace:::BEGIN { printf("Sampling... Hit Ctrl-C to end.\n"); } /* * Process io start */ io:::start { /* fetch details */ this->size = args[0]->b_bcount; cmd = (string)curpsinfo->pr_psargs; /* store details */ @Size[pid,cmd] = quantize(this->size); } /* * Print final report */ dtrace:::END { printf("\n%8s %s\n","PID","CMD"); printa("%8d %s\n%@d\n",@Size); }
从这里。
运行使用
sudo dtrace -s bitsize.d
回答
正如Porkchop D. Clown提到的,我们可以使用iostat。我们可以使用的命令是:
iostat -c 50 -w 5
它将每5秒运行iostat 50次。