2

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.

  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!

Zanna
  • 72,471
  • sudo crontab -e is 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.
  • – sudodus Jul 24 '21 at 22:40
  • I actually want to bypass the password entry in this instance because I may not be in front of the system itself when the script is executed. It's essentially restarting the miner after the system has encountered a critical error. In the case where I am in front of the system and intend to not start the miner, I want the terminal window to come up and display an option to stop the auto-initiation of the script. I thought about utilizing the LXQT autostart; however, I was under the impression it does not run as a script as root, which is very important in this situation. – XXX_BlueFire_XXX Jul 24 '21 at 23:47
  • If nobody will be able to take advantage of a security hole you can 1. use sudo -S and even enter the password in clear text in the shellscript. But it is a bad idea; 2. It is better to use visudo and 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 for visudo or some other relevant search string. – sudodus Jul 25 '21 at 00:33
  • Security on this machine isn't much of a concern; however, I personally like to keep things as secure as possible while accomplishing a goal. I'll look into visudo as a way to run a shell script as root with no password. This is a good step; however, I'm still lost on how exactly I would get the system to run the script after reaching the GUI desktop. – XXX_BlueFire_XXX Jul 25 '21 at 00:46
  • See also this AskUbuntu link, which might be more relevant to your case. – sudodus Jul 25 '21 at 00:48
  • See the link in my first comment. Autostart works that way for me in Lubuntu 20.04.x. – sudodus Jul 25 '21 at 00:50
  • Will do, I'll let you know how it goes moving forward utilizing the Autostart method. Perhaps it has changed in later versions of Lubuntu. – XXX_BlueFire_XXX Jul 25 '21 at 01:06
  • Comment on "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.": I suggest that you do not run the whole scripts as root, but only the watchdog (specifically called with sudo in the second script. This means that you should allow execution with NOPASSWD of the watchdog (set via visudo). – sudodus Jul 26 '21 at 12:40