Scripting: How to tell when `ipfs daemon` is ready?

I’m using a private IPFS swarm to push out OS updates to some lab equipment. IPFS is not usually running, so my update script has to launch ipfs daemon and then ipfs pin add ... the data I want to get.

Since ipfs daemon runs in the foreground, I have a challenge finding out when IPFS is ready to accept commands. I’m essentially doing this:

#!/bin/bash
ipfs daemon --enable-gc &  # backgrounded
ipfs pin add $CID

Since the daemon takes some time to start-up, the ipfs pin command fails. Sure, I could add a sleep, but that’s a hack.

Is there a way to make ipfs pin block until it succeeds, or some other means to wait for the daemon to be ready?

I tried the following, but it doesn’t work unless I remove the redirects:

#!/bin/bash
ipfs daemon --enable-gc &  # backgrounded

until ipfs stats bitswap &>/dev/null
do
  sleep 1
done

ipfs pin add $CID

If I remove the &>/dev/null redirect it seems to work, but of course it outputs some lines to the screen. Especially when the daemon is starting up and the lock is held.

Does your system use systemd? If yes, Kubo should notify systemd when it is ready and you can use that to launch your own code.

Otherwise your approach is fine, and I am not sure why it doesn’t work, but you can certainly refactor the loop into something like:

(pseudocode)
until $status == 0
do
  ipfs version &> /dev/null
  status=$?
  sleep 1
done

and see if that helps.

Or you can test for the .ipfs/api file to appear in place and hold requests until then.

Thanks, using ipfs version as the sentinal is neat, and I got it working (some silly bash stuff got in my way).

I didn’t know about .ipfs/api, that’s handy thanks.