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?


1 Answer 1


You can use the following script (found here)


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")

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).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .