Score:0

Execute visible shell script as sudo at login in the home directory

jp flag

I have a script which works fine when I put it manually in the /home/ubuntu directory and execute it manually with the sudo rights (It must be run from the $HOME directory).

The script path is:

/home/ubunt/script.sh

..and looks like this

#!/bin/bash
sudo add-apt-repository -y ppa:maas/3.0-next
sudo apt update
sudo apt install -y maas jq markdown nmap traceroute git curl wget zfsutils-linux cloud-image-utils virtinst qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils whois
sudo maas createadmin --username ubuntu --password password --email [email protected] --ssh-import gh:wt-b
...

I want to make it run at login (once) and I need to see what is happening (a visual cmd line).

I've already checked the options with the /etc/init.d folder (and also the /lib/systemd/system folder where I activate the service via systemctl), calling the script via following command line

#!/bin/bash
gnome-terminal -e '/home/ubuntu/script.sh'

or without cmd interface visible

#!/bin/bash
source /home/ubuntu/script.sh

of but it fails and does only install some components.

Any ideas? Thanks

Score:2
cn flag

Strange need, to automatically install packages again and again automatically during login. Yet, you may have good reasons.

You can use "Startup applications" to automatically start up a terminal after login, which then starts your script.

  1. Change your script and remove sudo. You will run the entire script as root.
  2. Autostart Gnome Terminal, the default terminal emulator of the Ubuntu desktop as following: gnome-terminal -- sudo <path-to-your-script>

On login, the terminal will launch and you will see the prompt to enter the sudo password. After entering the password, the script will run and output will appear in the terminal.

When the script has finished, the terminal will close. To prevent that from happening, you could add the command "bash" to keep a shell (and the terminal emulator) open, or use a prompt such as

read -rsn1 =p $'Press a key to continue\n'
Andy McRae avatar
jp flag
Hi thanks for your answer. So I made a sh.desktop file where I put inside the code [Desktop Entry] Type=Application Exec="/home/ubuntu/script.sh" Hidden=false NoDisplay=false X-GNOME-Autostart-enabled=true Name[en_IN]=Maas Name=MaasSetup Comment[en_IN]=Maas Comment=MaasSetup and I logout and login again but this doesnt do anything. I checked the paths and they are correct. I think startup tasks only work with .desktop files and not with sh files correct? Greetings
vanadium avatar
cn flag
You can launch "Startup Applications" to add a startup command. That indeed will create a .desktop launcher in the autostart folder. With respect to the command needed, reread my answer carefully.
Andy McRae avatar
jp flag
Thanks. If I could get one thumbs up It would be great.
Score:0
jp flag

Okay so I found a solution with the startup task mentioned by vanadium.

But I thought I would make it a bit more explicit.

I use 3 Files:

Two are located int the /home/ubuntu/ folder

  1. /home/ubuntu/init.sh

And which contains

#!/bin/bash
gnome-terminal -- sudo '/home/ubuntu/script.sh'
  1. /home/ubuntu/script.sh

which contains the actual code I want to execute

#!/bin/bash
sudo add-apt-repository -y ppa:maas/3.0-next
sudo apt update
sudo apt install -y maas jq markdown nmap traceroute git curl wget zfsutils-linux cloud-image-utils virtinst qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils whois
sudo maas createadmin --username ubuntu --password password --email [email protected] --ssh-import gh:wt-b

...

And then We need to enable a startup application by putting a config file in the /etc/xdg/autostart folder 3) /etc/xdg/autostart/conf.desktop

Which contains following infos:

[Desktop Entry]
Version=1.0
Name=LordofSpaghettis
Comment=Spaghettis are good
Exec=/home/ubuntu/init.sh
Path=/home/ubuntu/
Terminal=true
Type=Application
X-GNOME-Autostart-enabled=true
Categories=Utility;Development

Then when I restart the machineof logoff and login again, I get a prompt to ask the sudo password. And then everything works fine. Thanks to vanadium for the help.

raj avatar
cn flag
raj
You don't need to manually create files in step 3). As @vanadium said, open "Startup Applications" from System Tools and add `/home/ubuntu/init.sh` to the list of programs that start automatically with the session. However, as you have already done this manually, there's no need to do this again :)
Andy McRae avatar
jp flag
Actually I need to do it like that because it is a part of an automation process where we deploy multiple machines. And I cannot start the script.sh file directly from the .desktop file because the .desktop file can apparently not start gnome-desktop directly. hope it helps
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.