'Opening multiple gnome terminals from bash and execute sudo services

for my measurements on a Ubuntu OS I need to open in total 8 terminal and run services/commands that requires sudo. So the idea is to do that in a bash script.

What I want: call "sudo ./init.sh" ones, enter sudo password and then all 8 terminals should open parallel and execute the services/commands without any further sudo password request.

What I tried: (example with 2 terminals)

user:~/myFolder$ sudo ./init.sh

#!/bin/bash
gnome-terminal -- ptp4l -i enp3s0f2s -P -2 -s -m -q
gnome-terminal -- phc2sys -a -rr -q -m

that gave me an error since gnome-terminal should not be called with sudo.

user:~/myFolder$ sudo ./init.sh

#!/bin/bash
sudo -u $SUDO_USER gnome-terminal -- ptp4l -i enp3s0f2s -P -2 -s -m -q
sudo -u $SUDO_USER gnome-terminal -- phc2sys -a -rr -q -m

That give me an error since both services need sudo

user:~/myFolder$ ./init.sh

#!/bin/bash
gnome-terminal -- sudo ptp4l -i enp3s0f2s -P -2 -s -m -q
gnome-terminal -- sudo phc2sys -a -rr -q -m

no error but here I need to enter sudo password for each terminal



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source