700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java远程执行命令报错java.io.IOException: Cannot run program “ifconfig“: error=2 No such file or directory

java远程执行命令报错java.io.IOException: Cannot run program “ifconfig“: error=2 No such file or directory

时间:2021-01-24 13:06:19

相关推荐

java远程执行命令报错java.io.IOException: Cannot run program “ifconfig“: error=2  No such file or directory

直接执行ifconfig没问题,但是在java代码里远程执行ifconfig就找不到命令。

session = conn.openSession();

session.execCommand(cmd);

// Cannot run program "ifconfig": error=2, No such file or directory

解决办法:

在 用户的 home 目录下的 文件 .bashrc 加入

export PATH=$PATH:/sbin

然后执行

source .bashrc 就可以执行 ifconfig 命令了

解决办法二:

在用户目录的.bashrc文件里加上 source/etc/profile

原因分析:java远程执行shell应该是非登录shell。非登录shell与登录shell加载的配置文件不同,环境变量不同。

因为非登录shell方式的 .bashrc中的path没有将/sbin目录加入到path中,导致找不到下面的ifconfig命令。只要在用户.bashrc文件中将/sbin目录配置上就行了。

区分方法

如何区分登录 shell 和非登录 shell 呢,可以通过查看 $0 的值,登录 shell 返回 -bash,而非登录 shell 返回的是 bash 。

注意:

执行 exit 命令, 退出的 shell 可以是登录 或者 非登录 shell ;

执行 logout 命令,则只能退出登录 shell,不能退出非登录 shell

附录:登录shell和非登录shell的区别

登录shell配置

用户登陆时,输入用户名和密码后启动的shell通过带--login参数的命令:bash --login而启动的shell加载文件顺序 1. ~/.bash_profile2. ~/.bash_login3. ~/.profile加载 登录即加载主动加载 source.

非登陆shell配置

~/.bashrc /etc/bashrc用户登录后,直接运行bash命令启动的shell

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。