Crontab运行shell打开一个jar语言 - jar打开然后在几秒钟内关闭



我正在从事一个项目,该项目包括一系列数据记录传感器,这些传感器通过Java应用程序将所有数据发送回CSV文件。当手动运行时,.jar文件和.sh文件都将打开应用程序并记录数据,而不会出现问题。

在这一点上,我需要在每天早上6点重新启动程序,以便将CSV文件分割成1天的块。下面是我使用的shell脚本示例:

#!/bin/bash cd /home/pi/Desktop/Weights/410510/ sudo java -jar weight.jar

下面是我在Crontab中使用的:

0 6 * * * /home/pi/Desktop/./start410510 >/tmp/file_name.log 2>&1

我添加了用于调试的日志文件输出。程序将运行,有时甚至记录1个数据点,然后立即关闭。该程序通过用户在键盘上的干预或终端的物理出口手动关闭。是否有可能crontab正在输入导致程序关闭的内容?

下面是程序中允许它关闭的部分:

        System.out.print("nnPress any key to close...nn");
        try {
            System.in.read();
        }
          catch (IOException ex) {}

输出应该是这样的,但是包含了整整24小时的数据点:

  pi@raspberrypi ~/Desktop $ sudo java -jar weight.jar
Waiting for the Phidget Bridge to be attached...
Phidget Information
====================================
Version: 102
Name: Phidget Bridge 4-input
Serial #: 410510
# Bridges: 4
Setting the enable state of bridge 0 to true
Setting the gain of bridge 0 to 8
Setting the enable state of bridge 1 to true
Setting the gain of bridge 1 to 8
Setting the enable state of bridge 2 to true
Setting the gain of bridge 2 to 8
Setting the enable state of bridge 3 to true
Setting the gain of bridge 3 to 8
Setting the data rate to 1000

Press any key to close...
2015-08-27 11:39:29.05,1,7.6E-4
2015-08-27 11:39:29.252,2,0.002682
2015-08-27 11:39:29.46,3,-0.001937
2015-08-27 11:39:29.836,0,-5.36E-4
2015-08-27 11:39:30.044,1,8.2E-4
2015-08-27 11:39:30.252,2,0.002563
2015-08-27 11:39:30.468,3,-0.001922
2015-08-27 11:39:30.836,0,-4.77E-4
2015-08-27 11:39:31.044,1,7.3E-4
2015-08-27 11:39:31.252,2,0.002638
2015-08-27 11:39:31.468,3,-0.001952
2015-08-27 11:39:31.836,0,-4.32E-4
2015-08-27 11:39:32.044,1,7.3E-4
2015-08-27 11:39:32.252,2,0.002667
2015-08-27 11:39:32.468,3,-0.001878
2015-08-27 11:39:32.836,0,-4.92E-4
2015-08-27 11:39:33.044,1,6.41E-4

Turning off Phidget Bridge

相反,当程序通过Crontab运行时,日志文件的输出是:

 pi@raspberrypi /tmp $ more file_name.log
Waiting for the Phidget Bridge to be attached...
Phidget Information
====================================
Version: 102
Name: Phidget Bridge 4-input
Serial #: 410510
# Bridges: 4
Setting the enable state of bridge 0 to true
Setting the gain of bridge 0 to 8
Setting the enable state of bridge 1 to true
Setting the gain of bridge 1 to 8
Setting the enable state of bridge 2 to true
Setting the gain of bridge 2 to 8
Setting the enable state of bridge 3 to true
Setting the gain of bridge 3 to 8
Setting the data rate to 1000

Press any key to close...
2015-08-27 09:16:03.086,2,-1.94E-4

Turning off Phidget Bridge

当您以交互方式运行Java程序时,标准输入是控制台。如果用户没有输入任何数据,那么任何读取操作都将等待用户输入并阻塞。这是因为流是打开,但还没有数据。

这意味着您可以使用从标准输入(System.in)读取作为等待的方式。

但是,当您在cron中非交互地运行程序时,标准输入System.in不会绑定到控制台。它被绑定到一个零字节流(或者一个具有cron项提供的某些信息的流,但这在目前是无关的)。

当您有一个零字节流时,这意味着System.in.read()不会等待。它立即返回值-1,表示到达流的末端。

这意味着如果您使用System.in.read()在交互模式下等待,它将无法在非交互模式下工作。

当你从cron运行作业时,你需要找到另一种方法来停止作业,并使用睡眠循环或其他等待机制使程序等待,直到"停止"事件发生。

作为一个简单的例子,如果你想让程序在工作一段时间后停止,比如8小时,你可以使用:

try {
    Thread.sleep( 8L * 60L * 60L * 1000L ); // Sleep for 8 hours
} catch ( InterruptedException e ) {}       // Interrupt means stop execution.
// ... code that cleans up and stops the program

请注意,告诉程序等待24小时并让cron在24小时后开始一个新的执行可能不是一个好主意,因为很有可能旧的执行和新的执行在都在运行时(然后它们可能会争夺传感器)有一个很小的间隔,或者都不运行时(然后没有人在监听传感器)有一个很小的间隔。

最好将程序设计为无限运行,并有一个计时器,每24小时滚动一次日志文件,并且根本不从cron运行它。

最新更新