sjhcockrell
sjhcockrell

Reputation: 63

Waiting for shell command to execute in a makefile

I've been trying to learn how to use make and hit a roadblock that I'm assuming has more to do with process management than make.

I have the following target

install:
    ...
    @brew install postgres
    @pg_ctl -D data initdb
    @pg_ctl -D data start
    @createuser foo -s

I'm installing postgres, creating a database, starting it, then creating a user.

In this case, createuser continues to fail because the database hasn't started up yet.

I'm assuming what needs to happen is that I need to use wait to hold off on the createuser command until the database has started up, but after an hour of wrangling, I have a feeling I'm not using wait correctly with something like

@pg_ctl -D data start &
@wait $!
@createuser ...

Any idea what I'm missing?

Upvotes: 1

Views: 2420

Answers (1)

mklement0
mklement0

Reputation: 439193

Add the -w option to your pg_ctl ... start command to ensure that the command doesn't return before the database has finished starting up:

@pg_ctl -w -D data start

From the man page (emphasis mine):

-w Wait for the startup or shutdown to complete. Waiting is the default option for shutdowns, but not startups. When waiting for startup, pg_ctl repeatedly attempts to connect to the server. When waiting for shutdown, pg_ctl waits for the server to remove its PID file. pg_ctl returns an exit code based on the success of the startup or shutdown.


As for your solution attempt:

As Oliver Charlesworth points out in a comment on the question, both @pg_ctl -D data start and @pg_ctl -D data start & followed by @wait $! amount to the same: (effectively) synchronous execution of the CLI.

The problem was that while execution of the CLI itself (pg_ctl) was synchronous, the action it triggered was not; adding -w fixes that.

In hindsight, your problem is unrelated to make.

Upvotes: 4

Related Questions