Jump to content

Starting a background process?

Recommended Posts

Hi there.. 

I am developing a workflow to start a length process and have some trouble now getting the scripts to return. 


So I have a Run Script action and In the middle of the script it runs an external shell script... something like this..


echo "starting cobble" >> $log

result=`./cobble.sh $function ${runtimeVariables}`

echo "cobble: returned already" >> $log
in the cobble.sh script I have some code like this;
echo "$scriptName: START" >> $log
heavyliftCLI & >> $log 2>&1
echo "$scriptName: END" >> $log
So what happens is that this in the logs;
> starting cobble
> cobble: START
> heavyliftCLI .... blah blah blah
> blah blah blah
> etc
> cobble: END
I never get the expected file log entry (i.e. "cobble: returned already")
What I expected to happen was that the & at the end of the heavyliftCLI would background the task and then we'd return all the way back. 
Instead - it spawns the process and then exit's the script (I see "cobble: END")
... then nothing ...
If I go open the terminal and 'killall heavyliftCLI' I will immediately see the "cobble: returned already" entry in the log.
Sort of suggests that Alfred is awaiting for all the spawned processes to finish...
Can anyone help me work out what is happening and any possible workarounds?
Thanks in advance..


Link to comment

The problem is your command is the wrong way round.


Use cmd 2>&1 >> $log & not cmd & 2>&1 >> $log  (with the "backgrounding" ampersand at the end after the redirection).


If the ampersand isn't at the end, bash won't background the process, which is why your script isn't exiting immediately.

Edited by deanishe
Link to comment

The method Alfred uses to run scripts won't return while any sub-processes are still running. If you want something to run in the background you'll have to run it as a separate server that isn't triggered by an Alfred script.


That's not really true. It's perfectly possible to run a background process from within an Alfred script. You just have to be sure that any subprocesses you want to background also disconnect from the parent STDOUT and STDERR, as Alfred won't consider a subprocess finished while one of them is still open.

Edited by deanishe
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...