Bitcoin Forum
November 10, 2024, 11:43:50 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Detect if Bitcoin is running?  (Read 2113 times)
Rena (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 0



View Profile WWW
April 18, 2011, 05:51:58 AM
 #1

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?
BioMike
Legendary
*
Offline Offline

Activity: 1658
Merit: 1001


View Profile
April 18, 2011, 05:54:39 AM
 #2

# ps|grep bitcoind

That is how I normally do a quick check if services are running.
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5376
Merit: 13407


View Profile
April 18, 2011, 06:04:14 AM
Last edit: April 18, 2011, 06:15:27 AM by theymos
 #3

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

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
Rena (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 0



View Profile WWW
April 18, 2011, 06:27:46 AM
 #4

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...
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5376
Merit: 13407


View Profile
April 18, 2011, 07:01:29 AM
 #5

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.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
genjix
Legendary
*
expert
Offline Offline

Activity: 1232
Merit: 1076


View Profile
April 18, 2011, 11:26:23 AM
 #6

Use my branch and do getinfo

https://github.com/genjix/bitcoin/tree/isinitialized
Rena (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 0



View Profile WWW
April 19, 2011, 01:51:38 AM
 #7

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?
Gavin Andresen
Legendary
*
qt
Offline Offline

Activity: 1652
Merit: 2301


Chief Scientist


View Profile WWW
April 19, 2011, 02:05:01 AM
 #8

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.

How often do you get the chance to work on a potentially world-changing project?
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!