Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Crontab运行shell打开一个jar,jar在几秒钟内打开然后关闭_Java_Shell_Jar_Raspberry Pi_Raspbian - Fatal编程技术网

Java Crontab运行shell打开一个jar,jar在几秒钟内打开然后关闭

Java Crontab运行shell打开一个jar,jar在几秒钟内打开然后关闭,java,shell,jar,raspberry-pi,raspbian,Java,Shell,Jar,Raspberry Pi,Raspbian,我正在从事一个项目,其中包括一系列数据记录传感器,这些传感器通过Java应用程序将所有数据发送回CSV文件。当手动运行时,.jar文件和.sh文件都将打开应用程序并记录数据,不会出现问题 在这一点上,我需要在每天早上6点重新启动程序,以便将CSV文件分为1天的块 下面是我正在使用的shell脚本的一个示例: !/bin/bash cd/home/pi/Desktop/Weights/410510/ sudojava-jarweight.jar 下面是我在Crontab中使用的内容: 0 6 *

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

在这一点上,我需要在每天早上6点重新启动程序,以便将CSV文件分为1天的块

下面是我正在使用的shell脚本的一个示例:

!/bin/bash cd/home/pi/Desktop/Weights/410510/ sudojava-jarweight.jar

下面是我在Crontab中使用的内容:

0 6 * * * /home/pi/Desktop/./start410510 >/tmp/file_name.log 2>&1
我添加了日志文件输出以进行调试。程序将运行,有时甚至记录1个数据点,然后立即关闭。通过键盘上的用户干预或终端的物理出口手动关闭程序。有没有可能crontab正在输入导致程序关闭的内容

以下是允许其关闭的程序部分:

        System.out.print("\n\nPress any key to close...\n\n");
        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程序时,标准输入是控制台。如果用户未输入任何数据,则任何读取操作都将等待用户输入并被阻止。这是因为流是开放的,但还没有数据

这意味着您可以使用标准输入系统中的读取作为等待方式

但是,在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运行