简体   繁体   English

您如何确定Java程序在Linux服务器上是否失败?

[英]How can you find out if a Java program failed on a Linux server?

I was running a big overnight batch program last night written in Java on a Linux based server. 昨晚我在一个基于Linux的服务器上运行一个大型的夜间批处理程序,该程序用Java编写。 I can't seem to find anything in my error logs that suggests an error was encountered in my Java application. 我似乎无法在错误日志中找到任何暗示我的Java应用程序遇到错误的内容。

Is there a way in Linux to see if a program exited unexpectedly? Linux中是否有办法查看程序是否意外退出?

The program is one of many programs that get run overnight off a chronjob/tab and runs off its own main method. 该程序是可以在chronjob / tab中隔夜运行并运行其自己的main方法的众多程序之一。 It catches a series of exceptions which prints messages to System.err.println and exits with status one if these are hit. 它捕获了一系列异常,这些异常将消息打印到System.err.println并在被击中时以状态1退出。

NB: I always use a Logger in my code unfortunately I'm dealing with legacy code written by someone else. 注意:不幸的是,我一直在代码中使用Logger,但是我正在处理别人编写的遗留代码。

If Java crashed there will be a hs_err_pid????.log file in the working directory of the application by default. 如果Java崩溃,则默认情况下,应用程序的工作目录中将包含一个hs_err_pid????.log文件。 (This is unlikely to be the case) (不太可能是这种情况)

If the application logged an error before exiting, you need to understand where your application places its logs and read those (as they can be anywhere on your system) 如果应用程序在退出之前记录了错误,则您需要了解应用程序将其日志放置在何处并读取日志(因为它们可以在系统上的任何位置)

There's no easy mechanism to discover what you're after, if whatever tool you used to start the java JVM didn't bother recording the exit status for you. 如果您用来启动java JVM的任何工具都没有为您记录退出状态,那么就没有一种简单的机制可以发现您要执行的操作。

If you're running the auditd(8) server to provide audit logging, and your auditd(8) is configured to log abnormal exits and your java JVM exited abnormally -- signal-based termination -- then you can look for ANOM_ABEND events in /var/log/audit/audit.log : 如果您正在运行auditd(8)服务器以提供审核日志记录,并且您的auditd(8)配置为记录异常退出而java JVM异常退出(基于信号的终止),则可以在以下位置查找ANOM_ABEND事件: /var/log/audit/audit.log

# ausearch -m ANOM_ABEND
/sbin/audispd permissions should be 0750
----
time->Tue Nov  8 18:42:22 2011
type=ANOM_ABEND msg=audit(1320806542.571:264): auid=4294967295 uid=1000 gid=1000 ses=4294967295 pid=11955 comm="regex" sig=11
...

For future executions you might want to do something like this: 对于将来的执行,您可能需要执行以下操作:

java /path/to/whatever.jar && echo `date` >> /path/to/dir/success || echo `date` >> /path/to/dir/failure

This will echo the date of success or failure into a log file -- assuming that your application uses the standard Unix-style exit(0) for success and anything else for failure. 这会将成功或失败的日期回显到日志文件中-假设您的应用程序使用标准Unix风格的exit(0)表示成功,而其他则表示失败。

it is the virtual machine process (named java ) whose termination status you want to check. 它是您要检查其终止状态的虚拟机进程(名为java )。 you can write a trivial script with 2 commands, the first invokes the java vm to run the java program and the second records the exit status: echo $? 您可以使用2个命令编写一个简单的脚本,第一个调用Java vm来运行Java程序,第二个记录退出状态: echo $?

If you did write the application, you should use a logger that writes to a file. 如果确实编写了应用程序,则应使用可写入文件的记录器。

See this tutorial how to use Log4j with a file appender. 请参阅本教程,了解如何将Log4j与文件附加器一起使用。 In your code, you need to catch and log exceptions. 在您的代码中,您需要捕获并记录异常。

See this issue . 看到这个问题

Because you've run your programs out of cron(8) , there's a good chance that the standard error of the program has in fact been captured and mailed somewhere. 因为您已经用cron(8)运行了程序,所以很有可能实际上已经捕获了该程序的标准错误并将其邮寄到了某个地方。

Check the crontab(5) for the user account that runs the program. 检查crontab(5)中运行该程序的用户帐户。 (If it is run out of /etc/crontab or /etc/cron.d/ , then in those files.) Look for the MAILTO variable. (如果它用完/etc/crontab/etc/cron.d/ ,则在这些文件中。)查找MAILTO变量。 If it doesn't exist, then cron(8) tried to deliver mail to the crontab(5) owner. 如果不存在,则cron(8)尝试将邮件传递给crontab(5)所有者。 If it does exist, then cron(8) tried to deliver mail to whoever is specified with the variable. 如果确实存在,则cron(8)尝试将邮件传递给使用该变量指定的任何人。

Look in /var/spool/mail/ for the user's mailbox, if the server doesn't seem like it's got an email setup in place -- there might be enough for local delivery. /var/spool/mail/查找用户的邮箱,如果服务器看起来好像没有设置电子邮件-本地交付可能就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到运行java程序时使用的JIT名称 - How to find out the name of JIT used when you run your java program 我可以找出java程序是使用java还是javaw启动的 - Can I find out if the java program was launched using java or javaw 如何找出在Linux上运行Java类所需的共享库? - How can I find out which shared libraries I need to run a Java class on Linux? 我如何才能知道是否有人正在使用 Python 或 Java 积极使用 Linux 计算机? - How can I find out if someone is actively using a Linux computer in Python or Java? 如何找出我的 Java 程序不断崩溃的原因? - How can I find out why my Java program keeps crashing? 你能帮我找出这个 Java 代码中的错误吗 - Can you help me to find out the error in this Java Code 如何在Linux Server上通过Java程序检查Oracle Listner状态 - How to check oracle listner status by java program on linux Server 如何从Eclipse Windows到Linux服务器执行Java程序 - How to execute java program from eclipse windows to Linux server 您是否可以仅使用名称在Linux中运行Java程序,而不使用“java”命令? - Can you run a Java program in Linux using the name only, without the “java” command? 我在哪里可以找到 Linux 服务器上的“java 二进制文件的完整路径”? - Where can I find the "full path to java binary" on linux server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM