7

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
v7d8dpo4
  • 668
  • 5
  • 12

1 Answers1

7

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

EightBitTony
  • 20,963
  • 4
  • 61
  • 62