Scripting help

Forum rules
Before you post please read how to get help
luka1002
Level 1
Level 1
Posts: 8
Joined: Sun Jun 18, 2017 3:19 pm

Scripting help

Postby luka1002 » Sat Jul 08, 2017 7:59 am

Have this file "getstat":

Code: Select all

{"method":"getstat", "error":null, "start_time":1499374659, "current_server":"zec-eu.suprnova.cc:2142", "available_servers":1, "server_status":2, "result":[{"gpuid":0, "cudaid":0, "busid":"0000:01:00.0", "name":"GeForce GTX 1070", "gpu_status":2, "solver":0, "temperature":71, "gpu_power_usage":128, "speed_sps":438, "accepted_shares":150, "rejected_shares":0, "start_time":1499374659},{"gpuid":1, "cudaid":1, "busid":"0000:02:00.0", "name":"GeForce GTX 1070", "gpu_status":2, "solver":0, "temperature":58, "gpu_power_usage":129, "speed_sps":440, "accepted_shares":136, "rejected_shares":0, "start_time":1499374659}]}


1. Need script (getstat.sh) that will do:

Code: Select all

if "server_status":value <> 2 ; goto script00.sh
if "temperature1": value  > 85 ; goto script01.sh
if "temperature2": value  > 85 ; goto script02.sh
if "gpu_status1": value <> 2 ; goto script03.sh
if "gpu_status2": value <> 2 ; goto script04.sh
rear "error": value <> "null" goto  script05.sh else goto exit


question beside how to do it in linux is how script will recognize same values for different VGAs? Maybe to use "cudaid" and first value after that.

2. Also one script that look every hour for proccess ./miner . if dectected goto getstat.sh if not reboot now.


Thx
Luka

User avatar
xenopeek
Level 24
Level 24
Posts: 21468
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Scripting help

Postby xenopeek » Sat Jul 08, 2017 8:27 am

The file getstat looks to be a JSON format file? It has some invalid control code characters when I copy it from here. Does it have that also in your file? After cleaning up the file (made it a single line of text) I could use jq to query it. jq is a tool to query JSON files from the Linux shell. So start by installing jq with command: apt install jq

To test if a file is in good format you can run: jq . filename
If the file is good that will pretty print it's content. For your example file it does this for me (and it has color as well):

Code: Select all

{
  "method": "getstat",
  "error": null,
  "start_time": 1499374659,
  "current_server": "zec-eu.suprnova.cc:2142",
  "available_servers": 1,
  "server_status": 2,
  "result": [
    {
      "gpuid": 0,
      "cudaid": 0,
      "busid": "0000:01:00.0",
      "name": "GeForce GTX 1070",
      "gpu_status": 2,
      "solver": 0,
      "temperature": 71,
      "gpu_power_usage": 128,
      "speed_sps": 438,
      "accepted_shares": 150,
      "rejected_shares": 0,
      "start_time": 1499374659
    },
    {
      "gpuid": 1,
      "cudaid": 1,
      "busid": "0000:02:00.0",
      "name": "GeForce GTX 1070",
      "gpu_status": 2,
      "solver": 0,
      "temperature": 58,
      "gpu_power_usage": 129,
      "speed_sps": 440,
      "accepted_shares": 136,
      "rejected_shares": 0,
      "start_time": 1499374659
    }
  ]
}

An example script that should work with your example file is below. I don't have any error check in here (does the file exists, did the jq command return any errors, does the script to invoke exist, and so on; exercises for the reader :wink:) I assume the "goto scriptNN.sh" commands means to continue execution in that other script and not return to this script? If you want something else to happen, please describe in words.

Code: Select all

#!/usr/bin/env bash

readonly STATFILE='getstat'

if [[ $(jq -Mr '.server_status' ${STATFILE}) -ne 2 ]]; then
   exec script00.sh
elif [[ $(jq -Mr '.result[0].temperature' ${STATFILE}) -gt 85 ]]; then
   exec script01.sh
elif [[ $(jq -Mr '.result[1].temperature' ${STATFILE}) -gt 85 ]]; then
   exec script02.sh
elif [[ $(jq -Mr '.result[0].gpu_status' ${STATFILE}) -ne 2 ]]; then
   exec script03.sh
elif [[ $(jq -Mr '.result[1].gpu_status' ${STATFILE}) -ne 2 ]]; then
   exec script04.sh
elif [[ $(jq -Mr '.error' ${STATFILE}) != 'null' ]]; then
   exit
fi
Last edited by xenopeek on Wed Jul 19, 2017 1:04 am, edited 1 time in total.
Reason: removed semicolon after readonly STATFILE='getstat'
Image

luka1002
Level 1
Level 1
Posts: 8
Joined: Sun Jun 18, 2017 3:19 pm

Re: Scripting help

Postby luka1002 » Mon Jul 10, 2017 9:01 am

