You are not logged in.

#1 2006-05-31 22:24:45

twiistedkaos
Member
Registered: 2006-05-20
Posts: 666

Terminal Question

This is something that's been bothering me for quite some time. Say I am running a server that's console based(no gui) but it's urnning in the background, every once in awhile I want to bring it up so I can type a few commands in, is their a way to bring hidden cnonsole apps back up in the terminal?

Offline

#2 2006-05-31 22:32:02

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Terminal Question

'screen' is your friend.
you can detach from it, logout, login later, and then reattach.


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#3 2006-05-31 23:03:25

twiistedkaos
Member
Registered: 2006-05-20
Posts: 666

Re: Terminal Question

cactus wrote:

'screen' is your friend.
you can detach from it, logout, login later, and then reattach.

Sorry i don't understand what you mean, can you be more in detail?

Offline

#4 2006-05-31 23:08:47

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Terminal Question

Yup ... as cactus indicates screen is the solution.
Just for clarifications screen will act as a "program wrapper" so say you want to connect to a server over ssh, you can do :
screen -t remoteserver ( this gives your instance a name "remoteserver").
Screen starts and you can then issue the connect command say:
ssh you@the_remote_server
once you connect you press Ctrl+d to disengage screen (but remains active in the background).
An hour later you need to go to the server again, so you re-engage the  process with screen -r -t remoteserver
and you are back to the exact point you were before.
Naturally, this also works for programs working in the local machine not just for remote access.

Offline

#5 2006-05-31 23:16:33

twiistedkaos
Member
Registered: 2006-05-20
Posts: 666

Re: Terminal Question

Alright, so for example i can run:

screen -X foo -t foofoo

foo being the command I want to run, and foofoo being the title of the screen?

Offline

#6 2006-05-31 23:20:56

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Terminal Question

Yup.
Then to disengage you type Ctrl + a and then d ( I made a mistate in my first explanation)
Then when you want to go back you type: screen -r -t foofoo

Offline

#7 2006-05-31 23:32:47

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Terminal Question

As he said Screen is your friend, I use it on my everyday work wether on the server to run IRSSI and mp3player or the laptop for any thing i do, here's a little howto (VERY little one!)

