8

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?

3
  • 1
    like this: rudd-o.com/linux-and-free-software/… Commented May 9, 2016 at 10:35
  • @ArkadiuszDrabczyk why don't you write that up as an answer, with details rather than as a link? Commented May 9, 2016 at 10:47
  • Because I think it doesn't make any sense to copy it here. It took me 30 seconds to find this article and OP would find it too if he only wanted. Commented May 9, 2016 at 10:49

1 Answer 1

8

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

You must log in to answer this question.

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