Thank you!

Yeah, I copied text from linux to Windows so this is why there are some invalid code. I knew it was JSON but did not know anything about it. Thx. In the mean wile i found help with JSON about this miner (he generates getstat file), but i do not uderestand it very well. Thx for your help it is clear how to do it.

API Description.
Api establish the tcp connection and send through it json data, like a stratum protocol.
Every json request and response must be ended with new line symbol \n, this symbol used
as a sign of data end.

Code: Select all

Request example: {"id":1, "method":"getstat"}\n
Respond example: {"id":1, "method":"getstat", "error":null, "result":[{
      "gpuid":0,
      "cudaid":0,
      "busid":"0000:01:00.0",
      "gpu_status":2,
      "solver":0,
      "temperature":64,
      "gpu_power_usage":150,
      "speed_sps":420,
      "accepted_shares":1000,
      "rejected_shares":1
   },{
      "gpuid":1,
      "cudaid":1,
      "busid":"0000:04:00.0",
      "gpu_status":2,
      "solver":0,
      "temperature":70,
      "gpu_power_usage":100,
      "speed_sps":410,
      "accepted_shares":1111,
      "rejected_shares":2
   }
]}\n

Method getstat:

Code: Select all

  Request parameters:
    id       optional,  type: unsigned int,  response will be have this id.
    method   required,  type: string,        method name
                     
  Response parameters:
    id                          type: unsigned,          id from request or 0 in other case.
    method                      type: string,            method name
    error                       type: null, string,      null if no error otherwise message
    result                      type: array of objects,  array contain objects by one for each gpu
    result[0].gpuid             type: unsigned,          internal gpu identifier
    result[0].cudaid            type: unsigned,          gpu cuda identifier
    result[0].busid             type: string,            pci bus id in format: 0000:00:00.0
    result[0].gpu_status        type: unsigned,          status of gpu
                                                         0 - gpu launched, but not yet work,
                                                         1 - gpu launched, but he prepare to work, for example execute benchmark.
                                                         2 - gpu works
                                                         3 - GPU stopped, for example, a temperature limit is reached
    result[0].solver            type: unsigned,          selected solver
    result[0].temperature       type: integer,           gpu core temperature
    result[0].gpu_power_usage   type: unsigned,          gpu power usage in watts, not all devices support this in this case this value will be 0
    result[0].speed_sps         type: unsigned,          gpu performance in solutions per seconds
    result[0].accepted_shares   type: unsigned,          amount of accepted shares
    result[0].rejected_shares   type: unsigned,          amount of rejected shares
    Remark: temperature, gpu_power_usage and speed_sps updated every 30 seconds.

User avatar
xenopeek
Level 24
Level 24
Posts: 21468
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Scripting help

Postby xenopeek » Mon Jul 10, 2017 11:52 am

\n is an escape character, a way to represent a non-printable character. It means a single newline character. That's fine and should work perfectly with jq and my example script.
Image

luka1002
Level 1
Level 1
Posts: 8
Joined: Sun Jun 18, 2017 3:19 pm

Re: Scripting help

Postby luka1002 » Mon Jul 10, 2017 3:39 pm

Thank you for your help. 8)

luka1002
Level 1
Level 1
Posts: 8
Joined: Sun Jun 18, 2017 3:19 pm

Re: Scripting help

Postby luka1002 » Tue Jul 18, 2017 4:09 pm

I had busy few weeks, so now i tried this what you wrote (script). I get errors. My sh: (it is just test, i will configure other scripts later)

Code: Select all


#!/usr/bin/env bash

readonly STATFILE='getstat';

if [[ $(jq -Mr '.server_status' ${STATFILE}) -ne 2 ]]; then#!/usr/bin/env bash

readonly STATFILE='getstat';

if [[ $(jq -Mr '.server_status' ${STATFILE}) -ne 2 ]]; then
   echo "server ok"
elif [[ $(jq -Mr '.result[0].temperature' ${STATFILE}) -gt 85 ]]; then
   echo "temp ok"
elif [[ $(jq -Mr '.result[1].temperature' ${STATFILE}) -gt 85 ]]; then
   echo "temp ok"
elif [[ $(jq -Mr '.result[0].gpu_status' ${STATFILE}) -ne 2 ]]; then
   echo "gpu 0 ok"
elif [[ $(jq -Mr '.result[1].gpu_status' ${STATFILE}) -ne 2 ]]; then
   echo "gpu1 ok"
elif [[ $(jq -Mr '.error' ${STATFILE}) != 'null' ]]; then
   exit
fi


run :

Code: Select all

