115

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.

8
  • 3
    Does ~/.ansible/ansible_hosts contain a list of servers? That's an odd way to read the values if so
    – arco444
    Commented Apr 1, 2015 at 17:41
  • hope it's not a simple matter of permissions?
    – Kashyap
    Commented Apr 1, 2015 at 18:10
  • What happens when you manually log in on these machines? Commented Apr 1, 2015 at 18:25
  • arco444 - Well, the .ansible/ansible_hosts was actually from someone else's directory which is where I first saw the problem. I tried it with a hosts.txt file in my home directory. Same behavior. Kashyap - I have permission to be in my home directory and the other directories I've tried this with. the shell initialization files are the same across the whole environment, and logging in manually doesn't raise these errors. Commented Apr 1, 2015 at 19:33
  • that other guy - perfectly normal, uneventful login. Further symptom: a "cd -" works fine from the shell, but when it's in the script I see a "cd: OLDPWD not set" error. Commented Apr 1, 2015 at 19:39

8 Answers 8

233

Most likely the error is not related to the script at all. The issue is: a directory in 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 the directory with the same name because the new directory will have a different inode index.

6
  • 1
    I agree with Putnik. Keep in mind that, even if your directory name appears to not change, you could be referencing a symbolic link that references an explicit path that has changed. This fouled me up as well.
    – Josh Lopez
    Commented Apr 18, 2017 at 16:01
  • Or you could be cd'ing around in some function.
    – mpersico
    Commented Aug 13, 2019 at 19:16
  • 1
    This was my case as well. I had pushed to the server again and apparently this overwrite my previous code and I got this error. I needed to exit the server and then SSH back into the server and the error went away. Thanks! Commented May 5, 2020 at 9:27
  • 3
    It also happens (on mac) when you run a command from a folder which was deleted and then recreated but you didn't refresh your shell.
    – ccpizza
    Commented Mar 1, 2021 at 20:29
  • This is what happened to me also. Killing that session and starting a new one would also work as mentioned by @Medhi. The root cause is that the directory didn't exist anymore which wouldn't be referenced in a new session.
    – sholsinger
    Commented Dec 19, 2023 at 18:53
92

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

4
43

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

2
  • 1
    what does cd - do?
    – alper
    Commented Jul 20, 2022 at 22:19
  • 3
    cd - goes back to the previous ("old") directory, for example if you run cd x && cd y/z && cd - you endup in directory x.
    – cglacet
    Commented Jul 21, 2022 at 14:27
5

opening a new tab on mac terminal worked for me.

2

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.

1

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
1
  • It could very well be that ansible_hosts is the name of a script that returns all hosts; otherwise, I think the script would not have worked in the first place since the result of executing a non-executable would be the empty string (the error being printed to stderr), and attempting to loop over that should not have executed the ssh command even once ...
    – AdminBee
    Commented Jan 21, 2021 at 10:54
1

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

-3

maybe because of the first line "#!/bin/bash". try others, for example: #!/bin/ksh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.