【问题标题】:Crontab runs shell that opens a jar - jar opens then closes within secondsCrontab 运行打开一个 jar 的 shell - jar 打开然后在几秒钟内关闭
【发布时间】:2015-08-27 16:38:56
【问题描述】:

我正在开展一个项目,该项目包括一系列数据记录传感器,这些传感器通过 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("\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 shell jar raspberry-pi raspbian


    【解决方案1】:

    当您以交互方式运行 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 小时并在 24 小时后让 cron 启动一个新的可能不是一个好主意,因为旧执行和新执行的可能性很大两者都在运行时的间隔(然后它们可能会争夺传感器),或者两者都在运行时的小间隔(然后没有人在听传感器。)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      相关资源
      最近更新 更多