Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Rena on April 18, 2011, 05:51:58 AM



Title: Detect if Bitcoin is running?
Post by: Rena on April 18, 2011, 05:51:58 AM
What would be the proper/best way for a script to tell if bitcoin[d] is running? I'm writing a Lua script that calls `bitcoin getinfo` and similar commands, and I see it outputs a message to stderr if the server isn't running, but Lua doesn't provide any standard way to read stderr, so I was hoping there's another way to check?


Title: Re: Detect if Bitcoin is running?
Post by: BioMike on April 18, 2011, 05:54:39 AM
# ps|grep bitcoind

That is how I normally do a quick check if services are running.


Title: Re: Detect if Bitcoin is running?
Post by: theymos on April 18, 2011, 06:04:14 AM
Code:
if curl -s 127.0.0.1:8332 >/dev/null; then
echo "running"
else
echo "not running"
fi

Edit: Another way (faster):
Code:
if /sbin/ss -l |grep -q "127.0.0.1:8332"; then
echo "running"
else
echo "not running"
fi


Title: Re: Detect if Bitcoin is running?
Post by: Rena on April 18, 2011, 06:27:46 AM
Hmm, both of those rely on Unix shell commands and redirection. I was hoping for something like a PID file I can check that only exists when the server is running. I can do similar just by appending 2>&1 to the command I call from Lua, but as far as I know there's no guarantee that io.popen() will actually execute the command the way a shell would rather than, for example, just passing everything after the first space as the parameter to the program being called.
I can use sockets as well, but that requires adding LuaSocket, and I was hoping not to depend on any external libraries. Lua doesn't provide much on its own (it focuses on being simple and lightweight) so that may prove difficult to avoid...


Title: Re: Detect if Bitcoin is running?
Post by: theymos on April 18, 2011, 07:01:29 AM
There is a .lock file in the data directory, though I don't know how reliable this is. Maybe it only appears when Bitcoin is doing something to the database.

You could parse /proc/net/tcp.


Title: Re: Detect if Bitcoin is running?
Post by: genjix on April 18, 2011, 11:26:23 AM
Use my branch and do getinfo

https://github.com/genjix/bitcoin/tree/isinitialized


Title: Re: Detect if Bitcoin is running?
Post by: Rena on April 19, 2011, 01:51:38 AM
That's the kind of thing I was looking for. Of course that means anyone using my script also has to use that branch. Will that change be in the official client in some future version?


Title: Re: Detect if Bitcoin is running?
Post by: Gavin Andresen on April 19, 2011, 02:05:01 AM
Hmm, both of those rely on Unix shell commands and redirection. I was hoping for something like a PID file..

I pulled a patch that makes Linux/Mac bitcoin/bitcoind write a bitcoind.pid file when started with the -daemon switch (and erases it on shutdown).

Next release will have several changes to make startup on Linux/Mac more unix-standard-like.