如何将命令行参数传递给 unix/linux 系统上的正在运行的进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the command line args passed to a running process on unix/linux systems?
提问by Hemant
On SunOS there is pargs
command that prints the command line arguments passed to the running process.
在 SunOS 上,有一个pargs
命令可以打印传递给正在运行的进程的命令行参数。
Is there is any similar command on other Unix environments?
在其他 Unix 环境中是否有类似的命令?
采纳答案by markus_b
There are several options:
有几种选择:
ps -fp <pid>
cat /proc/<pid>/cmdline | sed -e "s/\x00/ /g"; echo
There is more info in /proc/<pid>
on Linux, just have a look.
/proc/<pid>
Linux 中有更多信息,请查看。
On other Unixes things might be different. The ps
command will work everywhere, the /proc
stuff is OS specific. For example on AIX there is no cmdline
in /proc
.
在其他 Unix 上,事情可能会有所不同。该ps
命令可以在任何地方使用,这些/proc
东西是特定于操作系统的。例如在 AIX 上没有cmdline
in /proc
.
回答by lothar
On Linux
在Linux 上
cat /proc/<pid>/cmdline
get's you the commandline of the process (including args) but with all whitespaces changed to NUL characters.
获取进程的命令行(包括 args),但所有空格都更改为 NUL 字符。
回答by LOGAN
Full commandline
完整的命令行
For Linux & Unix System you can use ps -ef | grep process_name
to get the full command line.
对于 Linux 和 Unix 系统,您可以使用ps -ef | grep process_name
获取完整的命令行。
On SunOS systems, if you want to get full command line, you can use
在 SunOS 系统上,如果您想获得完整的命令行,可以使用
/usr/ucb/ps -auxww | grep -i process_name
To get the full command line you need to become super user.
要获得完整的命令行,您需要成为超级用户。
List of arguments
参数列表
pargs -a PROCESS_ID
will give a detailed list of arguments passed to a process. It will output the array of arguments in like this:
将给出传递给进程的参数的详细列表。它将像这样输出参数数组:
argv[o]: first argument
argv[1]: second..
argv[*]: and so on..
I didn't find any similar command for Linux, but I would use the following command to get similar output:
我没有找到任何适用于 Linux 的类似命令,但我会使用以下命令来获得类似的输出:
tr 'xargs -0 < /proc/<pid>/cmdline
' '\n' < /proc/<pid>/environ
回答by Michael B?ckling
This will do the trick:
这将解决问题:
cat -v /proc/PID/cmdline | sed 's/\^@/\ /g' && echo
Without the xargs, there will be no spaces between the arguments, because they have been converted to NULs.
如果没有 xargs,参数之间将没有空格,因为它们已转换为 NUL。
回答by Diego
Another variant of printing /proc/PID/cmdline
with spaces in Linux is:
/proc/PID/cmdline
Linux 中使用空格打印的另一种变体是:
pgrep -l -f PatternOfProcess
In this way cat
prints NULL charactersas ^@
and then you replace them with a space using sed
; echo
prints a newline.
通过这种方式cat
打印NULL字符的^@
,然后你用一个空格替换它们sed
; echo
打印换行符。
回答by Sopalajo de Arrierez
You can use pgrep
with -f
(full command line) and -l
(long description):
你可以用pgrep
与-f
(完整命令行)和-l
(长说明):
tr ' ps -eo pid,comm
' ' ' </proc/<pid>/cmdline
This method has a crucial difference with any of the other responses: it works on CygWin, so you can use it to obtain the full command line of any process running under Windows (execute as elevatedif you want data about any elevated/admin process). Any other method for doing this on Windows is more awkward ( for example).
Furthermore: in my tests, the pgrep way has been the only system that workedto obtain the full path for scripts running inside CygWin's python.
此方法与任何其他响应都有一个关键的区别:它适用于CygWin,因此您可以使用它来获取在 Windows 下运行的任何进程的完整命令行(如果您想要有关任何提升/管理进程的数据,则以提升的方式执行) . 在 Windows 上执行此操作的任何其他方法都比较笨拙(例如)。
此外:在我的测试中, pgrep 方式一直是唯一能够获取在 CygWin 的 python 中运行的脚本的完整路径的系统。
回答by Tom Evans
Rather than using multiple commands to edit the stream, just use one - tr translates one character to another:
而不是使用多个命令来编辑流,只需使用一个 - tr 将一个字符转换为另一个:
##代码##回答by Timothy J. Biggs
In addition to all the above ways to convert the text, if you simply use 'strings', it will make the output on separate lines by default. With the added benefit that it may also prevent any chars that may scramble your terminal from appearing.
除了上述所有转换文本的方法外,如果您只是使用“字符串”,则默认情况下会在单独的行上进行输出。额外的好处是它还可以防止出现任何可能扰乱您的终端的字符。
Both output in one command:
一个命令中的两个输出:
strings /proc//cmdline /proc//environ
字符串 /proc//cmdline /proc//environ
The real question is... is there a way to see the real command line of a process in Linux that has been altered so that the cmdline contains the altered text instead of the actual command that was run.
真正的问题是……有没有办法在 Linux 中查看进程的真实命令行,该命令行已被更改,以便 cmdline 包含更改后的文本而不是运行的实际命令。
回答by repzero
try ps -n
in a linux terminal. This will show:
ps -n
在 linux 终端中尝试 。这将显示:
1.All processes RUNNING, their command line and their PIDs
1.所有进程RUNNING,它们的命令行和它们的PID
- The program intiate the processes.
- 程序启动进程。
Afterwards you will know which process to kill
之后你就会知道要杀死哪个进程
回答by HuntM
On Solaris
在 Solaris 上
##代码##similar can be used on unix like systems.
类似的可以在类 Unix 系统上使用。