[Cialug] Starting two jobs at once

Matthew Nuzum newz at bearfruit.org
Tue Nov 27 21:52:27 CST 2012


On Nov 27, 2012, at 9:23 PM, Zachary Kotlarek <zach at kotlarek.com> wrote:
> Mostly, with two exceptions. First, these PID variables are local to the current shell, so you don't want to launch subshells/backticks/etc. In your example it will work because $$ will be evaluated before `echo` is launched, but it's much safer and more efficient to use:
> 	p1pid=$$
> 
> Second, for this usage you mean $! not $$, as you want the PID of the background'ed process, not of the shell itself:
> 	p1pid=$!
> or since you're only launching a single process in the background, you can simply end with:
> 	kill $!
> as $! is not affected by the foreground process launched in the middle.
> 
> If you want to be able to handle abnormal exits/SIGINT/SIGTERM/etc. you'll need to be sure shell execution continues as you expect when the foreground process exits (at least be sure you aren't running with `set -e`) and/or install a handler for the situations. Some quick testing is probably the easiest way to determine if this is a concern for your usage.

Well, for the original use-case I've tried this and it works perfectly!

run:
    $(MONGO) --dbpath=. &
    $(MANAGE) runserver 0.0.0.0:8000
    kill $! && fg

I found that doing kill $! caused it to happen asynchronously which was weird, so adding && fg caused everything to wait until mongo actually ended.

However, I think that foreman may be a better long-term solution, especially as things get a bit more complex with multiple background jobs. This article that Eric sent fits my use case perfectly: http://bruno.im/2011/sep/29/streamline-your-django-workflow/ (and as an added benefit is much easier to read than the foreman man page/website)

Next step will be for me to decouple my settings.py file from mongo so that I can run the tests without mongod in the background. That will be a topic for another discussion though. :-)

Thanks for the help!

-- 
Matthew Nuzum
newz2000 on freenode, skype, linkedin and twitter

♫ You're never fully dressed without a smile! ♫


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4136 bytes
Desc: not available
URL: <http://cialug.org/pipermail/cialug/attachments/20121127/707eb270/attachment-0001.bin>


More information about the Cialug mailing list