l0b*_*0b0 7 ps process-management
ps -o command
在单独的行上显示每个命令,用空格分隔,不带引号的参数:
$ ps -o command
COMMAND
bash
ps -o command
Run Code Online (Sandbox Code Playgroud)
在检查引用是否正确或复制并粘贴命令以再次运行它时,这可能是一个问题。例如:
$ xss-lock --notifier="notify-send -- 'foo bar'" slock &
[1] 20172
$ ps -o command | grep [x]ss-lock
xss-lock --notifier=notify-send -- 'foo bar' slock
Run Code Online (Sandbox Code Playgroud)
的输出具有ps
误导性 - 如果您尝试复制和粘贴它,该命令将不会执行与原始命令相同的操作。那么有没有一种方法,类似于 Bash 的printf %q
,打印带有正确转义或引用参数的运行命令列表?
在 Linux 上,/proc/$pid/cmdline
对于给定的进程 ID ,您可以从命令中获得稍微更原始的 args 列表。args 由 nul 字符分隔。试着cat -v /proc/$pid/cmdline
看完全无效的^@
,你的情况:xss-lock^@--notifier=notify-send -- 'foo bar'^@slock^@
。
以下 perl 脚本可以读取 proc 文件,并通过为您的示例提供的换行符和制表符替换空值:
xss-lock
--notifier=notify-send -- 'foo bar'
slock
Run Code Online (Sandbox Code Playgroud)
或者,您可以获得如下重新引用的命令:
xss-lock '--notifier=notify-send -- '\''foo bar'\''' 'slock'
Run Code Online (Sandbox Code Playgroud)
如果更换if(1)
的if(0)
:
perl -e '
$_ = <STDIN>;
if(1){
s/\000/\n\t/g;
s/\t$//; # remove last added tab
}else{
s/'\''/'\''\\'\'\''/g; # escape all single quotes
s/\000/ '\''/; # do first nul
s/(.*)\000/\1'\''/; # do last nul
s/\000/'"' '"'/g; # all other nuls
}
print "$_\n";
' </proc/$pid/cmdline
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1341 次 |
最近记录: |