Using bash with screen in an alias [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 months ago.
Improve this question
I have a screen session that is always running in a separate user (for a minecraft server). Each time I want to attach to this screen, I use su -c 'screen -r mc-1.18.2' minecraft to attach back to this screen.
I wanted to make an alias for this command just so I didn't have to type it out or go up in the command history each time. I added alias mcconsole="su -c 'screen -r mc-1.18.2' minecraft" to my ~/.bash_aliases folder. My other aliases work just fine but when I run mcconsole I am left with a brand new screen session. I am not sure where the hiccup is when attaching to the screen session (which is running as I can attach to it normally).

maybe try to invert simple and double quotes, something like:
alias mcconsole='su -c "screen -r mc-1.18.2" minecraft'
or escape double quotes inside:
alias mcconsole="su -c \"screen -r mc-1.18.2\" minecraft"

Sounds like something screwy is happening with quoting when using the alias. Try using a shell function instead.
mcconsole() {
su -c 'screen -r mc-1.18.2' minecraft
}

Related

How to programmatically trigger actions when last Linux terminal is exited/killed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I've modified my .bashrc to mount directories when I start first terminal after login.
If mount point still exists when I start new terminal nothing is done.
Now I want to add a bit of code when last linux terminal is closed/exited (e.g. umount those directories, etc..)
Also there is this not-so-intelligent way of discovering how many terminal instances are still running:
ps -au | grep "bash" | grep "grep" -v -c
I'm running Ubuntu 20.04. and I'm using bash shell.
Questions:
Is there a file which is "triggered" on terminal exit just like .bashrc is on terminal startup? I've tried messing around with .bash_logout but it doesn't seem to do anything in my case (echo, touch..)
Is there another way to do what I'm trying to achieve which doesn't include file from question #1 (if such file even exists)?
You can get it done with the help of trap which is a shell builtin.
For example if you want to clear a folder on running exit command in bash >
trap "rm /cache/*" EXIT
The syntax should be like trap <command> <SIGNAL>
Just put this in the bottom of ~/.bashrc with your desired command and it should run before the terminal is killed.
Try trap --help to know more.

Automate SSH login and programmatically induce remote, interactive shell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm working from Mac OSX and am trying to create a script that will ssh user#host and subsequently change the working directory (cd) from within the virtual server.
Googling indicated that ssh -t user#host "command goes here" would allow for this, but when I enter cd /my/path it says that there is no such directory. It also forces me to exit the server. What is the best, simplest way to accomplish this?
I'm assuming that the desired end result is a remote interactive shell, in the desired target directory, which you can interact with from your keyboard. If not, please clarify the question.
Force a TTY with the -t argument to ssh and spawn an interactive shell with an explicit invocation of bash -i:
ssh -t user#host "cd /my/path && exec bash -i"
Scripting/automating the authentication is road already well-traveled (and, thus, duplicative); see existing questions such as Bash: controlling SSH for assistance on that matter.

Change dir after 'sudo -i', in one command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
Is there a way to switch dir after doing sudo -i (in one command)?
We use lots of ssh connections on work, and it is a pain to manually set them up when you reboot your pc. So I'm trying to make aliases like ssh remote-dev -t 'sudo -i; bash -l', which connects me to a machine and makes me root there (it is required to do that way; because of Kerberos we can't directly ssh root#...)
So what I would like to do now is expanding the above command in a way that I can also tell it to switch to a specific dir after the sudo, or open a specific file, or tail a logfile or something... Is that possible?
-- edit: Of course you could tell it to do some command everytime someone logs in via ssh. But this is a bad option, because only I want to have these commands to be executed; When other people connect to this machine, they probably want different commands.
-- edit: Sorry I posted it here, did not realize it is offtopic in stackoverflow
Try
sudo -i bash -c "cd /path/to/dir; exec bash"

How to properly format this command: Exec=a=$(service httpd restart 2>&1) && notify-send "$a" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I am trying to figure a way to
fix this code so that Exec= line can work properly.
[Desktop Entry]
Name=a
GenericName=a
Exec=a=$(service httpd restart 2>&1) && notify-send "$a"
Terminal=false
Type=Application
StartupNotify=true
MimeType=text/plain;
Icon=/root/Desktop/111.svg
this is basically a panel shortcut i created by copying another app's shortcut.
i tested this and it worked great with..
Exec=notify-send 'a'
in other words it does execute a command.
but i am trying to stuff 2 commands in this line .. as seen above.
You need to start a wrapper shell, like this:
Exec=bash -c "notifiy-send \"$(command)\""
Note: Don't miss the double quotes around $(command)
What makes in your case:
Exec=bash -c "notifiy-send \"$(service http restart)\""
Or if the user need to authenticate use gksu:
Exec=bash -c "notifiy-send \"$(gksu service http restart)\""

run same command on multiple tabs of a terminal gnome-terminal [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
Every day I run same ssh(succeeded by entering password) on 3 or more terminals. Is there any way to run same command parallely on all the terminal tabs opened on gnome-terminal. It can be ssh or cd any any other command. I know this feature is there in Konsole(from KDE) but not in gnome-terminal.
Thanks
To open tabs in gnome-terminal with a predefined commands (i.e. ssh server1, ssh server2, ...) you can use a command
gnome-terminal --tab -e "ssh server1" --tab -e "ssh server2"
And, it was previously mentioned, you should use key authentication to not to enter your passwords every time, but if you can't, change your ssh command to
sshpass -p 'mysecretpass' ssh server1
In addition to this, you can create a script file with the sshpass command and pass it with the -e option while creating the terminal.. this would further simplify things.
Terminator terminal supports the function you are looking for. You can find more information here. I am using this program every day. It is a very useful piece of software.

Resources