shell-init: error retrieving current directory: getcwd -- The usual fixes do not wor

LinuxBashShellSshAnsible

Linux Problem Overview


I have a simple script:

#!/bin/bash
for server in $(~/.ansible/ansible_hosts)
do
    ssh $server "hostname; readlink /opt/mydir/mylink;"
done

It works fine - the program returns the correct hostname and link - except that I get the following error on some but not all of the servers:

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

All the directories exist. One of the most common suggestions has been to add a cd, a cd -, or a cd /. All that happens when that step is added is an additional:

chdir: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

I tried kickstarting the nfs daemon on the off chance that there was some confusion about my homedir and substituted /etc/init.d in case the problem was with /opt. No difference

This would simply be an annoyance except that when I try to use an ansible playbook instead of a simple ssh command it fails for that server.

Any insights would appreciated.

Linux Solutions


Solution 1 - Linux

I believe the error is not related to the script at all. The issue is: the directory at which you are when you try to run the script does not exist anymore. for example you have two terminals, cd somedir/ at the first one then mv somedir/ somewhere_else/ at the second one, then try to run whatsoever in the first terminal - you'll receive this error message.

Please note you'll get this error even if you re-create directory with the same name because the new directory will have different inode index.

At least this was in my case.

Solution 2 - Linux

According to this answer, it happens when "you re-create directory" but already have a shell at this location, so we could simply run:

cd ~ && cd -

Notice: If you are using zsh and end up in this state, you might see a lock icon at the beginning of your prompt (this normally indicates that you don't have write permissions).

Solution 3 - Linux

On Mac, just kill and relaunch the Terminal. It will get it right.

Solution 4 - Linux

opening a new tab on mac terminal worked for me.

Solution 5 - Linux

You are executing this as a script.. $(~/.ansible/ansible_hosts). The $() means that bash will attempt to execute that script and then output the results.

But it's not a script, right? It's a list of hosts!

Just add the word cat and it should work.

#!/bin/bash
for server in $(cat ~/.ansible/ansible_hosts)
do
    ssh $server "hostname; readlink /opt/mydir/mylink;"
done

Solution 6 - Linux

I had same error in my unittest in my tearDown method I was creating and removing test files for my test cases. Strangely enough even I had full paths for the directories I wanted to delete through for loop, I had to provide os.chdir(path_where_dirs_at) before to get rid of the error. Don't know if it's a good solution to it, but messages dissapeared.

Solution 7 - Linux

I find a solution in a similar problem in Colab suddenly unable to navigate through directories

Without losing your variables in the Colab instance,

Use os library to change directory.

import os
path = "/content" # /content is pretty much the root. you can choose other path in your colab workspace
os.chdir(path)

This solution works and doesn't require restarting the runtime (and losing all data).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTodd EllnerView Question on Stackoverflow
Solution 1 - LinuxPutnikView Answer on Stackoverflow
Solution 2 - LinuxcglacetView Answer on Stackoverflow
Solution 3 - LinuxMedhiView Answer on Stackoverflow
Solution 4 - LinuxPratyaksh SharmaView Answer on Stackoverflow
Solution 5 - Linuxfatal_errorView Answer on Stackoverflow
Solution 6 - LinuxPoliView Answer on Stackoverflow
Solution 7 - LinuxHong ChengView Answer on Stackoverflow