Bash script with multiple sudo commands in tabs - linux

I have a bash script that opens a mate-terminal with 3 tabs, each tab having a command that requires sudo. The problem is that I have to put in my sudo password on each of the 3 tabs, which is not ideal. Can I make it so I only have put in my sudo password once? Basically, I want to double click the shell script, put in my sudo password once, and have all 3 commands execute. How can I do this? This script looks like this:
#!/bin/sh
mate-terminal \
--tab -e "sudo /sbin/mgetty -s 115200 -D /dev/ttyUSB0" \
--tab -e "sudo tail -f /var/log/mgetty/mg_ttyUSB0.log" \
--tab -e "sudo tail -f /var/log/messages"

Related

Gnome terminal doesn't recognise commands when started from startup-applications with .sh file

I am trying to use an .sh script to start a terminal when ubuntu boots. Gnome-terminal successfully starts up but when commands start executing such as (roscore, roslaunch or rosrun) it displays an error as follows: "bash: roslaunch command not found". Is there any way to fix this problem or any other way to start launch files of ROS with a visible terminal at start?
This is how my .sh file looks like
#!/bin/bash
gnome-terminal --geometry=40x40 \
--tab --title="roscore" -e "bash -c \"source ~/.bashrc;roscore;exec bash\"" \
--tab --title="navigation" -e "bash -c \"sleep 38;roslaunch navigation.launch;exec bash\"" \
--tab --title="robot" -e "bash -c \"sleep 28;roslaunch robot_config.launch;exec bash\""
Are you sourcing ros setup.bash file (source /opt/ros/$ROS_DISTRO/setup.bash)? If not just add it to ~/.bashrc file

Commands don't echo after sudo as another user

I have a single command to ssh to a remote linux host and execute a shell script.
ssh -t -t $USER#somehost 'bash -s' < ./deploy.sh
Inside deploy.sh I have this:
#!/bin/bash
whoami; # I see this command echo
sudo -i -u someoneelse #I see this command echo
whoami; # I DON'T see this command echo, but response is correct
#subsequent commands don't echo
When I run the deploy.sh script locally all commands echo.
How do I get commands to echo after I sudo as another user over ssh?
Had to set -x AFTER sudo as another user
#!/bin/bash
whoami;
sudo -i -u someonelese
set -x #make sure echo on
whoami; #command echoed

How to "setup" screen using a bash script

I'm trying to write a bash script to create a screen (software) session with a specific set of windows, and cd to specific directories on each one.
Here is the script I have so far:
#!/bin/bash
killall screen;
screen -AmdS work;
screen -S work bash -c "cd myDir";
The problem is that I can't seem to change directories on that session. After running this script, I run $ screen -r and the current directory is still my default directory (~/).
(I've tried changing the cd command to touch myFile and the file is there after I run the script)
Try the following, it will open a new screen session with a bash which will change the directory and open a new bash with this directory as current:
screen -S work bash -c 'cd myDir && exec bash'
Adding -d -m to run it in detached mode. And after reattaching you will be in myDir:
screen -S work -d -m bash -c 'cd myDir && exec bash'
Better solution
The following code will create a detached screen with 3 screens each running myCommand1/2/3 in directory myDir1/2/3.
cd myDir1
screen -S work -d -m
screen -S work -X exec myCommand1
screen -S work -X chdir myDir2
screen -S work -X screen
screen -S work -X exec myCommand2
screen -S work -X chdir myDir3
screen -S work -X screen
screen -S work -X exec myCommand3
cd -
Note the last cd - that will return you back to your original working directory.
Finally just use screen -r work to attach your running screen session.
You can save the command line you want to run (including the final newline) into a register and paste it into the screen input:
screen -S work -X register c $'cd myDir\n'
screen -S work -X paste c

Linux shell scripting for different tabs

I want a script,which after executing will open multiple tabs and a specified command will run on every tab.Basically that command is ssh i.e. for connecting to other machines.
you can try something like this;
gnome-terminal --tab --title "server1" -x bash -c "ssh -t user1#host1 'nohup yourCommand'" --tab --title "server2" -x bash -c "ssh -t user2#host2 'nohup yourCommand'"

How to execute a file in separate gnome terminal in linux

I want to execute a C program in separate terminal, I've tried this command,
gnome-terminal -x ./test
and
gnome-terminal -e test
But it opens a new terminal and before giving me output, it just vanish.
How could I solve this issue using gnome-terminal?
Thanks in advance
This might be what you search:
gnome-terminal -e "bash -c \"!!; exec bash\""
or (shortly):
gnome-terminal -x sh -c "!!; bash"
It opens gnome-terminal with your last command (!!) executed and it stays open with the command output in the shell, even with an interactive command like top or less...
In your case its:
gnome-terminal -e "bash -c \"./test; exec bash\""
or
gnome-terminal -x sh -c "./test; bash"

Resources