Makefile runs processes in the background

I have this in my Makefile:

run: for x in *.bin ; do ./$$x ; done 

so that it runs all executable files one by one. I want to do this:

 run: for x in *.bin ; do ./$$x &; done 

so that it runs every executable file and puts it in the background. I get a syntax error for the above statement when I put an ampersand.

I don’t want to call make as make & , since this will start processes in the background, but still one by one, while I want individual executables to run in the background, so at any time I have several executables files running.

Thanks in advance.

+7
source share
3 answers

Try running through a subshell:

 run: for x in *.bin ; do (./$$x &); done 

Maybe make -j is the best option. Try a Makefile that looks something like this:

 BINS = $(shell echo *.bin) .PHONY: $(BINS) run: $(BINS) *.bin: ./ $@ 

And then do with make -j <jobs> , where <jobs> is the number of simultaneous jobs to run.

+10
source

The syntax error you are getting is a shell syntax error, not a problem with make syntax. Ampersand is actually a command terminator / separator , just like a semicolon; so the way to express the loop of the loop you want is:

 run: for x in *.bin ; do ./$$x & done 

However, as others have noted, where practical, it is usually more flexible to express things directly, how to create dependencies, rather than complex fragments of the shell and shell shell.

+4
source

Try:

 run: for x in *.bin ; do (./$$x &) ; done 

Command () runs the command in a subshell.

+2
source

All Articles