我的目标:登录后执行一个 shell 脚本,该脚本以 root 权限运行并从终端窗口获取适当的用户输入以确定其下一步操作。
剧本:我正在运行的脚本告诉用户,除非用户输入 s 或 S,否则第二个脚本将在 60 秒内自动启动。倒计时显示剩余时间。如果用户输入了正确的退出命令,脚本将结束,系统将正常运行。如果 60 秒内未输入任何有效内容,则将启动第二个脚本。第二个脚本也需要以 root 身份执行(这就是为什么第一个脚本必须以 root 身份启动的原因),因为它有一个看门狗,可以在发生硬件故障时执行系统的硬重启。
Bash 脚本:
#!/bin/bash
#Prompt user with information.
echo "Miner will begin in 60 seconds."
echo "Press s + enter to stop the process and resume normal operation."
#Declare variables.
input="a"
let seconds=60
#Await user input and countdown.
while [ $seconds -gt 0 ]; do
printf "\r........."$seconds
read -s -t 1 input
let "seconds-=1"
if [ "$input" = "s" ] || [ "$input" = "S" ]; then
break
fi
done
echo
#Initiate user selection
if [ "$input" = "s" ] || [ "$input" = "S" ]; then
echo "Resuming normal operation."
sleep 2
else
echo "Starting miner."
sleep 2
./TeamRedMiner_Config.sh
fi
目的:此脚本用于在 GPU 发生故障时,在看门狗重新启动系统后重新启动挖矿程序。如果我想正常使用系统,用户提示会给我时间在登录后停止矿工的执行。我通常不在系统旁,因此这对于保持挖矿操作的进行非常重要。我的一张卡在运行 2 天以上后(尽管有降频、降压等)出现 GPU 故障的情况相当常见,因为它现在已经很旧了(超过 6 年)。
我做了大量的研究,但还没有找到对我有用的解决方案。大多数都集中在执行不需要外部输入或终端窗口的脚本上。如果有的话,那么只需要几个命令。我需要一个可以启动终端窗口的东西,这样我就可以看到提示和倒计时。此外,我需要终端窗口在启动后显示矿工状态。
我尝试过的方法:
初始条件:文件为.sh、可执行且由root拥有。
- 利用 systemd 将脚本作为 .service 文件执行(什么也没发生)
- 利用 crontab 在启动时执行脚本(什么也没发生)
- 放置脚本
profile.d
(Lubuntu 花了更长时间登录,尽管什么也没发生) - 通过在文件顶部添加内容来编辑
$HOME/.bashrc
和文件(导致系统挂起)$HOME/.profile
./AutoStartMiner.sh
希望这表明我付出了很多努力,我们可以找到解决这个问题的方法。谢谢!
答案1
自动启动系统,其中部分内容以提升的权限运行
以下方法应该可以让你shield
电脑启动时自动启动。请修改此答案中的模板文件以匹配你的用户 ID 和看门狗程序文件的名称。
注意:我建议您不要将整个脚本作为 运行root
,而只运行看门狗(在第二个脚本中具体使用 sudo 调用)。这意味着您应该允许执行NOPASSWD
看门狗(通过 设置visudo
)。
~/.config/autostart/shield.desktop
[Desktop Entry]
Name=shield at tester
Comment=Shield only for testing
Exec=/home/tester/bin/shield
Terminal=false
Type=Application
StartupNotify=true
X-GNOME-Autostart-enabled=true
看此链接,描述了如何使用 Lubuntu 20.04.x LTS 使计算机在启动时到达桌面时程序自动启动。
./bin/shield
#!/bin/bash
xterm -display :0 -fullscreen -fa default -fs 16 -e bash -c \
'
tme=10 # grace period in seconds before mining starts
echo "Stop? (s/q) "
while [ "$tme" -gt "0" ]
do
echo -n "$tme "
tme=$((tme-1))
read -t1 -n1 ans
if [ "$ans" == s ] || [ "$ans" == S ] \
|| [ "$ans" == q ] || [ "$ans" == Q ];then
echo " - the computer is yours"
sleep 2
exit
fi
done
echo " - auto-action, mining starts"
echo "$ sudo -H parted -ls # demo with program needing sudo"
sudo -H parted -ls
while true
do
echo -n "."
read -t1 -n1 -s an2
if [ "$an2" != "" ];then
echo " - mining stopped, the computer is yours"
sleep 3
exit
fi
done'
sudo
应该打电话给你的看门狗,而不是parted
- 您还可以将宽限期
tme
从 10 秒修改为 60 秒。
/etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
%tester ALL=NOPASSWD: /usr/sbin/parted
最后一行(和一个空白行)被添加到默认文件中。请将其修改为您的用户 ID 和看门狗程序的名称。
以下链接说明如何使用来编辑sudoers
文件visudo
:
权限和所有权
-rwxrwxr-x tester/tester 649 2021-07-26 15:14 ./bin/shield
-rwxrwxr-x tester/tester 181 2021-07-26 14:47 ./.config/autostart/shield.desktop
-r--r----- root/root 795 2021-07-26 14:19 etc/sudoers
NOPASSWD
在我测试时,这种方法对我有用。您可能想要修改权限和所有权。但当您将该功能限制在看门狗程序上时,应该不会有风险。
全屏shield
显示,看起来像这样
当您在挖矿前的宽限期内中断它时:
当你让它开始挖矿时,这里用和parted
点(.....
)模拟,最后停止挖矿以将计算机用于其他用途:
您可能想要修改xterm
窗口的文本大小-fs 16
。
答案2
好吧,时间已经过去很久了,不过我终于有时间坐下来写下我的解决方案了。这是我第一次编写 bash 脚本,所以很兴奋。
我最终使用了 3 个脚本的组合,其中 2 个在 sudoers 文件中被赋予了 root 权限。也许有更好的方法来做到这一点;但是,我只是把完整的路径放到了脚本中。
sudo visudo
然后在标签下#includedir /etc/sudoers.d
userName ALL=NOPASSWD PATH/AutoStartMiner.sh, PATH/TeamRedMiner_Config.sh, Path/teamredminer
这将为 3 个脚本的连续运行奠定基础,确保其不会出现问题。
为了在终端窗口中启动 AutoStartMiner.sh,我使用了 gnome-terminal 模拟器。对于那些实现类似功能的人,您需要通过以下方式安装模拟器:
sudo apt install gnome-terminal
然后,我利用 LXQT 配置中心并导航到会话设置。在会话设置中,我将脚本 StartTerminalASM.sh 设置为在系统托盘加载后执行。这是通过按添加、为自动启动功能命名、粘贴脚本的整个路径(包括脚本本身)然后单击显示系统托盘后加载的框来完成的。重要的是要注意,这个第一个脚本不需要以提升的权限运行,因此这种方法有效。LXQT 的自动启动功能不会自动运行需要提升权限的脚本。
这是第一个脚本的内容:StartTerminalASM.sh
#!/bin/bash
gnome-terminal --command="bash -c 'sudo /home/userName/Scripts/AutoStartMiner.sh; $SHELL'"
此脚本只是在 gnome 终端中启动 AutoStartMiner.sh。由于我们之前已将此脚本添加到 sudoers 文件中,因此 sudo 命令执行时无需输入密码。
这是第二个脚本:AutoStartMiner.sh
#!/bin/bash
# Prompt user with information.
echo "Miner will begin in 60 seconds."
echo "Press s + enter to stop the process and resume normal operation."
# Delare variables.
input="a"
let seconds=60
#Await user input and countdown.
while [ $seconds -gt 0 ]; do
printf "\r........."$seconds
read -s -t 1 input
let "seconds-=1"
if [ "$input" = "s" ] || [ "$input" = "S" ]; then
break
fi
done
echo
#Initiate user selection
if [ "$input" = "s" ] || [ "$input" = "S" ]; then
echo "Resuming normal operation."
sleep 2
else
echo "Starting miner."
sleep 2
sudo /home/userName/Documents/Crypto/Miners/TeamRedMiner/teamredminer-v0.8.4-linux/TeamRedMiner_Config.sh
fi
因此,这基本上就像我在原始问题中描述的那样。它唯一的问题是,当时间低于 10 秒时,由于某种原因,个位会出现 0,而十位的数字会倒数(例如 10、90、80、70,... 等)。可能是我不太理解printf
我所说的内容。与上面类似,sudo
此处的命令不会要求输入密码,因为 sudoers 中创建了一行。
最后执行第三个脚本:TeamRedMiner_Config.sh
此脚本只是设置了运行矿工的参数。它还使用 执行矿工sudo
,因此我们向 sudoers 添加第三个命令,以确保无需提示即可启动矿工本身。
因此,除非用户提示系统停止启动矿机,否则矿机将无需任何输入即可执行。这非常有用,因为如果内核发生崩溃,它可以为我省去很多麻烦。我只需按下电源按钮,一分钟后矿机就会运行。如果我真的想自动化,我可以制作一个小伺服器来按下按钮或设置局域网唤醒。
说了这么多,现在又要说最新的问题了。更新到 Linux 内核 5.11.0-27-generic 后,我的无线适配器拒绝连接到我的网络,尽管至少系统可以识别它。
感谢大家的意见,我希望有一天这能帮助其他像我一样的初学者。