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
CodePudding user response:
Easier way is to use tmux
(see) to do it.
You can do initial sudo in terminal. Then launch the tmux with the commands you need to run in parallel (you can use the sample command below and add it in script).
tmux new-session -d -s multi-run 'your command'
tmux splitw -h -p 66 'your command'
tmux splitw -h -p 50 'your command'
tmux selectp -t 1
tmux splitw -v -p 50 'your command'
tmux selectp -t 3
tmux splitw -v -p 50 'your command'
tmux attach-session -t multi-run