-1

I'm trying to execute a linux command and convert the output to an int. This is my current code:

package main

import (
    "os/exec"
    "os"
    "strconv"
    _"fmt"
    "log"
    "bytes"
)

func main(){

    cmd := exec.Command("ulimit", "-n")
    cmdOutput := &bytes.Buffer{}
    cmd.Stdout = cmdOutput
    err := cmd.Run()
    if err != nil {
      os.Stderr.WriteString(err.Error())
    }

    count, err := strconv.Atoi( string(cmdOutput.Bytes()) )
    if err != nil {
        log.Fatal(err)
    }

    if count <= 1024 {
        log.Fatal("This machine is not good for working!!!")
    }
}

This is my current error:

2018/10/12 14:37:27 exec: "ulimit -n": executable file not found in $PATH

I don't understand what this error means and how I can fix it.

mbuechmann
  • 5,413
  • 5
  • 27
  • 40
  • 1
    You real code probably says `exec.Command("ulimit -n")` (single argument), not `exec.Command("ulimit", "-n")` (multiple arguments). If you don't specify an absolute path, the OS always looks for the program in the directories listed in `$PATH` (and there is no program named "uname -n", hence the error). – Peter Oct 12 '18 at 12:07
  • i changed with single argument, and return me same error exec: "ulimit -n": executable file not found in $PATH2 – Melania Trump Oct 12 '18 at 12:11

1 Answers1

3

There is no ulimit program in linux that you can run.

ulimit is a builtin of a shell. So you need to run a shell and have the shell run its internal ulimit command.

cmd := exec.Command("/bin/sh", "-c" "ulimit -n")

you will also have to remove the newline from the output of the ulimit command, e.g.

count, err := strconv.Atoi( strings.Trim(string(cmdOutput.Bytes()),"\n"))

A better alternative is to retreive these limits via the syscall API in go, see How to set ulimit -n from a golang program? - the accepted answer first gets and prints the current limit.

nos
  • 223,662
  • 58
  • 417
  • 506
  • It's both a builtin and a binary on most systems. Try `type -P ulimit` (/usr/bin/ulimit on my machine). – Peter Oct 12 '18 at 12:16
  • Not on any of my ubuntu,fedora or centos machines – nos Oct 12 '18 at 12:17
  • Interesting. It ships with the [bash-4.4.23-1.fc28.x86_64](https://fedora.pkgs.org/28/fedora-updates-x86_64/bash-4.4.23-1.fc28.x86_64.rpm.html) package for me. – Peter Oct 12 '18 at 12:20
  • @nos it is present on my Fedora 28 machine: `which ulimit` -> `/usr/bin/ulimit` – Adrian Oct 12 '18 at 13:28
  • Yes, I guess fedora or bash have added it somewhat recently - but you are still using the builtin shell command if you run `ulimit` from a shell. However the OP does not have that program, otherwise the go program would have worked. (having ulimit as a program sounds a bit strange though, if you want to alter/set an ulimit, it cannot be run as a separate program , since the ulimit only applies to the current process and its child. This is why it is a builtin to the shell, so the shell can alter its own limits and have it apply to any future programs run from within that shell.) – nos Oct 12 '18 at 13:54