3

I have a shell command thats runs endlessly, until you stop it using Control-C. When I run it from the Terminal, it goes something like:

$ /abspath/to/my-command
Command is running ...
Use Control-C to stop it.

I wanted to be able to launch this command easily from the dock, so I created an Automator application, with just a Run Shell Script component, where I wrote /abspath/to/my-command. Then, I saved "MyApp.app" in my Applications folder and drag & drop it to the dock.

So far, it works great, when I click the app on the dock, the command starts running. There's just one huge problem: I cannot quit the application! In fact, in the dock it doesn't even appear that the application is running. The only solution I have found so far is to do ps aux | grep my-command and then kill -9 <PID>.

How can I quit "MyApp.app" (like doing Control-C in the Terminal)?

2 Answers 2

1

Your app does start the process and then quits itself straight away leaving the process running.

You could try and check in the Automator action to see if the process is running. if it is quit it, if it is not launch it.

 isRunning=`ps aux | grep -i "Textedit.app"| grep -v grep`

    if [ $isRunning -eq "" ]; then 
        echo "is Not Running" 

/abspath/to/my-command
    else
        echo "is Running"  

# terminate code  here
    fi;

So when you click the App in the Dock. to will either start the process or stop it.

1
1

Opening an Automator application starts a process named Application Stub, but killall Application\ Stub doesn't terminate its child processes.

$ sleep 1 && open ~/Desktop/Untitled.app/ & sudo execsnoop
[1] 650
  UID    PID   PPID ARGS
  501    655    650 open
  501    656    143 Application Stub
  501    657    143 Automator Launch
  501    658    656 bash
  501    658    656 say

pkill -P $(pgrep Application\ Stub) and killall say made Automator show a dialog that the workflow encountered an error.

If you used an AppleScript application instead, it could be terminated from the Dock or with killall applet.

Or if you don't want to keep a shell window open, use disown or launchctl submit:

say {1..99} & disown $! # stop with killall say
launchctl submit -l my.say say {1..99} # stop with launchctl remove my.say

You must log in to answer this question.

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