4

I can start Apache Tomcat (installed via HomeBrew) in Terminal using

$ catalina run

But it does not work in an Automator application Shell script using bash

I tried to set up a bash Shell script in Automator like this:

catalina run

When I run the Automator application, it won't work and shows -: catalina: command not found.

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

2

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.

  • Thank you so much!!! The codes above works in 10.13! One thing I noticed is that, the dialog box does not actually take the buttons' values. (The start/stop script below the dialog box line will run whether I pressed the start*/*stop button or cancel) So I used a system notification instead: display notification "Tomcat has started/stopped." with title "Tomcat". – jackxujh Dec 1 '17 at 8:45
  • And I loved that AppleScript is still working in 10.13, while being easy and intuitive, it also is capable of sending out notifications or popping out dialog boxes! – jackxujh Dec 1 '17 at 8:47
  • @jackxujh, To address what you said in your first comment... You do realize that there are two separate approaches here and both are not in the same Automator app? 1. Automator app with a Run Shell Script action and a single line of code that checks the state and toggles accordingly. 2. Automator app with a Run AppleScript action that uses the example AppleScript code. Each approach uses one of the methods but not both in the same app. Continued in next comment... – user3439894 Dec 1 '17 at 17:05
  • @jackxujh, If AppleScript in macOS 10.13 is functioning properly, then I find it hard to believe my example AppleScript code doesn't work as stated. Sans trapping error "User canceled." number -128 when it occurs, there is no other code in the example AppleScript code that should get processed. As coded, an untapped error "User canceled." number -128 should stop anything else from occurring in that example AppleScript code. I tested every possible combination while simultaneously checking in Terminal and it works for me as stated. Later I'll build out a macOS 10.13.1 system and test it. – user3439894 Dec 1 '17 at 17:05
  • Yes, I understand that, within each run, or execution of the Automator app, it can only toggle Tomcat status once. But I found that during each, run, whether I click cancel or start/stop, it will still toggle Tomcat. This works as expected, right? – jackxujh Dec 1 '17 at 17:11
3

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.

  • I checked the location using which, and it was at the location you mentioned. – jackxujh Nov 29 '17 at 23:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .