30

The Installation Instructions for Ubuntu suggest running geth from the command line, how can I run it as daemon process / service on Ubuntu?

30

Run as a systemd service

Create a file geth.service:

[Unit]
Description=Ethereum go client

[Service]
Type=simple
ExecStart=geth 2>%h/.ethereum/geth.log

[Install]
WantedBy=default.target

Enable service:

systemctl --user enable geth.service
systemctl --user start geth.service

Source.

Alternatively you could use screen:

sudo apt-get update && sudo apt-get install screen -y

Then you can make a bash similar to this (~/geth.sh):

#!/usr/bin/env bash
echo "Starting geth"
screen -dmS geth /usr/bin/geth --verbosity 3

now let's make it executable:

sudo chmod +x ~/geth.sh

You can now run the bash ~/geth.sh

You attach to the screen with screen -x geth

You detach from the screen by pressing CTRL + a then d

If you want to attach to the geth console after the process runs in the background (or in screen), you can use:

geth attach

Source.

Or simply fork it in background:

When starting geht, put a & at the end of the command in a terminal:

geth --rpc &

Before closing the terminal you should disown the process:

disown

You could also pipe the logs to a file like that:

geth --verbosity 4 --rpc 2>> /path/to/logfile

But don't forget to disown it before you close the terminal.

Source.

  • 2
    Great answer. The "systemd" should probably be the first example though - at least it's what I would have expected to find as the top answer. – hcvst Jan 22 '16 at 11:26
  • You have my vote, but agree that the systemd section is probably the most relevant portion of the answer. – Taylor Gerring Jan 22 '16 at 12:01
  • updated the answer – Waqar Lim Jan 22 '16 at 12:27
  • 3
    Systemd method: Failed to enable unit: File geth.service: No such file or directory. I'm saving the file in my home folder and I'm executing the command there. – e18r Jun 8 '17 at 17:53
  • 4
    @emisilva I had a similar problem, which I solved by changing: 1) changing the enable command to sudo & making the path an absolute path: sudo systemctl --user enable /home/ubuntu/geth.service 2) also had to change the command to an absolute path in the service: ExecStart=/usr/bin/geth ... – carlolm Sep 29 '17 at 1:16
13

Just using this command is enough for me:

nohup geth --rpc &

To check if service is running:

ps ax | grep geth

To explore last log messages:

tail -f nohup.out

To stop the service I use:

pkill geth
  • 1
    To get a console, geth attach – e18r Jun 8 '17 at 18:01
  • 1
    killall geth seems like a better option to stop the geth-process. – alper Dec 18 '17 at 19:35
  • that's exactly what i need! – Richard Fu Sep 7 '18 at 0:39
2

Here is an update to 5chdn's answer that uses systemd. This shows how to run a headless geth on your Ubuntu server.

Of course, you must install the command line tools first, see https://www.ethereum.org/cli. You should also run MyEtherWallet on your normal desktop computer to create an account + private key so you can receive funds.

Step 1: put on your cape

sudo bash

Other steps here are based on you running as root. If you are not running as root then learn about using systemd with user processes. tl;dr in this case they would only run after you login, but you can configure so a user service starts as boot

Step 2: make a service

cat > /root/geth.service <<EOF
[Unit]
Description=Ethereum go client

[Service]
Type=simple
ExecStart=/usr/bin/geth --etherbase 0xe677698ab732Aa1E56CF2A7997a00e3FA6F5bD88 2>%h/.ethereum/geth.log

[Install]
WantedBy=default.target
EOF

Above, you can substitute your own wallet's etherbase. If you don't specify an etherbase then geth will emit a warning.

Step 3: run it

systemctl --user enable /root/geth.service
systemctl --user start geth.service

This will run it now and after you reboot.

Step 4: test it

sleep 5 # The geth server takes a few seconds to boot up
geth attach

If geth attach is successful, you will know the geth server is running, and you'll see:

Welcome to the Geth JavaScript console!

If something messed up then you'll see:

Fatal: Unable to attach to remote geth: dial unix /root/.ethereum/geth.ipc: connect: connection refused

0

Though the accepted answer is great, it didn't work for me. Most likely because I am on AWS Linux AMI (eugh, I know). I intend on switching at some point (systemctl not So far, this seems to have worked for me:

  1. Create file /etc/init.d/geth-testnet

#!/bin/bash
#chkconfig: 2345 20 80
#description Start RPC testnet

su ec2-user -c '/path/to/geth --verbosity 3 --testnet --rpc --rpcaddr="0.0.0.0" --rpccorsdomain="*" --rpcapi="db,eth,net,web3,personal"'

  1. Run
chmod +x geth-testnet
chkconfig --add geth-testnet
chkconfig --level 345 geth-testnet on
  • 1
    I may be wrong but is this part of the above command: '--rpc --rpcaddr="0.0.0.0" --rpccorsdomain="*" ' opening geth ports to possible attack? I seem to remember some posting five or six months ago about someone losing ether when running geth with ports open to the whole world as this is saying. Might be wrong though. I'm sure someone will correct me if I am. – Thomas Jay Rush Sep 16 '16 at 14:43
  • for safety, maybe anyone who finds this remove that part until someone confirms or denies what @ThomasJayRush said – babycakes Nov 23 '16 at 6:33

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.