Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 2151  / 3 Years ago, sat, october 16, 2021, 9:28:13

My Goal:
Execute a shell script after login which runs with root privileges and takes the appropriate user input from a terminal window to determine its next course of action.


The Script:
The script I'm running tells the user that a second script will automatically be initiated in 60 seconds unless the user enters s or S. A countdown displays the remaining time. If the user entered the correct exit command, the script ends and the system carries on as normal. If nothing valid is entered in 60 seconds, then the second script is initiated. The second script needs to also be executed as root (hence why the first one must be initiated as root), since it has a watchdog which executes a hard reboot of the system in the event of a hardware failure.


Bash Script:


#!/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 "
........."$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

Purpose:
This script is meant to restart a mining program after the watchdog restarts the system in the event of a GPU failure. The user prompt gives me time to stop the miner from executing after login if I want to use the system as normal. I'm usually away from the system, so this is pretty important to keep the mining operation going. GPU failure is fairly common after running 2 + days (despite underclock, undervolt, ect.) for one of my cards as she's getting very old at this point (over 6 yrs).


I've done a good amount of research and have yet to find a solution that has worked for me. Most focus on executing a script which requires no external input or terminal window. If it does, then it's only a few commands. I need something that launches a terminal window so I can see the prompts and the countdown. Additionally, I need the terminal window to display the miner status after it launches.


What I've tried:

Initial Conditions: File is .sh, executable, and owned by root.



  1. Utilize systemd to execute the script as a .service file (nothing happened)

  2. Utilize crontab to execute the script at boot (nothing happened)

  3. Placing the script in profile.d (Lubuntu took longer to log in, though nothing happened)

  4. Editing the $HOME/.bashrc and $HOME/.profile files by adding ./AutoStartMiner.sh to the top of the files (resulted in a system hang)


Hopefully this shows I put in a good amount of effort and we can find a solution to this issue. Thanks!


More From » bash

 Answers
4

Alright, it's been a while, though I finally had some time to sit down and write out my solution. This was my first ever time writing a bash script so it was exciting.


I ended up utilizing a combination of 3 scripts, 2 of which were given root permissions in the sudoers file. Perhaps there is a better way to do this; however, I simply put the full path to the script.


sudo visudo

Then under the tab #includedir /etc/sudoers.d


userName ALL=NOPASSWD PATH/AutoStartMiner.sh, PATH/TeamRedMiner_Config.sh, Path/teamredminer

This would set the stage for the 3 scripts to work one after the other without issues.


To start start AutoStartMiner.sh in a terminal window, I utilized the gnome-terminal emulator. For those implementing something similar, you'll need to install the emulator via:


sudo apt install gnome-terminal

I then utilized the LXQT configuration center and navigated to the session settings. In session settings I set the script StartTerminalASM.sh to execute after the system tray had loaded. This was accomplished by pressing add, giving the autostart function a name, pasting the whole path to the script, including the script itself, and then clicking the box that says load after system tray. It's important to note that this first script does not need to run with elevated privileges, so this method works. The autostart function of LXQT does not run scripts requiring elevated privileges automatically.


Here is the contents of the first script: StartTerminalASM.sh


#!/bin/bash
gnome-terminal --command="bash -c 'sudo /home/userName/Scripts/AutoStartMiner.sh; $SHELL'"

This script simply starts up the AutoStartMiner.sh in a gnome terminal. Since we added this script to the sudoers file earlier, the sudo command executes without a password prompt.


Here is the second script: 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 "
........."$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

So this essentially does as I described in the original question. The only problem it has is that when the time drops below 10 seconds, for some reason, a 0 appears in the 1s place and the numbers count down in the 10s place (e.g. 10, 90, 80, 70, ..., etc). Probably something I don't fully understand about the printf statement that I made. Similarly above, the sudo command here doesn't ask for a password due to the line created in sudoers.


Finally it executes a third script: TeamRedMiner_Config.sh


This script simply establishes the parameters for the miner to run. It also executes the miner with sudo, hence why we add a third command to sudoers to ensure no prompt is required to start the miner itself.


So unless the user prompts the system to stop to miner from starting, the miner will execute without any input. This is extremely useful as it saves me a ton of headache if the kernel happens to crash. I simply press the power button and boom, the miner is running one min later. If I really wanted to automate it, I could make a little servo to push the button or set up wake on lan.


That all being said, it's onto the latest problem. After updating to Linux kernel 5.11.0-27-generic, my wireless adapter refuses to connect to my network, though at least it's recognized by the system.


Thanks for the input everyone and I hope this helps other beginners like me one day.


[#1314] Sunday, October 17, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
torlim

Total Points: 408
Total Questions: 113
Total Answers: 110

Location: Estonia
Member since Wed, May 27, 2020
4 Years ago
torlim questions
Mon, Jun 13, 22, 09:32, 2 Years ago
Thu, Aug 4, 22, 05:08, 2 Years ago
Fri, Aug 20, 21, 05:39, 3 Years ago
Mon, Dec 5, 22, 18:26, 1 Year ago
;