Pause and Resume XMRig with Python
If you’re reading this, you probably already know that XMRig is used to mine Monero. You may have noticed that it comes with a simple built-in API, which (of course) you can control with Python.
I found that I wanted to mine Monero overnight, but pause it during the day (the CPU hits 100% during mining, making the computer nearly unusable). The problem is that I’d forget to resume mining at the end of the day.
So, I found a way to get Python to check if it is paused, and if it is, resume mining. Then it’s a simple matter to schedule the script to run each night.
Enable the API
The first step is to enable the API. This is done by editing the config.json file that XMRig created for you. You need to make sure the api and http settings look like this:
"api": { "id": null, "worker-id": null }, "http": { "enabled": true, "host": "192.168.200.11", "port": 80, "access-token": "1234", "restricted": false },
Here’s a quick summary of what these settings do:
- enabled – Enables the API
- host – The IP address of your computer
- port – The port (above we’re using HTTP on port 80)
- access-token – The token for authorization (think of it like a password); Change this to something you’re happy with
- restricted – Change to false to allow remote access to the API
API Summary
In the examples below, set these two headers. Remember to change the token (1234 in the example) to match what you set in config.json.
‘Content-Type’ | ‘application/json’ |
‘Authorization’ | ‘Bearer 1234’ |
Is this Paused?
To check if XMRig is paused, send a GET to http://<<your-ip>>/2/summary
XMRig will respond with a summary of the current settings. Save this in Python as a dictionary, and look at the paused key. This will be True or False.
Resume Mining
To resume mining, first set a payload with this:
{ "method": "resume", "id": 1 }
If you want to, you could set the method to pause instead of resume.
Make sure your headers are set, and send a POST to http://<<your-ip>>/json_rpc
The Full Script
import requests import json pause_url = "http://192.168.200.11/json_rpc" status_url = "http://192.168.200.11/2/summary" pause_payload = json.dumps({ "method": "resume", "id": 1 }) stat_payload = {} headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer 1234' } status = requests.request("GET", status_url, headers=headers, data=stat_payload) raw = status.json() # Dict type if (raw["paused"]): print ("paused, resuming...") pause = requests.request("POST", pause_url, headers=headers, data=pause_payload)