Fast way to check if file is open on OS X - macos

Is there a fast way to check if a file handle is closed from the command line on os x?
lsof works, of course, but is super slow.

You´ll probably want to check out the DTrace Family man dtrace.
If you´re only interested on pure "file actions" you should have a look at opensnoop, which builds on DTrace and has been included since Mac OS X 10.6.
You can show all file in use by a process (by -p pid or -n name) and watch files with -f /path/to/file .

Related

Running Octave-cli on Iterm/Zsh/Omgzsh in Mac OSX

It feels impossible to get it to run on any custom terminal. I know that octave-cli.app is there but it always opens in the standard terminal. Any ways to execute octave scripts like a compiler (or) run it interactively like an interpreter from Iterm?
Using Mac OSX 10.9+
Edit:
I know how to export path variables. But having searched the web can't find a way to do it. Is it even possible? I even tried it using homebrew to no avail.
You can see the content of octave-cli.app, it's a script. Mine goes like this
open -a Terminal.app /usr/local/octave/3.8.0/bin/octave | logger 2>&1
It specify the terminal application used to open octave. This is the reason of your problem, as I think.
The solution is linking octave-cli in system path, better locates at "/usr/local/bin". like
ln -s /usr/local/octave/3.8.0/bin/octave-cli /usr/local/bin/octave-cli
Finally, octave can be accessed via any terminal(like iTerm) or shell(bash, zsh) by just type "octave-cli" command, which will be searched in system path and found to executed directly.

What is the name of the command that shows disk access in real time?

I am running Mac OS X ( 10.9.4 ) using the built in Terminal, and bash version:
3.2.51(1)-release (x86_64-apple-darwin13)
At one point in the past, there was a CLI command I could issue that would show me what was going on with my drives, kind of like a tail -f system.log but deeper into the system where it would look at the actual drives and what was going on. A fully idle machine, say one that was sleeping and I was ssh'd into, would show a page that was doing little or nothing. Saving a file, or doing anything disk related, and tons of data would scroll past, which you could pipe to a file and grep through.
I have goggled and can't find this command.
Not sure about Mac OS X, but on Linux, there are several:
iostat
iotop
sar
dstat
vmstat
iozone
Most of them should be available on Mac OS X. I know that iostat is.
Or you can always look in
/sys/block/[device]/stat
or
/proc/diskstats
This was in fact a duplicate of question noted by anishane.
The command I was looking for was fs_usage but lsof, while not exactly what I wanted, is a close second in being helpful to track down the problems I'm running into.
I think you are looking for iosnoop. Seems like it is a wrapper on dstat only, as when I ran it without sudo it gave me the following error.
% iosnoop
dtrace: system integrity protection is on, some features will not be available
dtrace: failed to initialize dtrace: DTrace requires additional privileges

mount a drive on Mac OS X with bash script (and NOT use expect)

