Extract the uptime information from proc file system in linux using Go

package main

import (
“bufio”
“fmt”
“log”
“os”
“strings”
)

func main() {
file, err := os.Open("/proc/uptime")
if err != nil {
log.Fatal(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
	text := scanner.Text()
	
		fmt.Println(text)
	
}

if err := scanner.Err(); err != nil {
	log.Fatal(err)
}

}
It gives me both value but
I only need first numerical value of uptime which is updated in every 5 sec

Try strings.Split:

parts := strings.Split(text, " ")

what can i do to update in in every 5 sec
And i only need first value

Try this. It uses strings.Split to split the line on spaces and time.Sleep to sleep for five time.Seconds.

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strings"
	"time"
)

func main() {
	for {
		file, err := os.Open("/proc/uptime")
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			text := scanner.Text()
			parts := strings.Split(text, " ")
			fmt.Println(parts[0])
		}

		if err := scanner.Err(); err != nil {
			log.Fatal(err)
		}

		time.Sleep(5 * time.Second)
	}

}

Note that uptime has the property that it increases one second per second, exactly. You can read it once and then interpolate based on the current time.

1 Like

Thank you so much for your help

How can i interpolate the uptime information once read based upon the current time ?

Try applying the time package to your problem, you could parse the string representation of the uptime into a time and then use the packages functionality to do what you want like this: https://play.golang.org/p/Q4W7NJ8SHm9

1 Like

What can i do to change that string to int ?
I have applied atoi but it shows error as
cannot use parts (type []string) as type string in argument to strconv.Atoi

for scanner.Scan() {
text := scanner.Text()
parts := strings.Split(text, " ")
i, err := strconv.Atoi(parts)
if err != nil {
// handle error
fmt.Println(err)
os.Exit(2)
}

		}

Sorry that I have to say this, but if you are not able to act accordingly on this error message, and you do not know how to make a []string into a string, you really should take the go tour again.

Also, this forum uses markdown to format its code. So you have 3 ways to create a block of code here:

  1. Select the text you want to mark as code in the edit-box and then click the </>-button in the toolbar above the edit-box
  2. Indent each line of the code block by 4 spaces (this is what the button actually does)
  3. Wrap your code in a “fence”. Fences supported from this markdown processor are either at least 3 backticks (```) or at least 3 tilde (~~~). Put them in front and at the end of your codeblock. Eg:
```go
package main
```
1 Like

Hi @Bidhan
You would have to range over slice to convert each value in slice to convert to int.
Atoi would work but when you range.
( []string is slice multiple of strings no?)
:slight_smile:

Yes it is a slice of strings. Thank you for your help :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.