https://github.com/hekmon/processpriority
Easily change a process priority from a Golang program
https://github.com/hekmon/processpriority
linux nice priority process windows
Last synced: about 1 year ago
JSON representation
Easily change a process priority from a Golang program
- Host: GitHub
- URL: https://github.com/hekmon/processpriority
- Owner: hekmon
- License: mit
- Created: 2024-06-17T12:33:05.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-17T14:09:06.000Z (almost 2 years ago)
- Last Synced: 2025-01-11T12:37:45.584Z (about 1 year ago)
- Topics: linux, nice, priority, process, windows
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Process Priority
[](https://pkg.go.dev/github.com/hekmon/processpriority)
Easily get or set a process priority from a Golang program with transparent Linux/Windows support.
Under the hood, it will uses pre-defined nice values for Linux and the priority classes for Windows.
## Notes
### Linux
On Linux, a user can only decrease its priority (increase its nice value), not increase it. This means that even after lowering the priority, you won't be able to raise it to its original value.
To be able to increase it, the user must be root or have the `CAP_SYS_NICE` capability.
### Windows
On Windows, the real time priority can only be set as an administrator. If you try to set it as a normal user, you will won't get an error but your process will be set at `High` instead.
## Example
```golang
package main
import (
"fmt"
"os/exec"
"github.com/hekmon/processpriority"
)
func main() {
// Run command
// cmd := exec.Command("sleep", "1") // linux binary
cmd := exec.Command("./sleep.exe") // windows binary
err := cmd.Start()
if err != nil {
panic(err)
}
// Get its current priority
prio, rawPrio, err := processpriority.Get(cmd.Process.Pid)
if err != nil {
panic(err)
}
fmt.Printf("Current process priority is %s (%d)\n", prio, rawPrio)
// Change its priority
newPriority := processpriority.BelowNormal
fmt.Printf("Changing process priority to %s\n", newPriority)
if err = processpriority.Set(cmd.Process.Pid, newPriority); err != nil {
panic(err)
}
// Verifying
if prio, rawPrio, err = processpriority.Get(cmd.Process.Pid); err != nil {
panic(err)
}
fmt.Printf("Current process priority is %s (%d)\n", prio, rawPrio)
// Wait for the cmd to end
if err = cmd.Wait(); err != nil {
panic(err)
}
}
```
### Linux output
```
Current process priority is Normal (0)
Changing process priority to Below Normal
Current process priority is Below Normal (5)
```
### Windows output
```
Current process priority is Normal (32)
Changing process priority to Below Normal
Current process priority is Below Normal (16384)
```
## Install
```bash
go get -u github.com/hekmon/processpriority
```