Jump to content
drenwick

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..
 
dave

 

Share this post


Link to post

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.

Share this post


Link to post

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

Share this post


Link to post

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

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...