- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我写了一个简短的 bash 脚本,它每 10 秒收集一次 CPU 温度,并将其与其他一些数据一起输出到一个文件中。在终端中运行脚本工作得很好。但是,一旦我将脚本设置为在启动时运行,我遇到了 2 个问题:
- 使用 gedit 打开生成的 .txt 文件会导致:“gedit 无法检测到字符编码。请检查您是否在尝试打开二进制文件。”从菜单中选择一种字符编码,然后重试。”使用 Libre Writer 打开文件工作正常,文件具有正确的数据。
-当我在将脚本放入/etc/init.d/并运行 update-rc.d 后第一次尝试关机时,PC 花了莫名其妙的时间来完成它,以至于我不得不手动关机。
这是脚本代码:
#!/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
这是输出文件的格式:
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:41 58.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:11 59.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:42 63.0°C 87.0°C 105.0°C
当我收到“永久关机”时,我手动将其关闭。重新启动后,文件显示脚本仍在运行并写入文件,而 PC 已卡住,然后是一行 #s(可能是在手动关机期间写入的)。我想知道是什么原因导致卡住以及为什么 .txt 文件字符集会“损坏”?
最佳答案
/etc/init.d
中的脚本用于启动和停止服务。因此,他们需要准备好接受命令行参数 start
和 stop
。 (如果他们也接受诸如 restart
和 status
之类的参数,那就太好了。)
当您将这些脚本之一的符号链接(symbolic link)放在 /etc/rcN.d
中的目录中时,将使用参数 start
或停止
:
在启动时,参数取决于符号链接(symbolic link)的名称是否以 S
(start
)或 K
开头(stop
-- 这个字母代表“kill”)。
关闭时,参数将为stop
。
(以上是 debian 行为,取自 Debian policy manual 。)
您的脚本不是这种形式。它只是运行监控程序。所以当它被参数 stop
调用时,它不会停止监控。它只会启动另一个监控循环,永远不会终止。因为它是直接从关闭序列调用的,所以会阻止关闭终止。
编写 init.d
脚本的通常方法基于以下内容:
当使用 start
选项调用时,脚本将服务(即您当前的监控脚本)作为守护进程启动,并将其 PID 写入名称与服务名称。 (放置此文件的常见位置是 /var/run
目录,通常扩展名为 .pid
。)有一些标准实用程序可以将进程作为守护进程启动,这可以帮助完成这项任务。如果您有基于 Debian(包括 Ubuntu)的安装,请查看 shell 实用程序库 /lib/lsb/init-functions
。
当使用 stop
选项调用时,脚本使用存储在 start
操作中创建的文件中的 PID 来kill
过程。然后删除 pid 文件。
关于linux - Bash 脚本无限循环关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34816141/
我用 IntelliJ IDEA 2021.1 CE 在 流行!_OS 20.04 与 bash 5.0.17 . 问题造句:我将IntelliJ终端设置为/bin/bash通过 IntelliJ 设
给定如下命令: bash --shortcuts 我想显示一个快捷方式列表,就像在这个页面上一样: http://www.skorks.com/2009/09/bash-shortcuts-for-m
我有一个脚本可以操作数据、创建参数并将它们发送到第二个脚本。其中一个参数包含一个空格。 脚本1.sh: args=() args+=("A") args+=("1 2") args+=("B") .
我的脚本的“只运行一次”版本的一个非常简单的示例: ./myscript.sh var1 "var2 with spaces" var3 #!/bin/bash echo $1 #output: va
我想了解数字( double )在 bash 中是如何表示的,以及当我在 bash 中以十六进制格式打印数字时会发生什么。 根据 IEEE 754 标准,double 应由 64 位表示:52 位(1
我试图在 bash -c "..." 命令中获取 bash 脚本,但它不起作用。 如果我在 bash -c "..." 之外运行命令,它会起作用。 我需要使用 bash -c "..." 因为我想确保
如何检测我的 bash shell 中是否加载了 bash 补全包?从 bash-completion 的 2.1 版(包含在 Debian 8 中)开始,除了 BASH_COMPLETION_COM
我的 bash_profile 中有一个投影函数。现在我试图从 bash 脚本中调用这个函数,但是我得到了一个未找到的错误。如何使投影函数对 bash 脚本可见? 最佳答案 必须导出函数 export
我正在编写一个 bash 脚本,它接受许多命令行参数(可能包括空格)并通过登录 shell 将它们全部传递给程序 (/bin/some_program)。从 bash 脚本调用的登录 shell 将取
当我创建一个新的 bash 进程时,提示符默认为一个非常简单的提示符。我知道我可以编辑 .bashrc 等来更改它,但是有没有办法使用 bash 命令传递提示? 谢谢! 最佳答案 提示由 PS1、PS
好的,我希望这个问题有一定道理,但是 bash shell 和 bash 终端之间有什么区别?例子。当我第一次打开终端时,会提示我当前的目录和用户名。在终端窗口标题中显示 -bash- ,当我键入 e
我是 SBCL 的新手,我正在尝试从 bash 终端运行存储在文本文件中的 Lisp 脚本。 这是我在文件开头写的内容 http://www.sbcl.org/manual/#Running-from
我知道我们可以在 bash 中使用将十六进制转换为十进制 #!/bin/bash echo "Type a hex number" read hexNum echo $(( 16#$hexNum ))
我正在尝试在 bash 脚本中自动完成文件夹名称。如果我输入完整的文件夹名称,一切正常,但我不知道如何自动完成名称。有什么想法吗? repo() { cd ~/Desktop/_REPOS/$1 }
我想检查远程网站上的一些文件。 这里是bash命令生成计算文件md5的命令 [root]# head -n 3 zrcpathAll | awk '{print $3}' | xargs -I {}
是否有任何内置函数可以使用 bash shell 脚本从给定日期获取下周日(下周一、下周二等)?例如,2014 年 9 月 1 日之后的第一个星期日是什么时候?我预计 2014 年 9 月 7 日。
我一直在尝试根据表格重命名一些特定文件,但没有成功。它要么重命名所有文件,要么给出错误。 该目录包含数百个以长条形码命名的文件,我只想重命名包含模式 _1_ 的文件。 例子 barcode_1_bar
bash 中有没有办法用变量的内容替换文本文件中的占位符? 例如,我想发送一封电子邮件通知,如下所示: Dear Foo, Alert: blah blah blah blah blah blah
我有一个 bash 脚本,它在某些字符串上附加了一个重音字符,导致它失败,我找不到这些字符在哪里或如何进入那里。 这是一些示例输出: mv: cannot move â/tmp/myapp.zipâ
这个问题在这里已经有了答案: How do I place stdout on edit line? (1 个回答) Can a bash script prepopulate the prompt
我是一名优秀的程序员,十分优秀!