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 "\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
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.
- Utilize systemd to execute the script as a .service file (nothing happened)
- Utilize crontab to execute the script at boot (nothing happened)
- Placing the script in
profile.d(Lubuntu took longer to log in, though nothing happened) - Editing the
$HOME/.bashrcand$HOME/.profilefiles by adding./AutoStartMiner.shto 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!
sudo crontab -eis the way to enter a cron job for root. But I don't know how to make that interact with a user; You need a full path to all programs (or specify the PATH variable in rontab; 2. You can use autostart instead. This example might help you create your autostart job. I think you should allow time for the user to type in the [user] password to get the sudo task working, because the script will (and should) probably ask for the password.sudo -Sand even enter the password in clear text in the shellscript. But it is a bad idea; 2. It is better to usevisudoand allow only one particular program or script to be executed with sudo without password. See this link. You can find many more tutorials if you search the internet forvisudoor some other relevant search string. – sudodus Jul 25 '21 at 00:33sudoin the second script. This means that you should allow execution withNOPASSWDof the watchdog (set viavisudo). – sudodus Jul 26 '21 at 12:40