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。

这个错误信息表明当前目录无法访问或不存在。这可能是由于以下原因之一引起的: 1. 目录已被删除或移动:如果当前目录被删除或移动到其他位置,您将无法访问它。请检查当前目录是否存在,并且您具有适当的权限来访问它。 2. 权限问题:如果您没有足够的权限来访问当前目录或其父目录,您将无法获取当前目录的信息。请确保您具有适当的权限,并尝试使用 `ls -la` 命令来查看目录的权限和所有者。 3. 文件系统错误:如果当前目录所在的文件系统存在错误,可能会导致无法访问它。您可以尝试运行文件系统检查命令(如 `fsck`)来修复文件系统错误。 关于您提到的 "service" 命令的错误消息,它表示该命令只支持基本的 LSB(Linux Standard Base)操作,如启动、停止、重启等。对于其他操作,请尝试使用 `systemctl` 命令。 如果您遇到了这个问题,您可以尝试以下解决方法: 1. 尝试使用绝对路径:如果当前目录无法访问,您可以尝试使用绝对路径来执行操作,而不是相对路径。 2. 重新创建当前目录:如果当前目录确实不存在,您可以尝试重新创建它。使用 `mkdir` 命令创建一个新的目录,并确保您具有适当的权限。 3. 检查文件系统:如果问题是由文件系统错误引起的,您可以尝试运行文件系统检查命令(如 `fsck`)来修复错误。 请注意,根据您的具体情况可能需要采取不同的解决方法。如果问题仍然存在,请提供更多详细信息,以便我可以更好地帮助您解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值