linux-dash icon indicating copy to clipboard operation
linux-dash copied to clipboard

linux_json_api.sh no such file or directory with go

Open cyrialize opened this issue 1 year ago • 1 comments

Hello!

I've been running into issues with the go server, and the other servers when attempting to start linux-dash from my home directory.

For example:

jonny@raspberrypi:~ $ sudo ./linux-dash/app/server/index -listen="0.0.0.0:853" -static="/home/jonny/linux-dash/app/"
Starting http server at: 0.0.0.0:853
Error executing 'current_ram': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
Error executing 'ram_intensive_processes': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
Error executing 'cpu_intensive_processes': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
Error executing 'disk_partitions': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
Error executing 'docker_processes': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
Error executing 'swap': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
Error executing 'load_avg': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
Error executing 'cpu_utilization': fork/exec ./linux_json_api.sh: no such file or directory
	Script output:
^C

I fixed this by restoring the -scripts flag:

package main

import (
    "bytes"
    "flag"
    "fmt"
    "net/http"
    "os"
    "os/exec"
)

var (
    listenAddress = flag.String("listen", "0.0.0.0:80", "Where the server listens for connections. [interface]:port")
    staticPath    = flag.String("static", "../", "Location of static files.")
    scriptPath    = flag.String("scripts", "./linux_json_api.sh", "Location of shell scripts used to gather stats.")
)

func init() {
    flag.Parse()
}

func main() {
    http.Handle("/", http.FileServer(http.Dir(*staticPath)))
    http.HandleFunc("/server/", func(w http.ResponseWriter, r *http.Request) {
        module := r.URL.Query().Get("module")
        if module == "" {
            http.Error(w, "No module specified, or requested module doesn't exist.", 406)
            return
        }

        // Execute the command
        cmd := exec.Command(*scriptPath, module)
        var output bytes.Buffer
        cmd.Stdout = &output
        err := cmd.Run()
        if err != nil {
            fmt.Printf("Error executing '%s': %s\n\tScript output: %s\n", module, err.Error(), output.String())
            http.Error(w, "Unable to execute module.", http.StatusInternalServerError)
            return
        }

        w.Write(output.Bytes())
    })

    fmt.Println("Starting http server at:", *listenAddress)
    err := http.ListenAndServe(*listenAddress, nil)
    if err != nil {
        fmt.Println("Error starting http server:", err)
        os.Exit(1)
    }
}

And here's the result:

jonny@raspberrypi:~ $ sudo ./linux-dash/app/server/index -listen="0.0.0.0:853" -static="/home/jonny/linux-dash/app/" -scripts="/home/jonny/linux-dash/app/server/linux_json_api.sh"
Starting http server at: 0.0.0.0:853

Some questions:

  1. Is there a better way I should be calling linux-dash? I'd like to use the command shown above in cron.
  2. What are the best ways to get this change committed? I have a local branch that I could push. I could also fork the codebase.

Let me know if you have any questions!

Thanks.

cyrialize avatar Dec 21 '23 02:12 cyrialize

This command also works as well, for anyone else that comes across this. (This command will also launch it in the background).

cd /home/jonny/linux-dash/app/server && nohup /usr/bin/go run index.go -listen="0.0.0.0:853" -static="/home/jonny/linux-dash/app/" &

cyrialize avatar Dec 26 '23 15:12 cyrialize