1

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!

8
  • 1. 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, 2021 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. Jul 24, 2021 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, 2021 at 0: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. Jul 25, 2021 at 0:46
  • See also this AskUbuntu link, which might be more relevant to your case.
    – sudodus
    Jul 25, 2021 at 0:48

2 Answers 2

2

Autostart system where part runs with elevated permissons

The following method should work for you to get a shield start automatically, when your computer starts. Please modify the template files from this answer to match your user ID and the name of the watchdog program file.

Note: 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).

~/.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

See this link, that describes how to make a program start automatically when the computer reaches the desktop at boot with 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 should call your watchdog instead of parted
  • You may also modify the grace period tme from 10 to 60 (seconds).

/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

The last line (and a blank line) are added to the default file. Please modify it for your user ID and the name of your watchdog program.

The following links explain how to edit the sudoersfile with visudo:

Permissons and ownership

-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

This worked for me when I tested. You may want to modify the permissions and ownership. But it should not be risky when you limit the NOPASSWD feature to the watchdog program.

The shield is fullscreen and looks like this

When you interrupt it during the grace period before mining:

enter image description here

When you let it start mining, simulated here with parted and dots (.....), and finally stop the mining to use the computer for other purposes:

enter image description here

You may want to modify the text size of the xterm window from -fs 16.

2
  • Hi sudodus, thanks for taking the time to answer the question so thoroughly. In my spare time, I managed to find a different solution which I will post below when I have some time. Additionally, I'm eager to try your solution on another machine so I can learn. Thanks! Aug 7, 2021 at 2:09
  • You are welcome, and I am looking forward to your solution :-)
    – sudodus
    Aug 7, 2021 at 6:34
1

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 "\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

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.

2
  • 1
    Running the script in a terminal window seems like a poor design. If you want something to run concurrently, start it as a background job with output to a log file, and attach to that log file from wherever you happen to be logged in to see what it's doing.
    – tripleee
    Aug 30, 2021 at 8:34
  • The terminal window was to allow for user input. That's currently the only way I know how to allow a user to interact with a script. Though if there is another way I'm definitely open to learning. Aug 30, 2021 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.