Category: MacOSCmdLine

  • Command-line Knowledge for MacOS: Preventing Sleep

    One of the things I love about what I do is that I discover some oddity that makes me say to myself: “Self, Why did you not know about that?” Today’s entry in that category is MacOS’s caffeinate command. It’s a command-line tool that prevents your Mac from sleeping for a period of time, or while a specific process is running. It is simple, reliable, and does not require installing anything.


    How caffeinate works

    macOS has several power management behaviors:

    • Display sleep: the screen turns off.
    • System sleep: the whole Mac goes to sleep.
    • Disk sleep: disks can spin down.
    • Idle sleep: sleep that happens because there is no activity.

    caffeinate creates a temporary “stay awake” assertion with the power management system. While the assertion is active, macOS will avoid the kind of sleep you told it to prevent.


    The simplest usage

    Start caffeinate in Terminal:

    caffeinate
    

    As long as that command is running, your Mac will be kept awake (until you stop it).

    To stop it, press:

    • Ctrl + C

    Common options (the ones you will actually use)

    You can tailor what gets prevented:

    • -d prevents display sleep
    • -i prevents idle system sleep
    • -m prevents disk sleep
    • -s prevents system sleep (particularly useful on AC power)

    Examples:

    # Keep the Mac awake, but let the display sleep
    caffeinate -i
    
    # Keep the display awake (presentation mode)
    caffeinate -d
    
    # Keep system awake (often best for long tasks)
    caffeinate -s
    
    # Keep everything you can awake
    caffeinate -dims
    

    Note: Some flags make more sense in certain situations (for example, -s is most relevant when the Mac is plugged in).


    Set a time limit with -t

    If you want “stay awake” behavior for a fixed amount of time, use -t with a number of seconds.

    # Stay awake for 1 hour
    caffeinate -i -t 3600
    

    A handy trick: if you think in minutes, multiply by 60.


    Tie it to a command (the best way for long jobs)

    Instead of manually starting and stopping caffeinate, you can run it while another command runs.

    # Keep the Mac awake while rsync runs
    caffeinate -i rsync -av ~/Source/ /Volumes/Backup/Source/
    

    When the rsync command finishes, caffeinate exits automatically.

    This pattern is great for:

    • Backups and file copies
    • Long builds
    • Data imports
    • Video exports
    • Large downloads

    Real-world recipes

    1) Keep your Mac awake while a large file downloads

    If you are using curl:

    caffeinate -i curl -LO "<https://example.com/bigfile.zip>"
    

    2) Prevent sleep during a presentation

    caffeinate -d
    

    Leave it running for the duration of the presentation, then Ctrl + C.

    3) Keep your Mac awake for a meeting, then stop automatically

    # 90 minutes
    caffeinate -i -t 5400
    

    Safety notes and gotchas


    • Preventing sleep can use more power and generate more heat, especially on laptops.
    • If you use caffeinate with no time limit, it will run until you stop it. If you forget, your Mac may stay awake all night.
    • If the goal is “do not lock my screen,” that is a different setting. caffeinate is about sleep behavior, not password prompts.

    Quick cheat sheet

    caffeinate              # keep awake until you stop it
    caffeinate -i           # prevent idle sleep
    caffeinate -d           # prevent display sleep
    caffeinate -i -t 600    # preventsleep for 10 minutes
    caffeinate -i <command> # keep awake while command runs
    

    Linux equivalent?

    Many Linux desktop environments use the caffeine utility, which includes a caffeinate binary in some distributions (like Debian/Ubuntu) that mimics the macOS syntax.

    Installation:

    • Ubuntu/Debian/Mint: sudo apt install caffeine
    • Arch Linux: sudo pacman -S caffeine-ng
    • Fedora: sudo dnf install caffeine-ng [1, 2]

    Syntax & Usage:

    • Prevent screen blanking while running a command: bash caffeinate COMMAND Use code with caution.
    • Toggle via GUI: Run caffeine or caffeine-indicator from your app menu to get a coffee cup icon in your taskbar, then click it to toggle idle-prevention on and off manually

    Closing thoughts

    If you work in Terminal even occasionally, caffeinate is one of those small tools that saves you from big annoyances. The best default is usually caffeinate -i, and the best habit is adding it in front of any command you expect to run for a long time.

  • Command-line Knowledge for MacOS: Network Troubleshooting

    I am teaching a graduate-level class in computer communications and networking in this semester. One of the first things I do in that class is review the command-line tools you have available for network configuration and troubleshooting. Y’all know I’m an Apple person and I find that, like most command-line level stuff, Apple people are not very cognizant of the power you have with the command-line. So, I thought I’d share some of the notes from my class focused on macOS networking.

    macOS ships with a strong set of command-line networking tools. Many of them come from the BSD and POSIX tradition, and work almost exactly the same as they do on FreeBSD, OpenBSD, and Linux. On top of that, Apple adds a set of system configuration and diagnostics tools that integrate with macOS frameworks like System Configuration, NetworkExtension, and the Wi‑Fi stack.

    This post is organized in two parts:

    • Traditional BSD utilities you can expect on most Unix-like systems
    • Apple-specific tools that are especially useful on macOS

    Part 1: Traditional BSD utilities (the classics)

    These tools are generally stable, script-friendly, and easy to combine in pipelines.

    ping and ping6: basic reachability and latency

    Use ping to answer two questions:

    • Can I reach this host?
    • What is the latency and packet loss?

    Examples:

    ping -c 5 1.1.1.1
    ping -c 5 [example.com](<http://example.com>)
    

    Notes:

    • ICMP can be blocked by firewalls, so “no ping” does not always mean “no connectivity.”

    traceroute: path discovery (where packets go)

    traceroute shows the hop-by-hop path toward a destination.

    traceroute [example.com](<http://example.com>)
    

    Helpful options:

    • -n for numeric output (faster; avoids DNS lookups)
    • -w to adjust timeout

    ifconfig: interfaces, addresses, and link state

    On macOS, ifconfig is still the canonical interface tool.

    Show all interfaces:

    ifconfig
    

    Show one interface:

    ifconfig en0
    

    Common things to look for:

    • IPv4 address (inet)
    • IPv6 address (inet6)
    • MAC address (ether)
    • Status (status: active)

    Tip: on many Macs, Wi‑Fi is often en0 and Ethernet is often en1 or another en*, but it varies.


    netstat: sockets and routing info (legacy but handy)

    netstat can show listening ports, established connections, and routing tables.

    Show listening TCP sockets:

    netstat -anv | grep LISTEN
    

    Show routing table:

    netstat -rn
    

    macOS note: for some tasks, netstat is being superseded by tools like lsof, route, and Apple’s newer diagnostics tools, but it is still useful.


    route: view and modify the routing table

    View the current default route:

    route -n get default
    

    Show the route to a specific destination:

    route -n get 8.8.8.8
    

    Be cautious editing routes on a laptop. In most cases you want to inspect rather than change.


    arp: inspect the ARP cache (IPv4 LAN neighbor mapping)

    When troubleshooting local LAN issues (wrong gateway, duplicate IPs, suspicious devices), arp can help.

    arp -a
    

    nslookup, dig, and host: DNS troubleshooting

    DNS is often the source of “the internet is down” reports. These tools confirm what your resolver is doing.

    dig is the most informative:

    dig [example.com](<http://example.com>) A
    dig [example.com](<http://example.com>) AAAA
    dig +short [example.com](<http://example.com>)
    

    Check which DNS server is answering:

    dig [example.com](<http://example.com>) @1.1.1.1
    

    nc (netcat): quick TCP/UDP testing

    nc is great for testing whether a port is reachable.

    Test TCP connect:

    nc -vz [example.com](<http://example.com>) 443
    

    Listen on a port (for local testing):

    nc -l 9000
    

    tcpdump: packet capture (the gold standard)

    If you want to know what is actually on the wire, use tcpdump.

    Capture on Wi‑Fi (en0) and show DNS traffic:

    sudo tcpdump -i en0 -n port 53
    

    Capture HTTPS flow behavior:

    sudo tcpdump -i en0 -n tcp port 443
    

    Pro tips:

    • Always start with -n to avoid DNS lookups in your capture output.
    • Keep captures narrow (filter early) so you can read them.

    lsof: map ports back to processes

    When you see “something is listening on port 8080,” lsof answers “what process?”

    sudo lsof -nP -iTCP:8080 -sTCP:LISTEN
    

    This is often the fastest way to diagnose conflicts with dev servers, proxies, or VPN clients.


    Part 2: Apple-specific tools (macOS superpowers)

    These tools understand macOS network services and configuration layers better than the classic Unix utilities.

    networksetup: manage network service settings

    networksetup is a friendly CLI wrapper for many settings you would otherwise click through in System Settings.

    List network services:

    networksetup -listallnetworkservices
    

    Show Wi‑Fi info for a service name:

    networksetup -getinfo "Wi-Fi"
    

    See DNS servers configured for a service:

    networksetup -getdnsservers "Wi-Fi"
    

    Set DNS servers (example):

    sudo networksetup -setdnsservers "Wi-Fi" 1.1.1.1 1.0.0.1
    

    Tip: macOS has multiple “network services” (Wi‑Fi, Ethernet adapters, Thunderbolt bridges, VPN interfaces). Troubleshooting often gets easier once you know which service you are actually using.


    scutil: System Configuration and name resolution details

    scutil is lower-level than networksetup and is extremely useful when configuration looks correct in the UI but behavior is still odd.

    Show current DNS resolver state (one of the best troubleshooting commands on macOS):

    scutil --dns
    

    Show proxy configuration:

    scutil --proxy
    

    Get the “primary” network interface/service in use:

    scutil --nwi
    

    ipconfig (macOS): DHCP and interface address details

    On macOS, ipconfig is not the Windows tool. It is a small utility mainly for DHCP and interface state.

    Get the IPv4 address of an interface:

    ipconfig getifaddr en0
    

    Renew DHCP lease:

    sudo ipconfig set en0 DHCP
    

    system_profiler: inventory network hardware (when names are confusing)

    If you are unsure what hardware exists (especially with docks/adapters), system_profiler can help.

    Wi‑Fi details:

    system_profiler SPAirPortDataType
    

    Network hardware list:

    system_profiler SPNetworkDataType
    

    log show: read macOS unified logs for networking clues

    When problems are intermittent, the system logs can expose DHCP renewals, Wi‑Fi roam events, and VPN behavior.

    Example (last hour, filtering for Wi‑Fi keywords):

    log show --last 1h --predicate 'eventMessage CONTAINS[c] "Wi-Fi"'
    

    This is powerful but can be noisy. Narrow your time window and predicate.


    Wi‑Fi diagnostics: airport

    Apple ships a Wi‑Fi utility called airport. It is not always on your PATH, so you typically run it via the full path.

    Show Wi‑Fi link status (SSID, RSSI, channel, rates):

    /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I
    

    Scan for nearby networks:

    /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s
    

    If you troubleshoot Wi‑Fi often, airport -I is the quickest “is my signal bad?” check.


    dns-sd: browse and test Bonjour / mDNS service discovery

    macOS heavily uses Bonjour (mDNS) for printers, AirPlay, and peer discovery.

    Browse services of a type (example: AirPrint):

    dns-sd -B _ipp._tcp
    

    Resolve a service instance:

    dns-sd -L "Some Printer" _ipp._tcp local
    

    A practical troubleshooting flow (putting it together)

    When a Mac has “network issues,” a repeatable flow saves time:

    1. Link + IP
      • ifconfig en0
      • ipconfig getifaddr en0
    2. Default route
      • route -n get default
    3. DNS
      • scutil --dns
      • dig [example.com](<http://example.com>)
    4. Reachability and path
      • ping -c 5 1.1.1.1
      • traceroute [example.com](<http://example.com>)
    5. Ports and processes
      • nc -vz [example.com](<http://example.com>) 443
      • sudo lsof -nP -iTCP -sTCP:LISTEN
    6. Packet capture (when you must prove what’s happening)
      • sudo tcpdump -i en0 -n ...

    Closing thoughts

    The biggest shift on macOS is not that the Unix tools are missing. It is that the configuration and behavior often live in higher-level macOS systems (network services, DNS resolver stacks, Wi‑Fi frameworks, VPN profiles). Knowing when to reach for scutil and networksetup is what turns “I can’t reproduce it” into actionable information.

    If you only memorize three commands for macOS-specific diagnostics, make them:

    • scutil --dns
    • networksetup -listallnetworkservices
    • airport -I