Jump to content

Lubuntu 20.04 How To Auto Run A Shell Script After Login With Root Privileges

Hello All!

 

I'm currently having trouble figuring out how to run a shell script that executes with root privilege after the arriving at the desktop in Lubuntu 20.04. Unfortunately, the built in Autostart functionality in the LXQT configuration manager doesn't execute with elevated privileges. Additionally I also tried putting the shell script in etc/profile.d directory; however, all this did was make Lubuntu take longer to get to the desktop. I have made the file .sh, executable, and owned by root.

 

Essentially, I want the terminal window for the script to pop up after I arrive at the desktop to prompt me with options. The script waits 60 seconds for user input before automatically executing another script. The reason it has to have root privilege is because the second script getting automatically executed needs to have root privilege as well. That script contains a watchdog which needs root privilege to trigger a hard reboot in the event of a hardware failure while I'm away from the computer.

 

Hope someone can guide me in the right direction because I'm currently out of my depth. Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Turns out its trickier than you'd think to run an interactive script at login. The method you used apparently doesn't work well for scripts that require user interaction.

 

The best way to do it in your case is to place the script inside...

/etc/profile or 
$HOME/.profile or 
$HOME/.bash_profile

...and to make sure the script begins with #!/bin/bash otherwise you might also run into issues.

 

You could also use...

crontab -u root -e

... then add the script however AFAIK there's no way to cron a job at login.

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

@Master Disaster As I'm coming to find out, this is incredibly difficult.

I followed your instructions by adding #!/bin/bash to the script. I then modified both of the following:

$HOME/.profile and 
$HOME/.bashrc

$HOME/.bash_profile wasn't present on the system (usually a Mac file apparently); however, $HOME/.bashrc apparently is used for non login shells. I used it as a testing mechanism while currently logged in. I added the following lines to the top of both above mentioned files:

 

echo "Did something"

./Test.sh

 

Test.sh consists of the following:

"Type s key"

input="A"

read input

if [ "$input" = "s" ]; then

echo "success"

else

echo "other key"

fi

 

After starting a terminal window I was prompted with "Did something" and the script ran as expected. Then I ran into issues. I could no longer launch the terminal with ctrl shift T. Then, to test if the script initiated and prompted me on login, I tried a reboot. I would arrive at the login screen, where I would enter my password. After entering it correctly, the entire system would hang. I could move the mouse, though the screen was stuck on login. It was not updating either since the clock was stuck at the initial time I finished entering my password.

 

I then had to go into the recovery mode and edit the two files. After removing my new commands from both files, the system booted properly.

 

Hopefully we can figure something out. Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Have you tried a systemd-based approach instead? Systemd units allow to start scripts at boot, to start them as a regular user or as root, to start them not as daemons, to relaunch if needed, and, crucially, not to crash the entire system if one of the units fails.

Link to comment
Share on other sites

Link to post
Share on other sites

@Alexeygridnev1993 I did write a .service file utilizing systemd, setting it to the graphical.target; however, nothing happened after the gnome desktop GUI displayed 😕

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, XXX_BlueFire_XXX said:

@Alexeygridnev1993 I did write a .service file utilizing systemd, setting it to the graphical.target; however, nothing happened after the gnome desktop GUI displayed 😕

Did you use --no-daemon flag at ExecStart?

Link to comment
Share on other sites

Link to post
Share on other sites

On 7/25/2021 at 12:50 AM, Alexeygridnev1993 said:

Did you use --no-daemon flag at ExecStart?

I did not see that flag unfortunately; however, I did find a solution late Saturday night. I will post it soon when I have time to throughly write a response. Utilized a combination of the LXQT autostart functionality and three separate bash scripts to accomplish the goal stated in the initial post. Really felt good seeing that terminal window come up after start. gnome-terminal, a damn miracle worker!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×