linux
Java在linux上调用shell脚本?
一、Java在linux上调用shell脚本?
近期正好结合工作实践,分享了一篇关于Java如何调用Shell的文章,希望能够帮到您。内容如下。
《Java 配 Shell 等于美酒加咖啡》
化学中我们得知「氢气加氧气在点燃的情况下会生成水」。
生活中我们得知「良辰加美景的情况下会得到千金春宵一刻」。
技术上又何尝不是如此呢?先假设一个场景:BOSS 让你实现一个服务监控的指挥室,能看到每个服务器的磁盘剩余空间,能看到。。。能看到。。。
其实讲真,实现思路有很多,但是不管黑猫白猫能抓住老鼠都是好猫,今天我们尝试用 Java 与 Shell 搭配一下,看看是否会产生惊奇的反应。
1. 首先通过 JDK 源码,品一品 Runtime 这杯美酒。
上图是摘取 JDK 中 Runtime 的部分源码,主要分成 4 大代码段来粗略认识她。
第一块代码段,可以看出 Runtime 构造私有化,提供了静态属性,并提前创建对象实例,并提供获取实例的静态方法,这不就是单例设计模式的使用么,当有面试官再问设计模式,拿去狂喷。
第二块代码段,主要是 addShutdownHook() 方法,添加关闭的钩子,说的直白点,其实允许研发人员插入一段在 JVM 关闭时执行的代码。例如在搭建服务框架时,面对需要完成优雅停服,打扫战场,释放资源等等,诸如此类的场景下都很有用。其中在 Tomcat、Jetty 等容器中都可以看到 shutdownHook 的身影。
Runtime runtime = Runtime.getRuntime();
runtime.addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("打扫战场,释放资源,完成优雅停服");
}
});
System.out.println("服务启动完成");
代码运行效果如下。
服务启动完成
打扫战场,释放资源,完成优雅停服
第三块代码段,主要展现 JDK 针对 Runtime 提供的系列 exec 重载方法,这个是本次分享的重点,重头戏最后再说。
第四块代码段,主要是 Runtime 提供的一些获取系统信息的 API,直接抛代码,拿去用就行了。
Runtime runtime = Runtime.getRuntime();
System.out.println(String.format("JVM可用本机CPU内核数 %d", runtime.availableProcessors()));
//默认为系统的1/4
System.out.println(String.format("最大可用内存空间 %d M", runtime.maxMemory() / 1024 / 1024));
//默认为系统的1/64
System.out.println(String.format("可用内存空间 %d M", runtime.totalMemory() / 1024 / 1024));
System.out.println(String.format("空闲内存空间 %d M", runtime.freeMemory() / 1024 / 1024));
代码运行输出如下,其实真实环境中不妨用模板引擎 FreeMarker 渲染,然后通过邮件告警,实现的逼格高一些。
2. 在详细说 Runtime.exec() 这个重头戏之前,再品一品 df 这款咖啡。
Linux df 命令,用于显示目前在系统上的磁盘使用情况统计,主要用于查看磁盘的分区,磁盘已使用的空间,剩余的空间。
命令如下:
df [选项]... [FILE]...
常用选项如下:
3. Runtime 美酒加 Shell 咖啡会发生什么呢?
重头戏开始,回到 Runtime 的源码,我们看到 exec() 系列方法会帮我们启动一个 Process 进程,那不妨把 df -h 命令传入进去一探究竟。
public class Foo {
public static void main(String[] args) throws Exception {
//df命令用于查看磁盘的分区,磁盘已使用的空间,剩余的空间
//df -h以合适的单位来显示信息
System.out.println(exec("df -h"));
}
private static String exec(String command) throws Exception {
String[] cmd = {"/bin/sh", "-c", command};
StringBuilder out = new StringBuilder();
BufferedReader reader = null;
InputStream in = null;
try {
Process process = Runtime.getRuntime().exec(cmd);
in = process.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
out.append(line + "\n");
}
process.waitFor();
} finally {
if (reader != null) {
reader.close();
}
}
return out.toString();
}
}
代码中会发现调用了 process 的 waitFor() 方法,此方法作用会导致当前线程等待,一直要等到由该 Process 对象表示的进程终止,其实也就是等待把 exec 里面启动的 Process 中的所有事都干完(生产上出问题的大多出在这儿),代码运行效果如下。
效果确实可以,那么这么一来,想监控统计什么功能,不妨直接把命令交给 Java 程序去执行即可。
4.
如果关注一猿小讲的伙伴应该清楚,在《如何让Java应用成为杀不死的小强?(中篇)》《如何让Java应用成为杀不死的小强?(下篇)》两篇中都用到了 ProcessBuilder 来执行命令启动进程,那 Runtime 与 ProcessBuilder 的主要区别是啥呢?
其实 Runtime.exec() 方法设计,可接受一个单独的字符串,这个字符串是通过空格来分隔可执行命令程序和参数的;当然也可以接受字符串数组参数。
如上图所示,ProcessBuilder 的方法入参是一个List<String>或者多个字符串。
相同点是 ProcessBuilder.start() 和 Runtime.exec() 方法都被用来创建一个操作系统进程(执行命令行操作)。
5. 好了,几分钟的简单分享,主要让你接触并真正认识一下 Runtime,希望能帮助你在工作中锦上添花。
最后依然用阿里新六脉神剑中的三脉送给大家:今天最好的表现是明天最低的要求;此时此刻非我莫属;认真生活快乐工作!
二、linux shell是什么?
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。 Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。
三、Linux shell命令,-d?
[ ] 部分是判断表达式,-d 表示判断是否是目录(directory)。&& 是“逻辑与”操作符(这个与C语法类似啊),只有 && 前面的判断成立(返回逻辑真),后面的语句才会得到执行。总之,含义就是:若/root/Desktop/为目录,就执行 chmod 777 /root/Desktop/download.desktop若/root/Desktop/不是目录(不存在该目录),就执行 chmod 777 /root/桌面/download.desktop其实就是处理 Desktop 是英文和中文两种情况。
四、shell java debug
Shell Java Debug技巧
在软件开发过程中,调试是至关重要的环节。在Java开发中,通过命令行工具(shell)进行调试是一种高效的方式。本文将介绍一些Shell Java Debug的技巧,帮助开发人员更好地定位和解决问题。
1. 使用Shell进行Java应用程序调试
在进行Shell Java Debug之前,首先需要确保已经安装了Java Development Kit(JDK)并设置了环境变量。接下来,可以通过以下步骤在命令行中调试Java应用程序:
- 使用
javac
命令编译Java源文件,生成.class文件。 - 使用
java
命令运行Java应用程序,观察程序行为。 - 使用
jdb
命令启动Java调试器,连接到正在运行的Java进程。 - 通过
stop in
和step
命令设置断点并逐步执行程序。
通过这些步骤,可以在Shell中方便地对Java应用程序进行调试,定位问题所在。
2. 常见的Shell Java Debug技巧
在实际的Java调试过程中,以下几点技巧可能会帮助开发人员更快速地解决问题:
2.1 使用断点
通过在源代码中设置断点,可以让程序在特定位置停止执行,方便开发人员查看变量状态、代码执行顺序等信息。在jdb中,可以使用stop in
命令设置断点。
2.2 监视变量
在调试过程中,监视关键变量的数值变化是非常重要的。通过print
命令可以在Shell中查看特定变量的值,帮助开发人员分析程序状态。
2.3 模拟异常
有时候为了测试特定情况下的代码行为,可以在代码中人为地抛出异常。在jdb中,可以使用throw
命令模拟异常抛出的情况,进而验证程序的异常处理逻辑。
2.4 查看线程信息
在多线程程序调试过程中,了解每个线程的状态和堆栈信息是至关重要的。通过threads
命令可以查看当前Java进程中所有线程的信息,帮助开发人员排查并发问题。
2.5 调整调试级别
有时候需要在调试过程中输出更详细的信息,这时可以通过trace
命令调整调试级别。调整调试级别可以让开发人员更清晰地了解程序的执行流程。
3. 实战案例:使用Shell Java Debug解决问题
以下是一个使用Shell Java Debug技巧解决问题的实战案例:
假设某个Java应用程序在运行过程中出现了NullPointerException异常,开发人员需要通过调试找出异常的根本原因。可以通过以下步骤使用jdb进行调试:
- 使用
jps
命令查看正在运行的Java进程的ID。 - 通过
jdb -attach <进程ID>
命令连接到目标Java进程。 - 设置断点并逐步执行程序,定位引发异常的代码行。
- 使用
print
命令查看相关变量的数值,分析异常原因。 - 通过调整调试级别等方式获取更多信息,解决问题。
通过这个实战案例,开发人员可以更深入地了解如何利用Shell Java Debug技巧解决实际的程序问题。
结语
Shell Java Debug是Java开发中一个重要的调试工具,能够帮助开发人员更快速地定位和解决问题。通过本文介绍的技巧和实战案例,相信读者对Shell Java Debug有了更深入的了解。在日常的Java开发工作中,不妨多多尝试使用Shell进行调试,提升自己的调试能力。
五、shell窗口java
在shell窗口中运行Java程序的方法
编写和运行Java程序是每个Java开发人员日常工作的一部分。通过使用命令行工具,您可以在shell窗口中轻松地编译和运行Java程序。本文将向您展示在shell窗口中运行Java程序的方法,让您能够更高效地开发和测试您的Java应用程序。
准备工作
在开始之前,您需要确保已经安装了Java开发工具包(JDK)并已设置好JAVA_HOME环境变量。您还需要熟悉命令行工具的基本操作,以便能够顺利执行命令。
编写Java程序
首先,使用您喜欢的文本编辑器编写Java程序。在编写Java程序时,确保文件扩展名为.java。以下是一个简单的Java程序示例:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }在完成编写Java程序后,将文件保存到您选择的目录中。
编译Java程序
打开您的shell窗口,并导航到保存Java程序的目录。使用javac命令来编译Java程序。在shell窗口中输入以下命令:
javac HelloWorld.java
如果编译成功,将会生成一个.class文件,该文件包含了编译后的Java字节码。
运行Java程序
在编译成功后,您可以使用java命令来运行Java程序。在shell窗口中输入以下命令:
java HelloWorld
如果一切顺利,您将在shell窗口中看到输出:Hello, World!,这表明您成功地运行了Java程序。
结论
通过本文所介绍的方法,在shell窗口中运行Java程序变得轻而易举。这种方法不仅适用于简单的程序,也可用于复杂的Java应用程序。通过命令行工具,您可以更灵活地控制和调试您的Java程序,从而提高您的开发效率。
六、先学shell还是linux?
我个人认为想学linux,首先学shell编程还是有必要的。 通过学习shell编程,你可以很快的熟悉linux中各种命令的操作、脚本的编写、函数的应用等等。这为进一步学好linux的各种应用和服务打基础,也是不断熟悉linux的一种途径。 这只是我的看法,我是先学习shell编程的。
七、linux必须要shell么?
Linux的shell有很多个,包括bash。shell在内核、系统调用之外,所以叫shell,作为用户与计算机交互的界面。
因为Unix/Linux早期都是命令行界面,所有的功能都通过命令行完成,所以shell是很重要的, 即便有了图形用户界面,很多系统维护、自动化处理方面的任务还是通过shell完成更加高效,而且有的功能只能通过命令行。
命令行在主机、服务器的远程登录方面具有优势,因为安全和节省带宽,比如服务器托管在电信机房,在公司远程登录上去维护。
八、linux如何查看shell版本?
1、查看当前系统支持的Shell,使用cat、head、more等命令查看/etc/shells的内容即可。 cat /etc/shells 2、查看各个shell的版本 ,每个shell基本都带有--version参数,用于显示shell的版本号,执行如下命令 ksh --version bash --version tcsh --version
九、linux中什么是shell?
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。
十、linux的shell的求根?
我的回答:可以在shell里面用sqrt()求根函数,并且借助高级的精密数学计算器bc。
shell求根举例:
#! /bin/bash
echo "sqrt(100)"|bc
执行shell脚本文件,就会得出结果是10。
热点信息
-
在Python中,要查看函数的用法,可以使用以下方法: 1. 使用内置函数help():在Python交互式环境中,可以直接输入help(函数名)来获取函数的帮助文档。例如,...
-
一、java 连接数据库 在当今信息时代,Java 是一种广泛应用的编程语言,尤其在与数据库进行交互的过程中发挥着重要作用。无论是在企业级应用开发还是...
-
一、idea连接mysql数据库 php connect_error) { die("连接失败: " . $conn->connect_error);}echo "成功连接到MySQL数据库!";// 关闭连接$conn->close();?> 二、idea连接mysql数据库连...
-
要在Python中安装modbus-tk库,您可以按照以下步骤进行操作: 1. 确保您已经安装了Python解释器。您可以从Python官方网站(https://www.python.org)下载和安装最新版本...