Wilson Mar bio photo

Wilson Mar

Hello. Join me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

The Swiss Army knives for Linux and Mac - they’ve thought of everything


Overview

This is a guided tour of Linux utilities, presented in as logical a sequence as I can imagine.

MacOS/Mac OS X uses the BSD version command line tools, which are different from the Linux version, while they are both compliant with POSIX standards.

See https://en.wikipedia.org/wiki/GNU_Core_Utilities.

Uptime

uptime
   
 6:35  up 9 days, 11:56, 7 users, load averages: 2.09 2.08 2.06
   

Load averages lists calcuations of the average system load over three period of times: the last one-, five-, and fifteen-minute periods. Each process using or waiting for CPU (the ready queue or run queue) increments the load number by 1. In a system with four CPUs, a load average of 3.73 would indicate that there were, on average, 3.73 processes ready to run, and each one could be scheduled into a CPU.

Top processes

  1. To list the top hungry processes, and refersh the screen:

    top
    
  2. To cancel the display, press control+C.

    That works with any process you want to kill.

Processes

  1. So that we can kill it for fun, create a background process (by specifying &) which sleep for 999 seconds:

    sleep 999 &
    
  2. Get the process identifier:

    pgrep sleep
    
  3. List background processes:

    jobs
    
    [1]+  Running                 sleep 999 &
    

    The + shows the focus.

  4. To list all processes with a niceness (NI) column:

    ps -l
    

    Expand the terminal window width to avoid line wrapping.

UID   PID  PPID        F CPU PRI NI       SZ    RSS WCHAN     S             ADDR TTY           TIME CMD
501  2752  2749     4006   0  31  0  4320804     44 -      Ss                  0 ttys001    0:00.19 /Users/wilsonmar
   

### Niceness of priority

Default niceness of zero, but can be -20 to +19.

PROTIP: A niceness of +19 is a priority of 99, which is lowest.

nice -n 5 sleep 1000&
   

root permissions are needed to set nice below zero.

  1. Reset nicer:

    renice -n 5 sleep 1000&
    

    Kill

  2. To kill a single program by name:

    pkill sleep
    
  3. To kill several progams by name:

    killall sleep
    

Coreutils

Get the set of utilities from GNU Linux, but for Mac:

brew install coreutils
   

Then you can:

ln -s /usr/local/bin/gtac /usr/local/bin/tac
   

Cron Launchd Background Jobs

This mentions that Apple has deprecated cron in favor of launchd (a daemon running under the System context). to automatically start (after reboots) service programs at boot time.

If the system is turned off or asleep, cron jobs do not execute until the next designated time occurs.

However, a launchd job will run when the computer wakes up if the computer was asleep when the job should have run (if the StartCalendarInterval key has been set).

Since it’s a background process, launchd doesn’t present a user interface. So you get “launchd cannot be run directly.” when you run launchd like other commands.

The standard way now to run a service on Mac OS X is to use launchd , a program that starts, stops and manages daemons and scripts in Apple OS X environments.

An XML document named with file extension .plist defines its properties. The sample file below defines the Nexus Repository Manager from Sonatype.com installed in /opt:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">
<dict>
<key>Label</key>
<string>com.sonatype.nexus</string>
<key>ProgramArguments</key>
<array>
<string>/opt/nexus/bin/nexus</string>
<string>start</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

The above is the com.sonatype.nexus.plist file in /Library/LaunchDaemons/

Change its ownership and access rights:


sudo chown root:wheel /Library/LaunchDaemons/com.sonatype.nexus.plist
sudo chmod 644 /Library/LaunchDaemons/com.sonatype.nexus.plist
   

PROTIP: Consider setting up a different user to run the repository manager and adapt permissions and the RUN_AS_USER setting in the nexus startup script.

To manually start it after the configuration:


sudo launchctl load /Library/LaunchDaemons/com.sonatype.nexus.plist
   

Install appium_console gem

gem uninstall -aIx appium_lib
gem uninstall -aIx appium_console
gem install --no-rdoc --no-ri appium_console

Install flaky gem:
https://github.com/appium/flaky
(posix-spawn)

gem uninstall -aIx flaky
gem install --no-rdoc --no-ri flaky

More on OSX

This is one of a series on Mac OSX: