Making a process read a different file for the same filename

In recent versions of Linux, you can unshare the mount namespace. That is, you can start processes that view the virtual file system differently (with file systems mounted differently).

That can also be done with chroot, but unshare is more adapted to your case.

Like chroot, you need superuser priviledged to unshare the mount namespace.

So, say you have ~/.configuration and ~/.configuration-for-that-cmd files.

You can start a process for which ~/.configuration is actually a bind-mount of ~/.configuration-for-that-cmd in there, and execute that-cmd in there.

like:

sudo unshare -m sh -c "
   mount --bind '$HOME/.configuration-for-that-cmd' \
                '$HOME/.configuration' &&
     exec that-cmd"

that-cmd and all its descendant processes will see a different ~/.configuration.

that-cmd above will run as root, use sudo -u another-user that-cmd if it need to run as a another-user.


Soft links.

Create two config files, and point to one of them with a soft link most of the time, but change the soft link to point to the other one when the special app is running.

(I know this is a horrible hack, but it's slightly more reliable than changing file contents).

Or, manipulate $HOME.

In the script which starts the annoying process, set $HOME to be something under the regular $HOME directory, and your app should then use the config file located there (tested, and works for basic shell commands, ~ expands to $HOME).

Depending on what else the process does, changing $HOME may have unintended consequences (i.e. output files might end up in the wrong place).