First pacman -S screen lol
you start screen by invoking
<code>screen</code>
but the more appropriate way is to run it via
<code>screen -DR</code>
-DR tell screen to detach any attached screen (that belongs to ur username of course) and logout, i mean if you had a session on ur server for example running from the server itself, screen will quit and will logout the user automatically, and if u had already an ssh screen session, session will quit it and close that ssh connection ( <code>screen -Dr</code> same behaviour but doesn't logout/close ssh)

In screen ^a (^ means Ctrl so Ctrl+a) is the key to everything,
^a ^c : open new screen (new tab if u want)
^a A : Rename the current screen (tab)
^a d : detach the screen
^a DD : detach and logout/close ssh
^a x : lock the screen need a password to unlock it (or simply attach it elsewhere)
^a k : close the current tab
^a SPACE : next tab
^a BACKSPACE : previous tab
^a ' : Specify tab number
^a " : tab menu
^a ^a : Jump to previously used tab, very helpfull when u're working on sth on 2 tabs
^a ^ : kill all tabs and quit
^a l : redraw the current screen
^a S : split the screen
^a TAB : Jump to the next splitted part of the screen

Tutorial : http://rucus.ru.ac.za/docs/screen/Backtick.html

and If phrakture doesn't mind, his config which i use and love
.screenrc : http://phraktured.net/config/.screenrc
.screen : http://phraktured.net/config/.screen/

BTW I run a screen inside a screen, from the screen that runs locally, screen connects automatically to the server and i launch screen from it, but to accomplish this add
<code>escape ^Qq</code>
to the server's .screenrc so ^a will become ^q (change it to whatever u want)

Enjoy wink

Offline

#8 2006-05-31 23:38:45

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Terminal Question

_Gandalf_, very nice.
Just one thing, to detach you only do ^a  then press "d" (as opposed to ^a ^d).

Offline

#9 2006-05-31 23:41:14

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Terminal Question

Yep ralvez, sorry I use ^a DD more often and it sliped my mind
thx

Offline

#10 2006-06-01 00:40:52

twiistedkaos
Member
Registered: 2006-05-20
Posts: 666

Re: Terminal Question

Alright, you talk about about SSH, which isn't what I am running. I'll give you an example, but I'm pretty sure I can figure it our myself. I have a script that runs a game server:

#!/bin/sh
while true; do
/dir/foo/fooserver
done 

But, unfortunately that makes the console hidden so I can't access it anymore, which hopefull the 'screen' thing you're talking about will solve my little issue. So, if I want to start the server instead, I should do something like:

#!/bin/sh
while true; do
screen -DR && screen -t fooserver && screen -x fooserver
done 

Seomthing around those lines?

Offline

#11 2006-06-01 01:27:04

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Terminal Question

Wait, the server need to be run again and again and again ? coz of the script above the file is executed endless times, is it what u're looking for ??

Offline

#12 2006-06-01 01:31:33

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Terminal Question

I would assume that multi-runs of the same server would create enough forks that eventually the system would crash... not a good idea, if you ask me.  :?

Offline

#13 2006-06-01 01:44:33

twiistedkaos
Member
Registered: 2006-05-20
Posts: 666

Re: Terminal Question

Currently:

#!/bin/sh
while true; do
/dir/foo/fooserver
done 

Just restarts the server if it closes, it doesn't keep opening and opening ect. IE: Server crashes, it's automatically braught back up.

Offline

#14 2006-06-01 01:53:02

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Terminal Question

Oh, ok here's what you can do, install screen then do:

screen -DR

a screen will open with a bash of the current user, now do

while true; do /dir/foo/fooserver; done

And this way, the server will run, it will stay on the screen, plus from anywhere you can bring that screen up (by screen -DR) and stop it (serval Ctrl+c) or just monitor it... that would be a good idea

If you intend to only use it as a server and you don't want to monitor it but to start/stop it and in case it crashed launch it back, then create and rc.d script, that will output a pidfile and use monit to monitor it and automatically lauch it in case it fails (tell me if u need instructions...)

Offline

#15 2006-06-01 18:45:53

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Terminal Question

twiistedkaos wrote:

Currently:

#!/bin/sh
while true; do
/dir/foo/fooserver
done 

Just restarts the server if it closes, it doesn't keep opening and opening ect. IE: Server crashes, it's automatically braught back up.

That surprises me... if there was a 'wait' in there, I'd be less skeptical.. I believe you mind, it just shocks me that the thing doesn't spawn a gabillion fooservers wink


.oO Komodo Dave Oo.

Offline

#16 2006-06-01 21:36:18

ralvez
Member
From: Canada
Registered: 2005-12-06
Posts: 1,694
Website

Re: Terminal Question

It surprises me too. If I so somthing like:

INST=`ps -A | grep -c process_name`
if [ "$INST" == 0 ]; then ...

I would think there is  a programmatic constraint to prevent continual forking of the same action over and over ... but hey, what do I know  lol

Offline

#17 2006-06-01 22:20:23

phildg
Member
Registered: 2006-03-10
Posts: 146

Re: Terminal Question

It depends whether fooserver detaches itself from the shell and runs in the background or not. Doesn't it?

Try this

$ while true; do echo Hello; sleep 10; done

Now this

$ while true; do echo Hello; sleep 10 & 
> done

Note: the  then newline works the same as the semi colon, bash doesn't seem to like an ampersand then a semi colin.

Be warned, the second example will executed `sleep 10` over and over as fast as it can, so be ready with ctrl-c

if fooserver runs in the shell then it the loop will only iterate when the current instance of fooserver is killed

Offline

Board footer

Powered by FluxBB