当前位置:   article > 正文

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

java.io.ioexception: 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_profile
    • 2. ~/.bash_login
    • 3. ~/.profile
  • 加载
    • 登录即加载
    • 主动加载
      • source
      • .

 

非登陆shell配置

  • ~/.bashrc
    • /etc/bashrc
  • 用户登录后,直接运行bash命令启动的shell
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/504000
推荐阅读
相关标签
  

闽ICP备14008679号