Changing the current working directory of a certain process

cwdprocess

Can I change the current working directory of a certain process?

For example, I am running a process that has the pid 1000. Right now, its current working directory is ~. I want to change its current working directory to ~/1. How can I do it?

Best Answer

You can use the following script (found here)

#!/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

Call it by passing the PID as the first parameter and the target working directory as the second.

Caveats: This may have unexpected consequences on the target process, including files being closed, and misleading information provided in shell prompts for example.

You also need gdb installed (obviously).