How to get the path of the executable file for current process?

1,281 views
Skip to first unread message

axe

unread,
Dec 20, 2011, 6:00:56 AM12/20/11
to golang-nuts
Hi All,

I'm a newbie of golang :) learning.......

Currently I got a problem: how to ge the path of the executable file
for the current process. E.G.:
I wrote a native linux executable program with go, put it under /
home/axe/bin/testgo. /home/axe/bin is in my $PATH.
Then I run it under /home/axe/Desktop/ and want it to print out a
message: I come from /home/axe/bin/.
The message string is got in runtime, not hardcoded in source code :)

Jan Mercl

unread,
Dec 20, 2011, 9:57:38 AM12/20/11
to golan...@googlegroups.com
On Tuesday, December 20, 2011 12:00:56 PM UTC+1, axe wrote:

Currently I got a problem: how to ge the path of the executable file
for the current process. E.G.:
I wrote a native linux executable program with go, put it under    /
home/axe/bin/testgo. /home/axe/bin is in my $PATH.
Then I run it under /home/axe/Desktop/ and want it to print out a
message: I come from /home/axe/bin/.

I think filepath.Abs(os.Args[0]) should provide that info.

John Asmuth

unread,
Dec 20, 2011, 10:03:35 AM12/20/11
to golan...@googlegroups.com


On Tuesday, December 20, 2011 9:57:38 AM UTC-5, Jan Mercl wrote:
I think filepath.Abs(os.Args[0]) should provide that info.

This will not work if the command was launched not relative to the current directory (ie, it was found in $PATH).

I think exec.LookPath() might work for things found in the $PATH *and* things run from the current directory (ie "./myProgram" -> "CWD/myProgram").

Jan Mercl

unread,
Dec 20, 2011, 10:14:13 AM12/20/11
to golan...@googlegroups.com
On Tuesday, December 20, 2011 4:03:35 PM UTC+1, John Asmuth wrote:
This will not work if the command was launched not relative to the current directory (ie, it was found in $PATH).

Confirmed, sorry.

Paul Borman

unread,
Dec 20, 2011, 11:27:36 AM12/20/11
to axe, golang-nuts
In traditional UNIX you could not determine your location.  Further, arg0 is settable to any value by the exec system call (and was even used as a flag to programs in some cases).

In linux you can look at /proc/$PID/exe.  This is a symlink back to the executable.

I don't know of a portable way to do this, however.

    -Paul

Paul Borman

unread,
Dec 20, 2011, 1:09:56 PM12/20/11
to Michael Jones, axe, golang-nuts
That at least was a sane use of argv[0].  The login program sets arg0 to be -sh (or -ksh or -csh or ...) to indicate it is a login shell.  If you were directly execed by login the way to find out where you came from is to look at the environment variable SHELL (but there is no way for you to know if you were really called by login or not).

Using argv[0] is generally good enough for most situations, but it can be fooled and is certainly not secure!

    execl("mySpy", "-yourProgram", NULL)

On Tue, Dec 20, 2011 at 9:45 AM, Michael Jones <m...@google.com> wrote:
On the Sun-2 and Sun-3, ex/vi looked at argv[0] to decide if it should be ex or vi.


On Tue, Dec 20, 2011 at 8:27 AM, Paul Borman <bor...@google.com> wrote:
and was even used as a flag to programs in some cases



--
Michael T. Jones | Chief Technology Advocate  | m...@google.com |  +1 650-335-5765


Michael Jones

unread,
Dec 20, 2011, 12:45:12 PM12/20/11
to Paul Borman, axe, golang-nuts

Rob 'Commander' Pike

unread,
Dec 20, 2011, 1:37:29 PM12/20/11
to Michael Jones, Paul Borman, axe, golang-nuts

On Dec 20, 2011, at 9:45 AM, Michael Jones wrote:

> On the Sun-2 and Sun-3, ex/vi looked at argv[0] to decide if it should be ex or vi.

And System V used it to decide whether to be cp, ln, or mv. I opined at the time that there could be just one huge a.out for all the Unix commands. They didn't bite.

-rob


Brad Fitzpatrick

unread,
Dec 20, 2011, 1:39:38 PM12/20/11
to Rob 'Commander' Pike, Michael Jones, Paul Borman, axe, golang-nuts

Paul Borman

unread,
Dec 20, 2011, 2:12:12 PM12/20/11
to Rob 'Commander' Pike, Michael Jones, axe, golang-nuts
Perhaps you could have suggested that all the commands could be linked into the kernel.  It worked for VxWorks for many years!

On Tue, Dec 20, 2011 at 10:37 AM, Rob 'Commander' Pike <r...@google.com> wrote:

斧头哥哥

unread,
Dec 21, 2011, 12:44:28 AM12/21/11
to golang-nuts
Nice! It works!

Thank you a lot!
Reply all
Reply to author
Forward
0 new messages