Error while executing exec.command

655 views
Skip to first unread message

Sharan Guhan

unread,
Mar 15, 2021, 6:37:51 PM3/15/21
to golang-nuts
Hi Experts,

I am relatively new to GOLANG and trying a simple program as below which is failing in fetching the output of a file, which works otherwise on the command prompt:

Lang : GOLANG
OS : Linux/Centos
Problem : Using exec.command to get the output of a certain file
Error: Getting no such file or directory error:

        command = fmt.Sprintf("cat /sys/bus/pci/devices/%s.0/numa_node", name)

2021/03/15 11:55:59 fork/exec cat /sys/bus/pci/devices/0000:19:00.0/numa_node: no such file or directory
exit status 1

However, the same executed on linux prompt works from the same working directory as the code :
[root-v05 ~]# cat /sys/bus/pci/devices/0000:19:00.0/numa_node
0
[root-v05 ~]# 


Please suggest what I may be missing.

Sharan



Amit Saha

unread,
Mar 15, 2021, 6:40:59 PM3/15/21
to Sharan Guhan, golang-nuts
The exec.Command() expects the arguments to be passed separately. The problem you are having here is that it is thinking your entire command along with the arguments is assumed to be the command to execute.

I would have a look at the documentation for exec.Command



Sharan



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c5f6d10b-e857-4652-ac94-0e200233db9dn%40googlegroups.com.

Axel Wagner

unread,
Mar 15, 2021, 6:43:39 PM3/15/21
to golang-nuts
I would also suggest to just use os.ReadFile, instead of executing `cat`. The latter is pointless overhead and unnecessarily fragile.

Sharan Guhan

unread,
Mar 16, 2021, 2:02:09 AM3/16/21
to Axel Wagner, golang-nuts
Thanks a bunch !! 'ioutils.ReadFIle' did the trick :)

Sharan

Reply all
Reply to author
Forward
0 new messages