2018/02/12

Start xmr-stak as service in ubuntu

Note to self on how to get xmr-stak working on startup on ubuntu 17.10.

prerequisite: build xmr-stack-cpu

create service folder and copy files from bin folder to it
sudo mkdir /usr/local/xmr
sudo cp * /usr/local/xmr/.

cd /usr/local/xmr/


create start script in /usr/local/xmr

sudo nano server.sh

#!/bin/bash

PID=""

function get_pid {
   PID=`pidof xmr-stak-cpu`
}

function stop {
   get_pid
   if [ -z $PID ]; then
      echo "server is not running."
      exit 1
   else
      echo -n "Stopping server.."
      kill -9 $PID
      sleep 1
      echo ".. Done."
   fi
}


function start {
   get_pid
   if [ -z $PID ]; then
      echo  "Starting server.."
      sysctl -w vm.nr_hugepages=128
      ./xmr-stak-cpu &

      get_pid
      echo "Done. PID=$PID"
   else
      echo "server is already running, PID=$PID"
   fi
}

function restart {
   echo  "Restarting server.."
   get_pid
   if [ -z $PID ]; then
      start
   else
      stop
      sleep 5
      start
   fi
}


function status {
   get_pid
   if [ -z  $PID ]; then
      echo "Server is not running."
      exit 1
   else
      echo "Server is running, PID=$PID"
   fi
}

case "$1" in
   start)
      start
   ;;
   stop)
      stop
   ;;
   restart)
      restart
   ;;
   status)
      status
   ;;
   *)
      echo "Usage: $0 {start|stop|restart|status}"
esac


set permission and make executable
sudo chmod 744 start.sh

prepare service
sudo nano /etc/systemd/system/xmr-stak.service

[Unit]
After=network.target

[Service]
WorkingDirectory=/usr/local/xmr/
Type=forking
ExecStart=/bin/bash server.sh start
Restart=always
User=root

[Install]
WantedBy=multi-user.target


set premission for service
sudo chmod 644 /etc/systemd/system/xmr-stak.service

reload systemctl
sudo systemctl daemon-reload
sudo systemctl enable xmr-stak.service