LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-19-2020, 08:42 AM   #1
daweefolk
Member
 
Registered: Sep 2009
Distribution: raspbian
Posts: 94

Rep: Reputation: 15
connect to running screen instance on ssh connect, otherwise start one?


I often ssh into my machine, and then run 'screen -r' to connect to my (usually) running screen instance and pick up where i left off.
However, i feel it's tedious.
My question is, where (like, .bashrc or wherever else) could I put a script to first check for a running screen instance; then if one exists, connect to it.
I'm ok with building the script, I just don't know where to put it.
 
Old 02-19-2020, 09:24 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,345
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
It's rather easy:

Code:
ssh -t daweefolk@server.example.com 'screen -d -R'
The -t is needed to force the pseudo-terminal allocation needed for an interactive shell.

If you are trying to attach to a specific screen session then include the -S option.

Edit: be sure to see the RemoteCommand and the RequestTTY directives for ssh_config.

Last edited by Turbocapitalist; 02-19-2020 at 09:30 AM.
 
Old 02-19-2020, 09:49 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,345
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
The existence and use of ~/.ssh/config might be a little obscure so here is an example of what can go in it:

Code:
Host sss1
	Hostname server1.example.com
	User daweefolk
	IdentitiesOnly yes
	IdentityFile ~/.ssh/sss1_ed25519
	RemoteCommand screen -d -R -S firstsession
	RequestTTY yes

Host *
	PermitLocalCommand yes
	ServerAliveCountMax 3
	ServerAliveInterval 60
That will allow you to connect just by typing "ssh sss1" and nothing more, all the settings will be made automatically based on that shortcut.

The full set of configuration directives can be found in "man ssh_config". Note the lack of a "d" there. Most run-time arguments have corresponding configuration directives. The client goes on a first-match basis so the Host patterns must have the more host-specific settings first and the most general settings last.
 
Old 02-19-2020, 10:11 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,741

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
My guess would be to call it from .bashrc, if you want it to run every time you login...as when you su - to that user from a running session...but what do I know?

An alternative I use is to write the script with a name I can invoke with a couple of keystrokes by invoking the history.
For example: I have a script that scans my spamtraps named ckspam.sh. After login, I type !ck to invoke the script. Most of the time, it's also the last command I've used on a session, so up-arrow, enter will also re-execute. The latter probably wouldn't apply in your case, but if you had a unique-enough script name (sz.sh) you could invoke it with !sz anytime you wanted.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot connect to existing nam instance. Starting a new one... Failed to start animator: class Animator: constructor failed: unmatched open abidsyed4u Linux - Wireless Networking 1 06-20-2019 02:38 AM
Can't ping or ssh to the cirros instance of the openstack instance visuharsha Linux - Networking 0 12-21-2017 01:44 PM
Postfix multiple instance only default instance work Dante K Linux - Server 0 09-05-2017 09:45 AM
Which RPM distro (rhel based or otherwise) to start learning if coming from debian? slacker_ Linux - Distributions 16 03-13-2015 12:00 PM
[SOLVED] Switch to instance instead of creating a new instance of a program javascriptninja Linux - Newbie 8 01-28-2012 03:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration