15

Is there any start/stop shell script for geth which is executed after Debian or Ubuntu OS boot up?

For example, startup scripts usually located under

/etc/init.d/ /etc/rc.local

12
+50

I won't get into a system.d vs sysvinit debate, but here's how you could do it with the defaults init manager that Debian / Ubuntu is shipped with.

  • Create a file /etc/systemd/system/geth@.service and fill it with that:

    [Unit]
    Description=geth Ethereum daemon
    Requires=network.target
    
    [Service]
    Type=forking
    User=%I
    ExecStart=/home/USERNAME/go-ethereum/build/bin/geth
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    
  • Change /home/USERNAME/go-ethereum/build/bin/geth to wherever your geth is :)

  • Once you're done with the file, enable the service sudo systemctl enable geth@USERNAME.service

  • Then your user can start it with sudo systemctl start geth@USERNAME.service

Next time you reboot it should start!

For more details see systemd.service man page. There are plenty of options that may be interesting.

  • 1
    Have tried this. Geth is restarted by systemd all the time. I think you meant "Type=Simple" since geth doesn't fork itself – Leon Grapenthin Jan 17 '18 at 11:22
8

Here's what I use on Ubuntu via upstart. I put the following in the file /etc/init/geth.conf

description "geth 1.3.5"

start on runlevel [2345]
stop on shutdown

respawn
respawn limit 10 5

setuid your_nonroot_user

script
  exec bash -c '/usr/bin/geth --rpc --verbosity "2" 2>>/home/geth/logs/geth'
end script

This will start at boot and you can control via sudo start|stop geth

1

1. You can add startup app like this

  1. Find the location of geth executable using which geth
  2. Execute the following commands in order

Assume PATH-TO-GETH is the result of which geth

sudo cp PATH-TO-GETH /etc/init.d/
sudo chmod +x /etc/init.d/geth 
sudo update-rc.d geth defaults

2. Run after reboot with crontab

@reboot /path/to/script

3. Start/Stop Script from here

#!/bin/bash
#
# chkconfig: 35 90 12
# description: Geth Instance 
#
# Get function from functions library
. /etc/init.d/functions
# Start the service GETH
start() {
        initlog -c "echo -n Starting GETH: "
        /path/to/geth &
        ### Create the lock file ###
        touch /var/lock/subsys/geth
        success $"geth startup"
        echo
}
# Restart the service geth
stop() {
        initlog -c "echo -n Stopping GETH: "
        killproc geth
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/geth
        echo
}
### main logic ###
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status geth
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 1
esac
exit 0

How to run scripts on start up?

1

Here is another take for upstart script for Ubuntu 14.04 as a Ansible playbook template and non-root user:

description "geth 1.4.12"

start on runlevel [2345]
stop on shutdown

respawn
respawn limit 10 5

script
  exec sudo -H -i -u {{ ansible_user }} -- bash -c '/usr/bin/geth {{geth_args}} 2>>/home/{{ansible_user}}/logs/geth.log'
end script

Installation - roles/geth/tasks/main.yml:

- name: Install dependencies
  apt: name={{item}} update_cache=yes
  with_items:
  - software-properties-common
  become: yes

- name: Install Ethereum repositories
  apt_repository: repo='ppa:ethereum/ethereum'
  become: yes

- name: Install geth
  apt: name={{item}} update_cache=yes
  with_items:
  - ethereum
  become: yes
  notify:
  - Restart geth

- name: Create logs folder
  file:
    path: "/home/{{ansible_user}}/logs"
    state: directory

- name: Setup startup script
  template:
    src: geth.conf
    dest: /etc/init/geth.conf
  become: yes
  notify:
  - Restart geth
0

Running as systemd:

Create geth.service file (/etc/systemd/system/geth.service):

sudo nano /etc/systemd/system/geth.service

Paste the below

[Unit]
Description=Geth

[Service]
Type=simple
User={$USER}
Restart=always
RestartSec=12
ExecStart=/bin/geth --syncmode "full" --rpc --rpcaddr "0.0.0.0"

[Install]
WantedBy=default.target

Replace {$USER} with username above. Use useradd to create a newUser if required.

sudo useradd -d /home/newUser -m --uid 10000 newUser

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable geth.service
sudo systemctl daemon-reload

service geth status
sudo service geth start

Check the status:

service geth status

Attaching to geth instance:

geth attach

Stop and disable the service:

sudo service geth stop
sudo systemctl disable geth.service
sudo systemctl daemon-reload

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.