Can I create an Automator application to start Tomcat when I need?

To actually answer the questions your asked:

  • Can I create an Automator application to start and stop Tomcat when I need?

    Yes

  • What is the correct way to do this, or do I need AppleScript or stuff?

  • And is it possible to do both start and stop in one Automator app?

  • What is the correct way to create an Automator application, or do I need to use AppleScript?

Since there is usually more then one way to accomplish something, lets refrain from using "...the correct way to..." and show you some choices.

If you simply want to be able to toggle the state of Apache Tomcat, i.e, start or stop it, then the following command line can be used in a Automator Application workflow using a Run Shell Script action:

  • In Automator, create a new Application workflow.
  • Add a Run Shell Script action.
    • Settings: Shell: /bin/bash and Pass input: to stdin

Replace the default code with the following line of code:

[[ $(/bin/ps aux | /usr/bin/grep [t]omcat) == "" ]] && /usr/local/bin/catalina start || /usr/local/bin/catalina stop
  • Save the Automator Application workflow.

This command checks to see if Apache Tomcat is running and if not, then starts it, and if yes, then stops it. It simply toggles the state of Apache Tomcat. It's a no frills solution.


A more robust solution would be to use a Run AppleScript action instead.

  • In Automator, create a new Application workflow.
  • Add a Run AppleScript action.

Use the following example AppleScript code in place of the default code:

try
    set isRunning to (do shell script "/bin/ps aux | /usr/bin/grep [t]omcat")
on error number 1
    set isRunning to missing value
end try

if isRunning contains missing value then
    display dialog "                Click the Start button to start Apache Tomcat." buttons {"Cancel", "Start"} ¬
        default button 2 with title "Start Apache Tomcat"
    do shell script "/usr/local/bin/catalina start"
else
    display dialog "                Click the Stop button to stop Apache Tomcat." buttons {"Cancel", "Stop"} ¬
        default button 2 with title "Stop Apache Tomcat"
    do shell script "/usr/local/bin/catalina stop"
end if
  • Save the Automator Application workflow.

This also could be used in an AppleScript Application, created in Script Editor, by itself with no need for Automator. In either case when run you'll be presented with one or the other dialog boxes.

Start Apache Tomcat Stop Apache Tomcat

This gives you the opportunity to see whether or not it's running and cancel if you want instead.


Note: With the exception of using a try statement and an on error number 1 statement there is no other error handling presented in the example AppleScript code. For additional error handling as may be appropriate/needed/wanted, the onus is upon the user to add appropriate additional error handling for any example code presented.

Also, the example code presented herein was tested under a clean install of macOS 10.12.6 with Command Line Tools (macOS Sierra version 10.12) for Xcode-9.1, Homebrew, JDK 9.0.1, and apache-tomcat-8.5.23.tar.gz installed. I see no reason why this would not work in macOS 10.13 as well.


When you run a shell command or script in Automator it is passed your system's default PATH, not the one in your bash profile. This is why bash cannot find the catalina command.

You should always specify the full path to the command in shell scripts. If it was installed via HomeBrew this is probably /usr/local/bin/catalina but you can confirm this by running which catalina in a terminal session.