How can a Perl script know its own memory footprint? - windows

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 };'

Related

Why date '+%s%N' not working on mac terminal?

I am working on bash scripts on both linux and mac.
I run this command on remote server with linux OS and it just work perfect.
CURRENT_TIME=$(date '+%s%N')
echo "$CURRENT_TIME"
However, when I run the same command on mac terminal, it shows this error:
1654778186N: value too great for base (error token is "1654778186N")
It look like mac terminal did not recognized the '%N'. What should I do to fix the issue on mac terminal?
%N is a GNU extension to the POSIX standard, as clarified in GNU's own documentation: https://www.gnu.org/software/coreutils/manual/html_node/Time-conversion-specifiers.html
The POSIX version of date, doesn't include %N: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html
If I recall correctly, POSIX-certified systems (and macOS is certified) cannot add custom extensions to POSIX tools to guarantee portability across systems.

Is setsid command missing on OS X?

I mean I can't use it in bash, is it not available on OS X, or is it just missing on my Mac?
It's not a PATH variable issue, because I searched with find command, and there's no file named setsid on my Mac at all.
If it's missing on OS X, is there any alternative to it?
Or if it's the case that I somehow deleted it accidentally, where can I find a copy of it?
use Brew:
brew install util-linux
Yes. /usr/bin/setsid is missing on Mac OS/X.
The OS interface is available, so based on the chapter 2 man page there may be some hope for porting the Linux source to Darwin.
While macOS does not come with a setsid command, it does come with scripting languages which support calling the setsid C function, such as Perl and Python. So, if you don't want to (or for some reason can't) install a setsid command via Homebrew (or MacPorts or whatever), another option is to write your own in a scripting language. As an example, try this Perl script (which I based off this with some minor changes):
#!/usr/bin/perl -w
use strict;
use POSIX qw(setsid);
fork() && exit(0);
setsid() or die "setsid failed: $!";
exec #ARGV;
If you don't like Perl, Python's os module has a setsid function too.
A simple demo, which relies on the fact that /dev/tty is an alias of your controlling terminal if you have one, but reads/writes to it fail with an IO error if you don't:
$ bash -c 'echo I have a controlling terminal. > /dev/tty'
I have a controlling terminal.
$ ./setsid.pl bash -c 'echo I have a controlling terminal. > /dev/tty'
bash: /dev/tty: Device not configured
$
(Warning: With the release of macOS Catalina (10.15) in 2019, Apple deprecated the Perl, Python, Ruby and Tcl language runtimes shipped with macOS – they say new software should not use them, and they may be removed in a future macOS version – and Apple is not going to update their versions, which are becoming increasingly outdated. However, they are still there in Monterey, and while I haven't upgraded to Ventura yet, I haven't heard anything about their removal in that version either. One obviously shouldn't rely on them for any supported applications – if such software needs one of these runtimes, it should install its own copy of them. However, if it is just for a quick hacky script to easily test how some program behaves without a controlling terminal, using these OS-bundled runtimes is still fine.)

Solaris equivalent to OS X's pbcopy / pbpaste?

OS X has nice commands for manipulating the clipboard: pbcopy and pbpaste
I could really use something similar in Solaris 10. I've checked for xsel and xclip, and Solaris 10 doesn't have them. Am I completely out of luck?
There isn't anything quite equivalent on a system-wide basis for Solaris, because Solaris isn't a desktop operating environment. It has desktop OEs, but they're a layer on top of everything else and you cannot depend on them being there.
If you want, you can indeed build xclip from source. However, that's not a system-wide solution.
What problem are you trying to solve, btw?

Fast way to check if file is open on OS X

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 .

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

Resources