您可以使用以下脚本(在这里找到)
#!/bin/bash
pid="$1" # first arguvment is the PID
cwd="$2" # second argument is the target working directory
# now let's command the GNU debugger
gdb -q <<EOF
attach $pid
call (int) chdir("$cwd")
detach
quit
EOF
Run Code Online (Sandbox Code Playgroud)
通过将 PID 作为第一个参数和目标工作目录作为第二个参数来调用它。
警告:这可能会对目标进程产生意想不到的后果,包括文件被关闭,以及 shell 提示中提供的误导性信息。
您还需要gdb安装(显然)。