5

I want to make a script to restart my server and it's prerequisites, which one of them is geth. I don't see a command line to restart it. I want something like

geth --restart --xxxx --yyyyy

Does this exist?

In caso no, is it safe to just kill the process and run it again?

5

This is what I use in my Linux environment. I save the following in /home/user/bin/runGeth

#!/bin/sh

# Graceful exit, like pressing Control-C on a program
killall -q --signal SIGINT geth
sleep 10

# Hard kill, only to stop a process that refuses to terminate
killall -q geth

# Clear IPC as this can sometimes cause problems
rm -f /home/user/.ethereum/geth.ipc

DATE=`date +%Y%m%d_%H%M%S`
mv /home/user/ethlogs/geth.log /home/user/logarchive/geth.log_$DATE

# Message <= 32 bytes
MESSAGE="BokkyPooBah wuz here!"

# Use 6 for full details
VERBOSITY=3

geth --support-dao-fork --rpc --rpcaddr "192.168.7.123" --rpcport 8545 --extradata "$MESSAGE" --verbosity $VERBOSITY 2>> /home/user/ethlogs/geth.log &

Then chmod 700 /home/user/bin/runGeth.

And in /etc/rc.local, I add:

sudo -u user /home/user/bin/runGeth

I also restart geth periodically as I have had several instances where it locks up or dies. I add the following to crontab -e

# m h  dom mon dow   command
10 1,7,13,19 * * * sudo -u user /home/user/bin/runGeth 


Q: In case no, is it safe to just kill the process and run it again?

The graceful exit for geth is necessary. I leave some time for geth to gracefully shut down. I then try a hard kill as geth has in the past refused to gracefully exit.

I used only the hard kill before and the blockchain data got corrupted.


I also use similar scripts to send me periodic emails that report on the coinbase balance, ethminer hashrate, GPU temperature, ETH prices and other statistics. And a watchdog script to shutdown ethminer processes if the GPUs reach a specified temperature.

  • 1
    I've added killall -s SIGKILL -q geth just in case. – Pablo Yabo Jul 25 '16 at 18:24
1

I use a couple of scripts for managing my geth instances

For starting geth (here i provide as parameter the verbosity but you can provide more if you want)

verbosity=3
while [ "$1" != "" ]; do
    case $1 in
    -v | --verbosity )  shift
                        verbosity=$1
                        ;;
    esac
    shift
done
if test -z `ps -ef | grep 'SCREEN -dmS geth' | grep -v grep | awk '{print $2}'`
then
echo "Starting Geth..."
screen -dmS geth /usr/bin/geth --networkid "189" --identity "firstMiner" --nodiscover --rpc --rpcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3" --rpcaddr "0.0.0.0" --rpcport "8545" --rpccorsdomain "*"  --maxpeers "25" --mine --etherbase "3a4ac889777bce619ad88eef2ad7b9cace99273e" --gasprice "0" --targetgaslimit "9999999" --verbosity "$verbosity"
echo "Geth started with verbosity $verbosity"
else
echo "Geth is alreay running"
fi

For stopping geth

if test -z `ps -ef | grep 'SCREEN -dmS geth' | grep -v grep | awk '{print $2}'`
then
echo "Geth is already Stopped"
else
echo "Stopping Geth..."
ps -ef | grep 'SCREEN -dmS geth' | grep -v grep | awk '{print $2}' | xargs kill
echo "Geth stopped"
fi

For checking the status of geth

if test -z `ps -ef | grep 'SCREEN -dmS geth' | grep -v grep | awk '{print $2}'`
then
echo "Geth is stopped"
else
echo "Geth is running"
fi

For attaching to geth console (I provide a relative path to the ipc here but you can put an absolute path)

if test -z `ps -ef | grep 'SCREEN -dmS geth' | grep -v grep | awk '{print $2}'`
then
echo "Geth is not running"
else
geth attach ipc:../.ethereum/geth.ipc
fi

To restart you could put the geth stop and geth start scripts together in another script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.