shell-init: error retrieving current directory: ge

2020-05-25 05:49发布

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.

3条回答
相关推荐>>
2楼-- · 2020-05-25 06:18

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.

查看更多
\"骚年 ilove
3楼-- · 2020-05-25 06:26

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
查看更多
我命由我不由天
4楼-- · 2020-05-25 06:36

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.

At least this was in my case.

查看更多
登录 后发表回答