luka@Mmin /home $ sudo sh teyt.sh
[sudo] password for luka:
teyt.sh: 5: teyt.sh: [[: not found
teyt.sh: 7: teyt.sh: [[: not found
teyt.sh: 9: teyt.sh: [[: not found
teyt.sh: 11: teyt.sh: [[: not found
teyt.sh: 13: teyt.sh: [[: not found
teyt.sh: 15: teyt.sh: [[: not found


Variable "STATFILE"(getstat) is here so in syntax is problem, but i do not know where.

Using linux Mint 18.1:

Code: Select all

luka@Mmin /home $ jq --version
jq-1.5-1-a5b5cbe

User avatar
xenopeek
Level 24
Level 24
Posts: 21468
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Scripting help

Postby xenopeek » Wed Jul 19, 2017 1:04 am

luka1002 wrote:

Code: Select all

sudo sh teyt.sh

It's a bash file. Calling it with sh will break the script. Just mark the file as executable (chmod +x filename) and you can run instead (the ./ prefix is to indicate the teyt.sh file is found in the current directory; when running programs not in your PATH directories you must indicate where they are found):

Code: Select all

sudo ./teyt.sh


But are you sure you need root privileges to run your script?

And I hope the heading of your file is copy & paste error as it shows the first three lines duplicated? This does not look correct:
luka1002 wrote:

Code: Select all

#!/usr/bin/env bash

readonly STATFILE='getstat';

if [[ $(jq -Mr '.server_status' ${STATFILE}) -ne 2 ]]; then#!/usr/bin/env bash

readonly STATFILE='getstat';

if [[ $(jq -Mr '.server_status' ${STATFILE}) -ne 2 ]]; then

The first line in your file must be #!/usr/bin/env bash. No whitespace before it.

Also, drop the semicolon after readonly STATFILE='getstat';. My mistake, it should not be there.
Image

luka1002
Level 1
Level 1
Posts: 8
Joined: Sun Jun 18, 2017 3:19 pm

Re: Scripting help

Postby luka1002 » Thu Jul 20, 2017 10:23 am

Thank you it worked.

I use nano as text editor, and his first line is always empty and unwritable. Rest is ok. Thank you one more time.

User avatar
Termy
Level 3
Level 3
Posts: 170
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Scripting help

Postby Termy » Sat Sep 09, 2017 3:42 pm

Just so you (or anyone reading this) know, those errors came about because the [[ expression ]] testing syntax isn't in sh.

You needn't make a script executable to run it. Note that chmod -x <file> makes it executable for everyone, which is in general a potential security risk, although I doubt it's a concern with this particular script of course xD, still, it's worth keeping in mind. To make it so only you have execution privileges on a file, use chmod u+x <file> instead. You could run it with bash <script> without changing its permissions. You actually don't even need the shebang in a shell script unless you're going to ask the system to determine from the shebang which shell to use.

Lastly, #!/usr/bin/env bash isn't required (if you do use a shebang) as #!/bin/bash (or wherever bash is on your OS) is a more direct route. I consider the former method less secure, since I believe it picks up whichever "bash" is in your PATH. The caveat with the latter, is a slight drop in portability, so if a script is to support many different setups, then I'd assume the env route is probably the best one.

I know that was probably unnecessary as the thread is getting on a bit, but it gave me something to do. lol
Here to help.

User avatar
xenopeek
Level 24
Level 24
Posts: 21468
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Scripting help

Postby xenopeek » Sat Sep 09, 2017 4:32 pm

Termy wrote:Just so you (or anyone reading this) know, those errors came about because the [[ expression ]] testing syntax isn't in sh.
\
Well, yes and no. The script was fine and the shebang line indicated it was a bash script. The mistake was using "sudo sh filename.sh" to invoke the script. That ignores the shebang line and indeed forces a script to be run by a different script interpreter. Yeah, that doesn't work very well :) Also, sudo is not need for parsing JSON files...
Image

User avatar
Termy
Level 3
Level 3
Posts: 170
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Scripting help

Postby Termy » Sat Sep 09, 2017 5:13 pm

xenopeek wrote:
Termy wrote:Just so you (or anyone reading this) know, those errors came about because the [[ expression ]] testing syntax isn't in sh.
\
Well, yes and no. The script was fine and the shebang line indicated it was a bash script. The mistake was using "sudo sh filename.sh" to invoke the script. That ignores the shebang line and indeed forces a script to be run by a different script interpreter. Yeah, that doesn't work very well :) Also, sudo is not need for parsing JSON files...


Sorry, I forgot to quote. I was referring specifically to the [[: not found errors. Since the script was called with sh, as you pointed out, it resulted in those errors, because... [ insert my previous post :P ]
Here to help.

User avatar
xenopeek
Level 24
Level 24
Posts: 21468
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Scripting help

Postby xenopeek » Sun Sep 10, 2017 3:16 am

Perhaps we're meaning the same thing but when I read your "those errors came about because the [[ expression ]] testing syntax isn't in sh" I took that to mean "you shouldn't have used the [[ expression ]]" instead of "you shouldn't run bash scripts with sh, duh" :wink:
Image


Return to “Scripts & Bash”