Streamlining TeamViewer Management on macOS: Comprehensive Scripting Tutorial

Key takeaways

  • Automated TeamViewer restart: The script automates the restart process of TeamViewer on macOS, enhancing service reliability.
  • Customizable parameters: Users can customize the number of restart attempts and wait time between each attempt.
  • macOS specificity: Tailored specifically for macOS, leveraging system-specific commands for managing TeamViewer.
  • Enhanced accessibility: Ensures consistent availability of TeamViewer for remote access and support tasks.
  • Script security: Running the script requires administrative privileges, emphasizing the importance of secure usage.
  • Direct approach: Offers a streamlined solution compared to manual checks or third-party tools.
  • Testing and monitoring: Essential to test the script in a controlled environment and continue monitoring TeamViewer’s performance.
  • NinjaOne integration: The script’s functionalities complement NinjaOne’s platform, providing a holistic approach to IT infrastructure management.
  • IT infrastructure efficiency: Significantly boosts efficiency and reliability in managing remote access tools within IT infrastructures.

TeamViewer, a crucial tool in the IT toolkit, offers remote access and support functionalities essential for maintaining and managing IT infrastructures. In environments where remote connectivity is a lifeline, ensuring the reliability and readiness of such tools is non-negotiable. This is where scripting comes into play, providing a robust solution for automating the management of TeamViewer services, particularly on macOS systems.

Background of the TeamViewer restart script

The script in focus is designed for macOS to automate the restart process of the TeamViewer service. This is especially relevant for IT professionals and Managed Service Providers (MSPs) who rely on TeamViewer for remote access to client machines. Ensuring that TeamViewer is always running can be a challenge, and this script addresses that by automating the restart process, reducing downtime and ensuring consistent availability.

The script:

#!/bin/bash

# Description: Restarts the TeamViewer Service. Be sure TeamViewer is set to "Start TeamViewer with System" or that the "TeamViewer Host" app is installed.
#
# Preset Parameter: --attempts 'Replace with the number of attempts you would like to make'
#   The number of attempts you would like to make to bring the TeamViewer service back online.
#
# Preset Parameter: --wait 'Replace with the amount of time in seconds you would like to wait in between attempts'
#   The number of seconds you would like to wait in between attempts
#
# Release Notes: Initial Release
# By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
# Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. 
# Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. 
# Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. 
# Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. 
# Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. 
# Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. 
# EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#
_attempts=3
_waitTimeInSecs=15

# Help text function for when invalid input is encountered
print_help() {
  printf '\n### Below are all the valid parameters for this script. ###\n'
  printf '\nPreset Parameter: --attempts "ReplaceMeWithNumberOfAttempts" \n'
  printf '\t%s\n' "The Number of restart attempts you would like to make."
  printf '\nPreset Parameter: --wait "ReplaceMeWithTheAmountOfSecondsToWaitBetweenAttempts" \n'
  printf '\t%s\n' "The amount of seconds you would like to wait in between attempts."
}

# Determines whether or not help text is nessessary and routes the output to stderr
die() {
  local _ret="${2:-1}"
  echo "$1" >&2
  test "${_PRINT_HELP:-no}" = yes && print_help >&2
  exit "${_ret}"
}

# Grabbing the parameters and parsing through them.
parse_commandline() {
  while test $# -gt 0; do
    _key="$1"
    case "$_key" in
    --help | -h)
      _PRINT_HELP=yes die 0
      ;;
    --attempts | -a)
      _attempts=$2
      shift
      ;;
    --attempts=*)
      _attempts="${_key##--attempts=}"
      ;;
    --wait | -w)
      _waitTimeInSecs=$2
      shift
      ;;
    --wait=*)
      _waitTimeInSecs="${_key##--wait=}"
      ;;
    --*)
      _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
      ;;
    *)
      _PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
      ;;
    esac
    shift
  done
}

parse_commandline "$@"

if [[ -n $attempts ]]; then
  _attempts=$attempts
fi

if [[ -n $waitTimeInSeconds ]]; then
  _waitTimeInSecs=$waitTimeInSeconds
fi

TeamViewerProcess=$(pgrep -lf TeamViewer)
TeamViewerService=$(launchctl list | grep com.teamviewer.service)
TeamViewerPath=$(find /Applications/*TeamViewer*/Contents/Helpers/Restarter)
# Would rather do nothing if I was unable to restart it using the helper service
if [ -s "$TeamViewerPath" ]; then
  echo "TeamViewer found! Proceeding with restart..."
  Attempt=0
  while [[ $Attempt -lt $_attempts ]]; do
    if [ -n "$TeamViewerProcess" ]; then
      echo "TeamViewer is currently running! Killing process..."
      pkill TeamViewer
    fi
    echo "Restarting TeamViewer using restarter in case the process kill didn't work..."
    for Restarter in $TeamViewerPath; do
      $Restarter
    done
    TeamViewerProcess=$(pgrep -lf TeamViewer)
    TeamViewerService=$(launchctl list | grep com.teamviewer.service)
    if [ -z "$TeamViewerService" ]; then
      echo "TeamViewer Service is not running!"
      launchctl load /Library/LaunchDaemons/com.teamviewer.teamviewer_service.plist
    fi
    # Sleeping before checking for success
    sleep "$_waitTimeInSecs"
    Attempt=$(($Attempt + 1))
    echo "Attempt $Attempt complete"
    TeamViewerService=$(launchctl list | grep com.teamviewer.service)
    if [ -n "$TeamViewerProcess" ] && [ -n "$TeamViewerService" ]; then
      echo "TeamViewer Service and Process appears to be ready for connections"
      exit 0
    else
      echo "Restart failed"
    fi
  done
  exit 1
else
  echo "TeamViewer not found!"
  exit 1
fi

 

Access 300+ scripts in the NinjaOne Dojo

Get Access

Detailed breakdown of the script

The script works in several key stages:

  • Parameter parsing and help function: It starts by defining default parameters for the number of restart attempts and the wait time between these attempts. A help text function is included to guide users on how to use the script correctly.
  • Command line argument processing: The script processes command-line arguments, allowing users to customize the number of attempts and wait time.
  • TeamViewer process and service check: It checks for the existence of TeamViewer processes and services on the system. This is crucial to determine the current state of TeamViewer.
  • Restart logic: The script attempts to restart TeamViewer a specified number of times. It kills any running TeamViewer processes and attempts to restart the service using the TeamViewer Restarter utility.
  • Success and failure handling: After each restart attempt, the script checks if the TeamViewer service is running. If successful, the script exits; if all attempts fail, it provides a failure message.

Potential use cases

Imagine an IT professional managing a fleet of Macs across an organization. They could deploy this script to ensure that TeamViewer is always running on these machines, thereby guaranteeing remote access when needed for maintenance or support tasks.

Comparisons

Traditionally, managing TeamViewer services might involve manual checks or third-party monitoring tools. This script offers a more direct and tailored approach, specifically focusing on the TeamViewer service and providing a more lightweight and customizable solution.

FAQs:

  • Q: Can I modify the number of attempts or wait time?
    • A: Yes, the script allows customization through command-line arguments.
  • Q: Will this script work on non-macOS systems?
    • A: No, it is specifically designed for macOS due to its reliance on macOS-specific commands and paths.
  • Q: Is administrative access required to run this script?
    • A: Yes, administrative privileges are typically needed to manage system services like TeamViewer.

Implications

While this script enhances service reliability, IT professionals must ensure it doesn’t compromise security. Automating service restarts could potentially be exploited if not managed correctly, emphasizing the need for secure script deployment and management practices.

Recommendations for best practices

  • Security: Run the script in a secure environment and ensure that only authorized personnel have access.
  • Testing: Test the script in a controlled environment before deploying it widely.
  • Monitoring: Despite automation, regular monitoring of TeamViewer’s performance and security is recommended.

Final thoughts

In the context of such automation and scripting tasks, NinjaOne offers a comprehensive platform for managing IT environments. Its ability to centralize management tasks, offer robust remote monitoring and management, and integrate seamlessly with tools like TeamViewer makes it an invaluable asset for IT professionals aiming to streamline their operations and ensure reliability in their IT infrastructures.

Next Steps

Building an efficient and effective IT team requires a centralized solution that acts as your core service deliver tool. NinjaOne enables IT teams to monitor, manage, secure, and support all their devices, wherever they are, without the need for complex on-premises infrastructure.

Learn more about NinjaOne Remote Script Deployment, check out a live tour, or start your free trial of the NinjaOne platform.

Categories:

You might also like

Watch Demo×
×

See NinjaOne in action!

By submitting this form, I accept NinjaOne's privacy policy.

NinjaOne Terms & Conditions

By clicking the “I Accept” button below, you indicate your acceptance of the following legal terms as well as our Terms of Use:

  • Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
  • Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
  • Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library belonging to or under the control of any other software provider.
  • Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
  • Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
  • Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
  • EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).