Check if it was run as root

Golang Geteuid Examples, os.Geteuid Golang Examples - HotExamples

For checking the operation of batches run by root. Equivalent to whoami command

whoami.go


package main

import (
        "fmt"
        "os"
)

func main() {
        if os.Geteuid() != 0 {
                fmt.Println("Must be run as root")
                os.Exit(1)
        }
        os.Exit(0)
}
//General user execution
$ go run whoami.go
Must be run as root
exit status 1
//root execution
# go run whoami.go
# echo $?
0

Recommended Posts

Check if it was run as root
If it was Ruby
Even if I uploaded HTML with boto3, it was not recognized as HTML
Create Python folder Check if it already exists
Check if it is Unix in the scripting language
I wondered if Python 3.4 was faster, but it was slower
Check if it is Unix in the scripting language
What to do if you can't log in as root