0

Background

I created an Alfred workflow to manage a local server script. It's basically a mini HTTP server that performs actions by calling particular URLs. This is what the workflow looks like:

workflow

The script filter comes from a static XML file, just to get a list of actions to choose from:

<?xml version='1.0'?>
<items>
    <item arg='./start_server.sh'>
        <title>Start server</title>
    </item>
    <item arg='/usr/bin/curl -s "http://127.0.0.1:7070/start"'>
        <title>Start downloader</title>
    </item>
    <item arg='/usr/bin/curl -s "http://127.0.0.1:7070/stop"'>
        <title>Stop downloader</title>
    </item>
    <item arg='/usr/bin/curl -s "http://127.0.0.1:7070/shutdown"'>
        <title>Shutdown server</title>
    </item>
</items>

As you can see, the shell commands are inside of the arg attribute, which is passed to the next step in the workflow when selecting an entry.

The Run Script step is as bare as could be: Run Script

No escaping, no additional commands… It just takes the output from the Script Filter and executes it in a shell.

The start_server.sh is a bit special because:

  • The server is a Python script and it depends on a virtualenv to be activated.
  • Because it's a server, the child process has to be disconnected from Alfred, so it keeps running in the background.

Contents of the start_server.sh script:

#!/bin/bash
source $HOME/Development/virtualenv/alfred/bin/activate
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

nohup python traffic_server.py & disown

Issue

Whenever the server (Python script) is started through Alfred, I'm unable to shut it down again through Alfred (the HTTP call through cURL).

What does work (using the same script and commands each time):

  • Start from Alfred, stop through shell.
  • Start from shell, stop through Alfred.
  • And of course starting and stopping it from the shell.

I can tell the server is running when started from Alfred, through the PID file that's created. I can also see that when I try to stop it through Alfred, the PID file is still there and the process remains active.

It seems that whenever I start the server through Alfred, it won't listen to any subsequent calls coming from Alfred.

Debugging is rather limited in Alfred, so any hints / suggestions would be appreciated!

DocZerø
  • 8,037
  • 11
  • 38
  • 66
  • What does the `stop` URL do exactly? How does it stop the script? – Etan Reisner Mar 22 '16 at 12:12
  • the `shutdown` call is handled by the Python script, which shuts down the HTTP server and terminates the script. The `stop` call doesn't terminate the script, it stops a download function. – DocZerø Mar 22 '16 at 12:15
  • Sorry, asked about the wrong URL. How does the `shutdown` URL do that? What does it do exactly? Does it log requests? Do you see the request from Alfred when it doesn't work? Have you added verbose logging to that action to see what happens? – Etan Reisner Mar 22 '16 at 12:20
  • I added basic logging after your last comment. I can see `shutdown` is triggered in all cases in the OP that are marked as working. It's only when the script is started through Alfred, that I don't see any logging appear when calling `shutdown`. The script largely resembles [this one](http://stackoverflow.com/a/36109880/3165737) – DocZerø Mar 22 '16 at 12:36
  • No other calls from Alfred work or just the shutdown call doesn't work? Can you add debugging flags to the `curl` call and see the output somewhere? To see if `curl` is executing and connecting correctly? – Etan Reisner Mar 22 '16 at 13:15
  • @EtanReisner I updated the OP, as I located the cause (but don't yet understand it fully) – DocZerø Mar 22 '16 at 13:21
  • Don't update posts with answers. Post them as answers. – Etan Reisner Mar 22 '16 at 13:29

1 Answers1

0

I finally found what's causing the issue, but can't really explain it fully yet.

The Run Script step was set to run the scripts sequentially. I didn't expect this to be an issue, as the calls to curl were instantaneous, and the actual script was started with nohup and disown. However, it seems that even with those two additional keywords, it still blocks any following commands from executing.

I assume that this is something specifically related to Alfred?

Run Script settings

Community
  • 1
  • 1
DocZerø
  • 8,037
  • 11
  • 38
  • 66