shell-init: error retrieving current directory 解决方案

现象 

在linux程序运行过程中,调用system()时候出现了

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

原因

诱发的原因为:当前所有路径为已经被删除的路径。常见操作为处于当前目录,又另一个操作删除了当前目录。如下所示:

```
let’s try it:
1、Open a terminal window and go cd ~
2、Create a new directory and cd into it mkdir adir && cd adir
3、Now, open a new terminal window and go to that same place cd ~
4、Now, delete the directory rm -rf adir
5、Finally - go back to the previous window and try to do just about any command you can come up with and it will give you errors like the ones above.
Not surprisingly, since you are in essence running the command in a directory that doesn’t exist.
Easy to mix up when you are new to the terminal - and apparently hard to grasp even if you have been using the terminal for years…
```

解决方案

如果觉得没有类似上述的操作,可能的解法包括:

1、查看当前环境变量是否有问题

```

can you check your HOME env variable ? echo $HOME; if that points to an unexisting location, that't it. – Costi Ciudatu Sep 9 '12 at 15:57

```

2、虚拟机设备的硬件虚拟化未开启

```
not after rebooting the problem is still there . But SOLVED
I am running the traccar server in a QEMU virtualization environmentas as a virtual machine , one of the disabled options for this virtual machine was "KVM Hardware Virtualization" . After enabling this option all the error messages completely disappeared and all commands running as expected . this may give you a clue to a code problem. thank you
```
3、再看看代码,把所有调用 system 的相关log打印出来,查看是经过哪个操作后就会出现此error log。

本人就是通过添加log后,发现是调度一个解压函数,解压函数内部进行了移动目录的操作,后续将此解压目录删除后。进程所处于目录还是位于解压目录下,而此时解压目录不存在,于是报出此 error。

你可能感兴趣的:(shell-init: error retrieving current directory 解决方案)