简体   繁体   English

Bash脚本无限循环关闭

[英]Bash script infinite loop shutdown

I wrote a short bash script which collects CPU temperature every 10 seconds and outputs it to a file along with some other data. 我编写了一个简短的bash脚本,该脚本每10秒收集一次CPU温度,并将其与其他一些数据一起输出到文件中。 Running the script in the terminal works perfectly fine. 在终端中运行脚本非常正常。 However, once I setup the script to run on startup, I encounter 2 problems: 但是,一旦将脚本设置为在启动时运行,就会遇到2个问题:

-opening the resulting .txt file with gedit results in: "gedit has not been able to detect the character encoding. Please check that you are not trying to open a binary file." -使用gedit打开生成的.txt文件会导致:“ gedit无法检测到字符编码。请检查您是否未尝试打开二进制文件。” Select a character encoding from the menu and try again." Opening the file with Libre Writer works fine and the file has correct data. 从菜单中选择一个字符编码,然后重试。”使用Libre Writer打开文件可以正常工作,并且文件中的数据正确。

-first time when I try to shutdown after placing the script in /etc/init.d/ and running update-rc.d the PC takes inexplicably long to do it, to the point that I have to shutdown manually. -第一次将脚本放在/etc/init.d/中并运行update-rc.d后,我尝试关闭PC时,莫名其妙地花费了很长时间,以至于我不得不手动关闭。

This is the script code: 这是脚本代码:

#!/bin/bash

readonly DIR_PATH='/home/ivan/Documents/temp_data/' # path to output dir, change to yours, don't use HOME variable

while true; do
    temps_str=$(sensors | grep "Physical" | tr -dc "[:digit:][^ °.C]") #  extract numbers from sensors command output
    temps_str=${temps_str:5} # remove first 5 characters, they are ' ', ' ', '0', ' ', ' ' and useless
    temps_array=($temps_str) # convert string to array

    temp_now=${temps_array[0]} # CPU temp now
    temp_high=${temps_array[1]} # highest CPU temp recorded in this session
    temp_max=${temps_array[2]} # CPU temp at which PC turns off

    dt=$(date +%d-%m-%Y) # date, format: dd-mm-yyyy
    time=$(date +%H:%M:%S) # time, format: HH:MM:SS

    # create output directory if it doesn't already exist
    if [ ! -d $DIR_PATH ]; then
        mkdir -m 755 $DIR_PATH
    fi

    echo $time $temp_now $temp_high $temp_max >> ${DIR_PATH}${dt}.txt # write to output file

    sleep 10 # wait 10 seconds
done

This is the format of the output file: 这是输出文件的格式:

17:22:21 58.0°C 87.0°C 105.0°C 17:22:21 58.0°C 87.0°C 105.0°C

17:22:31 56.0°C 87.0°C 105.0°C 17:22:31 56.0°C 87.0°C 105.0°C

17:22:41 58.0°C 87.0°C 105.0°C 17:22:41 58.0°C 87.0°C 105.0°C

17:22:51 59.0°C 87.0°C 105.0°C 17:22:51 59.0°C 87.0°C 105.0°C

17:23:01 58.0°C 87.0°C 105.0°C 17:23:01 58.0°C 87.0°C 105.0°C

17:23:11 59.0°C 87.0°C 105.0°C 17:23:11 59.0°C 87.0°C 105.0°C

17:23:21 60.0°C 87.0°C 105.0°C 17:23:21 60.0°C 87.0°C 105.0°C

17:23:32 63.0°C 87.0°C 105.0°C 17:23:32 63.0°C 87.0°C 105.0°C

17:23:42 63.0°C 87.0°C 105.0°C 17:23:42 63.0°C 87.0°C 105.0°C

When I get the 'eternal shutdown', I shut it down manually. 当我得到“永久关闭”时,我手动将其关闭。 After rebooting, the file shows that the scripts was still running and writing to file while the PC was frozen, then a line of #s (which probably get written during the manual shutdown). 重新启动后,该文件显示脚本仍在运行,并且在PC冻结时正在写入文件,然后显示#号行(可能在手动关闭期间写入)。 I'm wondering what causes the freeze and why does the .txt file charset get 'corrupted'? 我想知道是什么原因导致冻结,为什么.txt文件字符集被“损坏”?

The scripts in /etc/init.d are expected to start and stop services. /etc/init.d中的脚本有望启动和停止服务。 So they need to be prepared to accept the command-line arguments start and stop . 因此,他们需要准备好接受命令行参数startstop (If they also accept arguments like restart and status , that's a bonus.) (如果他们也接受诸如restartstatus类的参数,那将是一个好处。)

When you put a symlink to one of these scripts in a directory in /etc/rcN.d , then the script will be invoked with either the argument start or stop : 当将符号链接放置到/etc/rcN.d目录中的这些脚本之一时,该脚本将使用参数startstop调用:

  • On start-up, the argument depends on whether the name of the symlink starts with S ( start ) or K ( stop -- the letter stands for "kill"). 在启动时,参数取决于符号链接的名称以Sstart )还是Kstop字母代表“ kill”)开头。

  • On shut-down, the argument will be stop . 关机时,参数将为stop

(The above is the debian behaviour, taken from the Debian policy manual .) (以上是Debian的行为,摘自Debian政策手册 。)

Your script is not of this form. 您的脚本不是这种形式。 It simply runs the monitor program. 它只是运行监视程序。 So when it is invoked with the argument stop , it will not stop monitoring. 因此,当使用参数stop调用它时,它不会停止监视。 It will just start up another monitoring loop, which never terminates . 它将启动另一个监视循环,该循环永远不会终止 Because it is called directly from the shutdown sequence, that will prevent the shutdown from terminating. 因为它是直接从关闭序列中调用的,所以可以防止关闭终止。

The usual approach to writing init.d scripts is based on the following: 编写init.d脚本的常用方法基于以下内容:

  • When called with the start option, the script initiates the service (ie, your current monitoring script) as a daemon process and writes its PID to a file whose name is related to the service name. 当使用start选项调用该脚本时,该脚本将其作为守护进程启动服务(即您当前的监视脚本),并将其PID写入名称与服务名称相关的文件中。 (A common place to put this file is the /var/run directory, usually with the extension .pid .) There are some standard utilities to start a process as a daemon, which can help with this task. (放置此文件的常见位置是/var/run目录,通常带有扩展名.pid 。)有一些标准实用程序可以将进程作为守护程序启动,可以帮助完成此任务。 If you have a Debian-based (including Ubuntu) installation, take a look at the shell utility library /lib/lsb/init-functions . 如果您具有基于Debian的安装(包括Ubuntu),请查看shell实用程序库/lib/lsb/init-functions

  • When called with the stop option, the script uses the PID stored in the file created in the start action to kill the process. 当被叫stop选项,该脚本使用存储在创建的文件中的PID start行动, kill进程。 It then deletes the pid file. 然后删除pid文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM