Score:0

Execute background script.sh as admin in $HOME directory

jp flag

I am trying to run a startup script in an ubuntu environment (Ubuntu 20.04) which would run as admin from the home directory once the machine has restarted after PXE deployment, like a normal sudo user. The user is "ubuntu". (the constraint is that it needs to be executed as admin in the home directory) So during the deployment, I download the sh script from my ftp server, and I make it executable like this:

cd /home/ubuntu
wget ftp://my_server_ip/common/preseed/postConf.sh
chmod 755 /home/ubuntu/postConf.sh

I make it run at startup like this (using the init.d folder and making symbolic link to it in the rc2 folder):

cd /etc/init.d
wget ftp://my_server_ip/common/preseed/init.sh
chmod 755 /etc/init.d/init.sh
sed -i -e 's/\r$//' init.sh
ln -s /etc/init.d/init.sh /etc/rc2.d/S99init

The postconf.sh file installs a maas Setup 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:superpowers
...

The init.sh file looks like this:

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

Once the installation is finished, the scripts are copied correctly in the /home/ubuntu and in the /etc/init.d folders.

The problem is that it seems not to run correctly because I see that the maas software is installed by typing apt list --installed | grep maas etc. but the setup is not installed correctly (configs fail etc.). When I just execute on the other hand the postconf.sh file in sudo manually:

/home/ubuntu/postconf.sh

it works without any problems. Any Ideas how to make it work like a sudo user from the home directory? Thanks a lot

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.