17

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

Satoshi Nakanishi
  • 5,749
  • 7
  • 38
  • 57
  • maybe duplicate: http://ethereum.stackexchange.com/questions/366/how-can-i-run-go-ethereum-as-daemon-process-on-ubuntu – euri10 Mar 22 '16 at 15:11

6 Answers6

14

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.

euri10
  • 4,640
  • 5
  • 24
  • 55
9

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

dbryson
  • 6,403
  • 2
  • 27
  • 37
2

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?

niksmac
  • 9,673
  • 2
  • 40
  • 72
2

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
Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127
1

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
AnuragP
  • 431
  • 3
  • 8
1

OMG, I finally did it...

Create Geth Service File: /etc/init.d/geth

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          geth
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $network $named $time
# Should-Stop:       $network $named $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the geth daemon
# Description:       Controls the geth server daemon
### END INIT INFO
#
. /lib/lsb/init-functions
start() {   
   PCOUNT=`pgrep -c geth`
   if ((PCOUNT==1)); then
      /usr/bin/geth --testnet --syncmode=light --rpc --verbosity 0 --cache=1024 &
   fi
   echo $"Geth Started"
}
stop() {
   PCOUNT=`pgrep -c geth`
   if ((PCOUNT>1)); then   
      /usr/bin/killall -9 geth &
   fi
   echo "Geth Stopped"
}
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

Set Permissions

sudo chmod 755 /etc/init.d/geth
sudo chown root:root /etc/init.d/geth

Enable Geth Service

sudo update-rc.d geth defaults
sudo update-rc.d geth enable
buycanna.io
  • 111
  • 3