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:
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:
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!