更改某个进程的当前工作目录

v7d*_*po4 5 process cwd

我可以更改某个进程的当前工作目录吗?

例如,我正在运行一个 pid 为 1000 的进程。现在,它的当前工作目录是~. 我想将其当前工作目录更改为~/1. 我该怎么做?

Eig*_*ony 6

您可以使用以下脚本(在这里找到

#!/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安装(显然)。