I'd like to mount a Samba drive in OS X using bash. This line does the trick:
mount -t smbfs //$SAMBAUSER#$ADRESS/$NAMEOFSHARE $MACOSXPATH
only one problem. I want it done without user input - which means no password can be manually put in. And I'm not going to ask my users to download fink just so they can install expect (as seen here).
I tried applying the accepted solution to a similar StackOverflow problem shown here by doing this:
echo "mypassword" | mount -t smbfs //$SAMBAUSER#$ADRESS/$NAMEOFSHARE $MACOSXPATH --stdin
but no luck - that doesn't work and Mac OS X tells me I used mount command improperly:
usage: mount [-dfruvw] [-o options] [-t ufs | external_type] special node
mount [-adfruvw] [-t ufs | external_type]
mount [-dfruvw] special | node
Any suggestions? This would be easy with an expect script - but that would ruin the user experience to have that prerequisite in my mind.
If mount(8) can't just call the mount syscall on the filesystem, it looks for a program to help it. On FreeBSD and Mac OS X, those helper programs follow the naming convention mount_XXX, where XXX is the -t argument's value.
That means you want to check the mount_smbfs(8) man page, which tells us about -N:
-N Do not ask for a password. At run time, mount_smbfs reads the ~/Library/Preferences/nsmb.conf
file for additional configuration parameters and a password. If no password is found,
mount_smbfs prompts for it.
Unfortunately the man page trail ends with one for nsmb.conf that doesn't mention anything about storing passwords. On FreeBSD 8.0, at least, the solution is to put a password key with a plain text(!) password value under a [SERVER:USER] heading. That would be type C according to the linked nsmb.conf man page.
So it seems that you'll want to dump a pre-configured nsmb.conf into your user's ~/Library/Preferences/ directory and then call your mount command with -N. As far as I know you can't provide a hashed value, which is not especially awesome. I'll try to get access to a MacBook in a few hours to test this.
NB: This is not how to do it with the GNU toolchain. If you're on Linux, you're probably going to be using something like mount.cifs(8). The correct solution in that case is the credentials=filename option (used after -o, of course), where filename is a file of credentials in key=value form, separated by newlines. See http://linux.die.net/man/8/mount.cifs
The answer in this apple support discussion thread worked for me:
osascript -e 'mount volume "smb://user:password#server/share"'
I'll give you 2 options. First, include the password on the command line:
mount -t smbfs //$SAMBAUSER:$PASSWORD#$ADRESS/$NAMEOFSHARE $MACOSXPATH
This is not a great option because the command line (including password) is visible to anyone who happens to be logged in at the moment. Not great, but it is a possibility.
Second, use expect. The Mac OS X Hints article you linked dates from 2002, when OS X v10.2 would've been current. While 10.2 apparently didn't include expect as a standard component, 10.6 does, and I'm pretty sure it's been included for several versions now.
#!/usr/bin/expect -f
spawn mount -t smbfs //fred#12.34.56.78/myfiles /tmp/mountpoint
expect "Password:"
send "wibble\r"
wait
You not only can use a hashed value, you're expected to, at least in older releases. The crypt option for smbutil is not DOD-level security but as with most security, you're trying to keep the honest people honest: the bent ones will find a way. It seems to be broken in Mountain Lion but the nsmb.conf file in ~/Library/Preferences should be secure at the OS level. The /etc/nsmb.conf would override it if it exists. I'm sure there's a reason why plain text files are obsolete but didn't we go this with NetInfo? How long did it take for Apple to allow the old standbys (/etc/hosts, /etc/passwd) to be used?

Discovery of Dynamic library dependency on Mac OS & Linux

On Windows there is a tool Depends.exe to discover dependency of an EXE/DLL file on other DDLs. Which commandline tool is equivalent on Mac OS and Linux?
Mac OS X: otool -L file
Linux: ldd file
If those commands don't provide what you want, on Mac OS X you can dump all the load commands with otool -l file. On Linux you can dump the entire contents of the dynamic section with readelf -d file.
You can also try MacDependency (https://github.com/kwin/macdependency) which provides an UI replacement for otool on MacOS X. It shows complete dependency trees and the exported symbols as well.
try ldd in the terminal. This will provide you a list of dynamic libraries that the binary needs.
You can put something like following into your bashrc so that you can always use "ldd" as interface but it will redirect macos equivalent one if machine is mac.
# Macos equivalent of ldd
if [[ "$OSTYPE" =~ "darwin"* ]]
then
alias ldd="otool -L"
fi

How can a Perl script know its own memory footprint?

I have a long running Perl script and I'd like to let it know (and report) how much memory it is using. I'd like to have this information both on Linux and Windows and if possible on Mac OS X as well.
These Perl modules could help you:
Windows: Win32::Process::Memory
Linux(and maybe Mac OSX): Linux::Smaps
This will show you how:
http://perldoc.perl.org/Devel/Peek.html
Also, http://perldoc.perl.org/perlguts.html
and, man pages for perldebug and perldebguts.
This is a quick and dirty and most of all CPAN-free method. It works on any OS that provides a /proc file system, that is Linux and Unix derivates, including Mac OS X, and also on Cygwin under Windows:
perl -e 'print qx{ grep VmSize /proc/$$/status };'

Resources