我正在寻找一种可靠的方法来找出我输入命令时运行的程序在文件系统中的位置。
ps -ef给我所有当前正在运行的进程的列表(-e意味着每个;-f意味着包含相关参数的完整列表)。
所以,为了找到uname程序的位置,我试过
grep `uname -n &` `ps -ef`
Run Code Online (Sandbox Code Playgroud)
我正在尝试捕获uname -n的进程 ID (PID) 并将其转发到grep,它应该返回包含该 PID 的行,以便我可以看到当前正在运行哪个命令。
我知道有像tee、管道这样的命令......但我还没有成功地将它们结合起来以实现我的目标。
我想有一些 shell 函数返回被调用函数的标准输出,比如:
stdout(some__command)
Run Code Online (Sandbox Code Playgroud)
所以我可以打电话:
grep stdout(uname -n &) stdout(ps -ef).
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
如果您想知道顶级命令存储在哪个目录中,您有多种选择:
$ which uname
/bin/uname
$ type -a uname
uname is /bin/uname
$ locate uname
/bin/uname
(... SNIP dozens of Windows files on C & D ...)
/usr/lib/klibc/bin/uname
/usr/lib/plainbox-provider-resource-generic/bin/uname_resource
/usr/share/man/man1/uname.1.gz
/usr/share/man/man2/oldolduname.2.gz
/usr/share/man/man2/olduname.2.gz
/usr/share/man/man2/uname.2.gz
Run Code Online (Sandbox Code Playgroud)
最后一个选项locate返回所有文件,uname不仅包含从命令提示符运行的程序。
您不需要pid调用 of 命令来查找函数调用的各种命令的名称。随着strace所有命令的名字被直接显示。
对于您的uname -n示例,输出为:
$ strace uname -n
execve("/bin/uname", ["uname", "-n"], [/* 62 vars */]) = 0
brk(NULL) = 0x2356000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=109073, ...}) = 0
mmap(NULL, 109073, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff2f9a9f000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\t\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1868984, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff2f9a9e000
mmap(NULL, 3971488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7ff2f94cb000
mprotect(0x7ff2f968b000, 2097152, PROT_NONE) = 0
mmap(0x7ff2f988b000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7ff2f988b000
mmap(0x7ff2f9891000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7ff2f9891000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff2f9a9d000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ff2f9a9c000
arch_prctl(ARCH_SET_FS, 0x7ff2f9a9d700) = 0
mprotect(0x7ff2f988b000, 16384, PROT_READ) = 0
mprotect(0x606000, 4096, PROT_READ) = 0
mprotect(0x7ff2f9aba000, 4096, PROT_READ) = 0
munmap(0x7ff2f9a9f000, 109073) = 0
brk(NULL) = 0x2356000
brk(0x2377000) = 0x2377000
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=10219008, ...}) = 0
mmap(NULL, 10219008, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff2f8b0c000
close(3) = 0
uname({sysname="Linux", nodename="alien", ...}) = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
write(1, "alien\n", 6alien
) = 6
close(1) = 0
close(2) = 0
exit_group(0) = ?
+++ exited with 0 +++
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅man strace:
STRACE(1) General Commands Manual STRACE(1)
NAME
strace - trace system calls and signals
SYNOPSIS
strace [-CdffhikqrtttTvVxxy] [-In] [-bexecve] [-eexpr]... [-acolumn] [-ofile] [-sstr?
size] [-Ppath]... -ppid... / [-D] [-Evar[=val]]... [-uusername] command [args]
strace -c[df] [-In] [-bexecve] [-eexpr]... [-Ooverhead] [-Ssortby] -ppid... / [-D]
[-Evar[=val]]... [-uusername] command [args]
DESCRIPTION
In the simplest case strace runs the specified command until it exits. It intercepts
and records the system calls which are called by a process and the signals which are
received by a process. The name of each system call, its arguments and its return value
are printed on standard error or to the file specified with the -o option.
strace is a useful diagnostic, instructional, and debugging tool. System administra?
tors, diagnosticians and trouble-shooters will find it invaluable for solving problems
with programs for which the source is not readily available since they do not need to be
recompiled in order to trace them. Students, hackers and the overly-curious will find
that a great deal can be learned about a system and its system calls by tracing even
ordinary programs. And programmers will find that since system calls and signals are
events that happen at the user/kernel interface, a close examination of this boundary is
very useful for bug isolation, sanity checking and attempting to capture race condi?
tions.
Run Code Online (Sandbox Code Playgroud)
以上只是联机帮助页的开头 strace