PI-Miner

Guide for automated xmrig on boot with tmux

This is a walkthrough on how I set up xmrig to start automatically on boot in the background using tmux. This is not the only way to do this and may not even be the easiest way. I am by no means a Linux expert. I believe this will work on any Debian based Linux including raspberry pi but I have only tried it on Mint.

This guide works under the following assumptions:

Your Linux username is Derrick

if you have not install and configured xmrig(How To), Now is the time todo it.

Rename user to user like below Derrick

xmrig is located at /home/Derrick/xmrig/build

First off, the biggest benefit of this setup is the ability to run your miner headless. To access the computer remotely you will want the machine to have an ssh server. You might also want to configure a static IP address on your network so you can find your miner easily.

sudo apt update
sudo apt install ssh

You also need to install tmux which stands for terminal multiplexer. This tool will allow you to attach to the terminal window where xmrig is running, interact, and detach all without interrupting the mining script.

sudo apt install tmux

Now install sensors. this will enable you to keep a eye on the pi`s temputures

sudo apt-get install lm-sensors

Next, create a shell script that will start a window in tmux running xmrig.

sudo nano autominer.sh

You can name this file whatever you want, but it has to end in .sh. Write the following lines into the file using the text editor.

# !/bin/bash
tmux new-session -d -s miner -n xmrig
tmux send-keys -t xmrig 'cd xmrig/build/
'
tmux send-keys -t xmrig 'sudo ./xmrig
'

Save the file with CTRL+xy, then enter. The first line creates a new session in tmux, the -d option, or detach, makes it run in the background. -s miner names the session “miner” and -n xmrig names the window “xmrig”. After creating and naming the new session and window the following lines simply send character strings to be executed in the target window, specified by -t xmrig. The content of these strings should look familiar, you are just going to the /build directory and starting xmrig.

Now you need to make the script executable with this command:

sudo chmod +x autominer.sh

Now you can test the script to make sure it works by entering:

./autominer.sh

Nothing happened. Or did it? Remember you used -d to have the window run in the background. Is tmux doing anything? You can check with the command tmux list-sessions. If you see miner: 1 windows (...) then you have successfully started a new session with one window. But how do you get to it? Use the command tmux attach. You should then see your commands from the script, cd xmrig/build/ and sudo ./xmirg executed on the command line. But the miner isn’t running, it is waiting on your root password. This won’t be very useful if you have to come in and type the password to start the miner. You need to allow ./xmrig to run as root without a password. To do this you can edit the sudoers file.

sudo visudo

Scroll to the bottom of the page and add a line like this: Remember to add your own user like i have Derrick

user ALL=(ALL) NOPASSWD:/home/Derrick/xmrig/build/xmrig

Save and exit. Remember the assumptions in this guide about your username and path to xmrig. If you edit this file and after saving you get a warning about a syntax error type e to go back in the editor and check again or x to exit without saving. Do not type Q to force save or you could cause big problems.

After making this change the script should run xmrig automatically. Enter the command ./autominer.sh followed by tmux attach. You should see xmrig running. To “detach” or send the window with xmrig back to the background type CRTL+b followed by d. You can start and stop xmrig from within that window or type any other commands the same way you normally would. You can stop tmux from the outside with the command tmux kill-server or conversely while attached to the tmux window you can use CTRL+b followed by & to kill the window.

The last step is to set your script to run at boot. For this I used crontab. Use the command crontab -e and edit the file with nano. Add @reboot /home/Derrick/autominer.sh to the bottom of the file, save and exit. The script will now run at boot. You can now run the machine headless, ssh into the machine, use tmux attach to attach to xmrig, and CTRL+b followed by d to detach from xmrig as it runs continuously.

As a side note you don’t necessarily have to name the session and window in tmux but it helps to keep things organized if you were to create additional tmux windows that are running other processes, such as a GPU miner. To illustrate you can add a couple lines to your script to create a second window that monitors CPU temperatures. If tmux is running kill it with tmux kill-server. Then edit your script with sudo nano autominer.sh and add the following lines to the script:

tmux new-window -n cpu \;
tmux send-keys -t cpu 'watch -n 2 sensors
'

Save and exit. Now when you run the script and attach to tmux you can type CTRL+b followed by n to toggle between the two windows. tmux can do much more too, if you want to learn more about how to use it and customize your setup you can read here:

Link

Edit- Something is going on with the markdown I don’t know how to fix, but there should not be a space between # and !/bin/bash at the start of the script.

Think about auto update

Tmux Cheat sheet

Share link: https://13ear.uk/u76k