LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2004, 07:23 AM   #1
wizard7423
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Rep: Reputation: 15
Switch from X to console [solved]


Hi at all,

i need to know a command to switch from X to a console. I need something with the same effect of: "Ctrl+Alt+F#" (with #=1,2,....,n); but as i need to put this command into a bash script file, it must be an istruction not a sequence of keystroke.
Thanks at all in advance.
Wizard

Last edited by wizard7423; 12-29-2004 at 07:57 AM.
 
Old 11-19-2004, 09:00 AM   #2
meblost
Member
 
Registered: May 2004
Location: At Keyboard
Distribution: Mandrake 10.0, SuSE 9.0
Posts: 114

Rep: Reputation: 15
If you want to stop X, you can do service dm stop, or if that is not available, init 3. I'm not sure what effect, if any it will have to put these in a script.
 
Old 12-26-2004, 07:53 AM   #3
wizard7423
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Original Poster
Rep: Reputation: 15
hi, and thanks for your answer...but i don't need to stop X server. I wish switch from x to a console (outside x) with a command, and not with the usual Ctrl+Alt+F#.

Thanks anyway.
wizard
 
Old 12-28-2004, 06:45 AM   #4
deveraux83
Member
 
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Rep: Reputation: 15
Ctrl+Alt+F#(1-6)

some kernels/distros disable certain terminals, such as slackware and only the 6th terminal is available for use. Hope this helps
 
Old 12-28-2004, 09:59 AM   #5
meblost
Member
 
Registered: May 2004
Location: At Keyboard
Distribution: Mandrake 10.0, SuSE 9.0
Posts: 114

Rep: Reputation: 15
Quote:
Originally posted by wizard7423
I wish switch from x to a console (outside x) with a command, and not with the usual Ctrl+Alt+F#.
 
Old 12-28-2004, 10:20 AM   #6
deveraux83
Member
 
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Rep: Reputation: 15
Quote:
Originally posted by meblost
I wish switch from x to a console (outside x) with a command, and not with the usual Ctrl+Alt+F#.
DUH! I really should stop and read carefully before blabbering my mouth!
 
Old 12-28-2004, 10:22 AM   #7
wizard7423
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by deveraux83
Ctrl+Alt+F#(1-6)

some kernels/distros disable certain terminals, such as slackware and only the 6th terminal is available for use. Hope this helps
Of course...thanks for your post. Anyway i know this feature, but i'm looking for a "command" that has the same effect of Ctrl+Alt+F# , where #=1,...,6
but that i could put that instruction into a script. (Ctrl+Alt+F# is a keystroke. I cannot put a keystroke into a bash-script)

I hope to be clear this time.
Sorry for my bad english...and thanks in advance for your replies.
wizard
 
Old 12-28-2004, 10:25 AM   #8
wizard7423
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by deveraux83
DUH! I really should stop and read carefully before blabbering my mouth!
No problems!
thanks for your time!!

wizard
 
Old 12-28-2004, 12:27 PM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Here is a little tool in c that will switch from your X session to a Linux Console :

(it take number 1 to 6 as argument, or if no argument ctrl+alt+F1 is the default)

Code:
/* 
	switch_from_x.c
 	compile with : 
	gcc -I/usr/X11R6/include -L/usr/X11R6/lib -o switch_from_x switch_from_x.c -lX11 -lXtst
	
	usage: switch_from_x [1..6] 
	emulate ctrl+alt+F1..F6
	It use F1 by default
*/


#include <X11/extensions/XTest.h>
#define XK_MISCELLANY
#define XK_XKB_KEYS
#include <X11/keysymdef.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
	int i;
	KeySym keys[3];
	KeySym fkeys[] = {XK_F1, XK_F2, XK_F3, XK_F4, XK_F5, XK_F6};
	
	Display* display = XOpenDisplay( NULL );
	
	if(!display) {
		printf("Error, can't open X display\n");
		exit(EXIT_FAILURE);
	}

	keys[0]	= XK_Meta_L;
	keys[1] = XK_Control_L;
	keys[2] = XK_F1;
	
	if(argc > 1) {
		i = atoi(argv[1]);
		if(i && i < 7) { keys[2] = fkeys[i-1]; }
	}
	
	for( i = 0; i < 3; i++ ) {
		XTestFakeKeyEvent ( display, XKeysymToKeycode( display, keys[i] ), 
							True, CurrentTime );
    }

	if( display == NULL ) exit(EXIT_FAILURE);
	XCloseDisplay(display);
	return 0;
}

Last edited by Cedrik; 12-28-2004 at 12:32 PM.
 
Old 12-28-2004, 09:17 PM   #10
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
chvt n, where n is the console number

You can use this at command line for switching to consoles. But i really donno wat will happen if u call from script.

HTH
 
Old 12-29-2004, 07:56 AM   #11
wizard7423
LQ Newbie
 
Registered: Jul 2004
Posts: 27

Original Poster
Rep: Reputation: 15
thanks at all...

my question is solved.

wizard
 
Old 12-29-2004, 08:08 AM   #12
PBSchmidt
Member
 
Registered: Aug 2004
Location: Aachen, Germany
Distribution: Debian Sarge
Posts: 129

Rep: Reputation: 15
on my box, chvt needs root context, is this correct?
 
Old 12-29-2004, 08:59 AM   #13
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
I have no problems executing chvt from any account. I use RedHat distros
 
Old 12-29-2004, 09:11 AM   #14
mAineAc
Member
 
Registered: Nov 2000
Location: Hermon, ME
Distribution: slackware
Posts: 201

Rep: Reputation: 30
on slackware I need root access for this. you can always add it to sudo so you don't need a password.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
console switch problem fireflyanant Linux - Newbie 3 04-06-2005 10:20 PM
How to switch to Gnome under console? explorer1979 Debian 1 08-27-2004 07:09 PM
Cant switch to console from X sepppel Linux - Newbie 2 07-18-2004 12:57 PM
to switch from X window to console: go_sooner Linux - General 2 02-03-2004 09:36 AM
how to switch to console login? miica Linux - Newbie 5 10-30-2003 10:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 AM.

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