Command-Line MacOS – How to Find the Running TFTP Server Process Name

command linemacos

I can use command sudo launchctl load -F /System/Library/LaunchDaemons/tftp.plist to start TFTP Server on mac. But what is the process name of the running TFTP server?

I tried ps aux | grep tftp and pgrep tftp, neither give me anything…

My goal is to use script to track if tftp server has been turned on OR not…

Best Answer

I wrote a script for that purpose if you care to use it. The usage for TFTP would be as follows.

sudo what-listens.sh -p 69

You might be surprised to find that it shows launchd instead of the actual TFTP process. The service needs to be running to see the actual TFTP process, and launchd is probably managing that service.

#!/bin/bash
if [[ "$EUID" -ne 0 ]]; then
    echo 'This script must be run as root.' 1>&2
    exit 1
fi

CMD_SUDO='/usr/bin/sudo'
CMD_LSOF='/usr/sbin/lsof'
CMD_GREP='/usr/bin/grep'

function port() {
    PORT="$1"
    $CMD_SUDO $CMD_LSOF -n -i4TCP:"$PORT" | $CMD_GREP 'LISTEN'
    if [[ "$?" -eq 1 ]]; then
        echo "There is no program listening on port $PORT."
    fi
}

function usage() {
    echo "Usage: $0 [-p,--port <port> ]"
}

B_NEED_ARG=0
case "$1" in
    -p|--port)
        FUNCTION=port
        B_NEED_ARG=1
        ;;
     *)
        echo "Error: unknown parameter: '$1'."
        FUNCTION=usage
        ;;
esac

if [[ $B_NEED_ARG -eq 1 ]] ; then
    if [[ -z "$2" ]] ; then
        echo "Error: option '$1' requires an argument."
        usage
        exit 1
    else
        if ! [[ "$2" =~ ^[0-9]+$ ]]; then
            echo "Error: argument to '$1' option must be an integer."
            usage
            exit 1
        fi
    fi
fi

${FUNCTION} "$2"

unset CMD_SUDO
unset CMD_LSOF
unset CMD_GREP
unset B_NEED_ARG
unset FUNCTION
unset PORT

I see the question was modified with...

My goal is to use script to track if tftp server has been turned on OR not...

This solution below was working up to Mavericks, 10.9, and probably works up to El Capitan, 10.11.6; but, I have not actually tried it on a Mac with a version higher than 10.9. To disable a service:

sudo defaults write /private/var/db/launchd.db/com.apple.launchd/overrides.plist 'com.apple.tftpd' -dict Disabled -bool true

It can then be checked:

sudo /usr/libexec/PlistBuddy -c 'print com.apple.tftpd:Disabled' /private/var/db/launchd.db/com.apple.launchd/overrides.plist

If the return value is not 'true', then the service is not disabled.