sidux.com
Scripts - Charon takes you to runlevel 3 and back
ModestUser - Jul 14, 2008 - 10:41 PM
Post subject: Charon takes you to runlevel 3 and back
Tired of Ctrl-Alt-F1, typing your root password again, doing smxi, sgfxi or apt-get dist-upgrade and moving back to runlevel 5?
This script performs a smooth transition to runlevel 3, executes an arbitrary command or program (smxi, sgfxi, apt-get, ...) and goes back to runlevel 5. It support session logging via the option -l. Try it, this is very useful in case something went wrong with your d-u.
Examples (e.g. in KDE)
Code:
charon smxi
or
Code:
charon apt-get update && apt-get dist-upgrade
to get session logging:
Code:
charon -l apt-get dist-upgrade
After this, you will have a file in /var/log/charon/ that contains everything that was displayed on your screen during that command.
Installation: save to /tmp/charon.zip
as root:
cd /usr/local/bin
cp /tmp/charon.zip .
unzip charon.zip
I am happy for any bug reports. charon is usually depicted as an ugly man and so is this code. Use with caution. It is really a dirty hack ... but it works great here.
changelog 0.2:
- added trapping, so CTRL-C will take you back to RL 5
- addeds session logging
- add a lock, so there can be only one instance of charon running on a tty
- do not do init 5 if there is dpkg/apt-get/smxi/sgfxi running.
- display a message on the VT after a command has been terminated
michaaa62 - Jul 15, 2008 - 07:23 AM
Post subject: Charon takes you to runlevel 3 and back
Nice one, i just did my du with it.
Did you pm h2? He was asking in another threat for such a script to make it possible to run smxi from X and return to X afterwards.
I had to reboot because of a new kernel anyway, but are there any left-over pieces of the older, 'su'-ed sessions?
To use charon i had to cd to the directory, that contains charon to execute it, although the directory was in my path. It there a way to change that?
cheers Micha
ModestUser - Jul 15, 2008 - 12:14 PM
Post subject: RE: Charon takes you to runlevel 3 and back
- No, there are no leftover root sessions. The X terminal is killed along with init 3 and the shell on the tty ends with the command.
- It should start from /usr/local/bin now without problems. Thanks a lot for the bug. That was a restarting problem.
h2 - Jul 15, 2008 - 06:07 PM
Post subject: RE: Charon takes you to runlevel 3 and back
I'll be doing an update on smxi to remove the init 3 component, leaving only the kill x/kde part, but it will also have to still support users who start from init 3 anyway, to bring them to init 5 if it's that type of system.
There are currently a wide range of options I'm seeing real world users running, and it's making the init 3 part of smxi basically worthless from what I can see.
Once I have this stuff worked out, maybe today, I'll start looking at the charon stuff, which I'm sure, based on modestuser's earlier work, will be very interesting.
h2 - Jul 15, 2008 - 09:43 PM
Post subject: RE: Charon takes you to runlevel 3 and back
I've removed the init 3 thing in smxi for shut x, but it will still start the default system init level on start desktop.
I found one strange thing, which I can't figure out how to resolve, the pid for for example kdm does not vanish when you do /etc/init.d/kdm stop, this is unfortunate, and might require a closer look because otherwise smxi will always try to kill x again even though smxi has already shutdown kdm/x.
dzz - Jul 17, 2008 - 10:26 AM
Post subject: RE: Charon takes you to runlevel 3 and back
Interesting. Seems to work although I had to stop smxi this morning because sun-jre would be removed, maybe that will be fixed later. Straight back to kde after smxi exit. I see only a few have downloaded charon so far.
I tried opening a second terminal (alt+f2) to paste smxi output as I can never remember how to answer the config questions. You can't always scroll back far enough.
Unfortunately alt+F1 then gave just a root shell with no evidence of smxi. Something was running somewhere because a second attempt gave "dpkg locked in another process" error but I couldn't find why or where so had to reboot.
muchan - Jul 17, 2008 - 10:58 AM
Post subject: RE: Charon takes you to runlevel 3 and back
dzz, just a guess, but you can try Alt+F8 or Alt+F6.. (Alt+F7 is X windows)
the next free number is used to create new virtual machine.
ModestUser - Jul 17, 2008 - 11:08 AM
Post subject: RE: Charon takes you to runlevel 3 and back
Most likely smxi was running at /dev/tty8. You can switch there with CTRL-ALT-F8. charon does no dpkg or apt-get at all, so your dpkg locked problem should not be related to this. If however this is reproducible, please report.
michaaa62 - Jul 17, 2008 - 04:29 PM
Post subject: RE: Charon takes you to runlevel 3 and back
Hi ModestUser,
so far caron smxi and charon ceni work flawless on my erebos system, but it makes me wonder why the kde-session is available so much faster for the second kdm process.
cheers Micha
dzz - Jul 17, 2008 - 04:33 PM
Post subject: RE: Charon takes you to runlevel 3 and back
Got my terminal at tty2 running nano at the same session and.......you're right, smxi still there at tty8, didn't look there!
As for the dpkg lock, I had part-run smxi at the time. I then lost smxi after opening another tty and got back to X by doing init 5 there. At that point apt itself was likely interrupted while still busy, maybe waiting for instruction.. So probably not looking at the charon script itself for the cause of that.
Anyway, I'll test charon some more when DU looks clean.
Would not a prompt at script execution to advise what will happen and wait for user input (y or n) be worth adding?
dzz - Jul 17, 2008 - 06:33 PM
Post subject: RE: Charon takes you to runlevel 3 and back
I reproduced the error.
Apt was still active, waiting for an answer
56 upgraded, 4 newly installed, 1 to remove and 3 not upgraded.
Need to get 56.3MB of archives.
After this operation, 43.8MB disk space will be freed.
Do you want to continue [Y/n]?
After restarting X from another tty the one running smxi (apt at that point) was still active. In fact, doing charon smxi again, two copies of smxi were found running!
So the script itself is good, just be careful to exit any other terminals cleanly.
h2 - Jul 17, 2008 - 06:44 PM
Post subject: RE: Charon takes you to runlevel 3 and back
Re checking configs/warnings, once you have started smxi, and the dist-upgrade has begun, all you need to do is switch to another tty, and start smxi again like this: smxi -W cw
This will show the configs and the warnings/alerts. -W c shows only configs, -W w shows only warnings.
If you start smxi -W.. as root, it will also update the warnings/configs to latest, if you start as user, it will just use the last versions from the last time smxi warnings ran pre du.
Re the other stuff, I'm interested to see how this all works out in terms of especially supporting kill.
Keep in mind as well, during the du, smxi has a trap function to remove any holds, so it's important that that works as well.
ModestUser - Jul 17, 2008 - 07:52 PM
Post subject: RE: Charon takes you to runlevel 3 and back
@dzz: that apt-get process that you quote asks for an answer. Did you press 'n' <RETURN>? This is decisive here. If a process like apt-get or smxi ends, its output is still on that screen available via CTRL-ALT-Fx, but it is not running anymore. But i agree, it might be confusing to tell what is running and what not. I will make some improvements tonight.
@h2: Maybe you could add a lock for smxi to prevent multiple instances of smxi running? Or even checking for dpkg / apt-get locks?
h2 - Jul 17, 2008 - 07:59 PM
Post subject: RE: Charon takes you to runlevel 3 and back
It's a feature now for multiple instances of smxi to run, that's how you use the smxi -W option during a smxi dist-upgrade session, for example. However, the -W option simply prints out the data and exits.
Checking for apt-get locks you can do, but usually apt get will let you know that anyway.
dzz - Jul 17, 2008 - 10:02 PM
Post subject:
@ModestUser
No, I did not hit
'n' <RETURN>
because I did not know at the time how to get back there to do it, having gone to tty2 to paste the output! Normally we do the DU in tty1.
Naturally, apt griped when invoked later.
That or other problems could probably happen by user error with or without charon; doing multiple tty's, not exiting cleanly then restarting X.
Your script is not to blame for that, it's very good indeed. A short warning before execution, as I suggested earlier, might help a first-time user though.
ModestUser - Jul 18, 2008 - 04:32 PM
Post subject:
new version 0.2 with trapping, locking and session logging
ModestUser - Jul 18, 2008 - 04:35 PM
Post subject:
@michaaa62: ceni can be run within X.
dzz - Jul 18, 2008 - 06:03 PM
Post subject:
charon -l smxi
shuts the kde session then immediately reopens it
Code:
INIT: Switching to runlevel: 3
* Stopping K Display Manager: kdm * Stopping K Display Manager: kdm
[ ok ]
[ ok ]You are now in runlevel 3.
/usr/local/bin/charon: line 83: -l: command not found
INIT: Switching to runlevel: 5
* Starting K Display Manager: kdm
[ ok ]
Will be great to get logging working.
ModestUser - Jul 18, 2008 - 07:43 PM
Post subject:
@dzz: did you get the new version?
dzz - Jul 18, 2008 - 07:52 PM
Post subject:
Ignore that post!
The old version was still in /usr/local/bin it's just me again, probably extracted it to the wrong place
It's working perfectly now (new version) and the log is where it should be. Sorry for the false alarm!
dzz - Jul 23, 2008 - 10:22 AM
Post subject:
I'm really enjoying myself trying out this script! Now working flawlessly (DU gets done here most days)
The log files are all there, you can even read the current in another tty as it's happening (mcedit)
As much as I like h2's terminal colours, their characters make a text log hard to read. My understanding of sed is pretty minimal but I cobbled this together:
Code:
sed 's/\x1b\[[0-9]\{1,2\}\(;[0-9]\{1,2\}\)\{0,2\}m//g' /var/log/charon/* > charon-log
and it wrote a text file to /home/me with those characters removed.
Maybe in the future, something along those lines to write the log directly?
h2 - Jul 23, 2008 - 06:10 PM
Post subject:
wow, I was banging my head against the wall trying to figure out how to remove color codes from output myself, lol...
By the way, if I didn't mention it, I added new option, because modestUser mentioned it, -j, which turns off smxi and related script colors.
Also, smxi also does full logging by default now, always. Those logs are quite substantial, found in: /var/log/smxi.log (with rotate running for > 200kB, to log1, log2), /var/log/sgfxi/sgfxi.log, with rotate, and /var/log/svmi.log
<added this to s**i logging, great stuff.
dzz - Jul 23, 2008 - 07:05 PM
Post subject:
Yes the -j option is mentioned elsewhere but why would we do that, the colours in the terminal look so good! Also actually help readability.
Good that logrotate is considered. Log files can get very large and forgotten (kernel logs have crashed my system in the past)
ModestUser - Jul 23, 2008 - 08:50 PM
Post subject:
The problem with the 'sed' command is that the escape sequence is not always escaped. They could be in quotes, e.g. One would need precise syntactical tracking to know if a certain sequence would indeed be displayed as color. This is why i did not go for sed but suggest using either the -j option or a pager that supports colors. 'most' (sidux default) can do this. Just do most /var/log/charon/foo.
btw, how do you handle rebooting? i found it very annoying that charon does init 5 if you are done and i thought i should change this. Probabaly CTRL-C should not go to runlevel 5 but give you a shell.
dzz - Jul 24, 2008 - 12:11 AM
Post subject:
Using charon to do smxi, normally the only reason to reboot is after a new kernel: then that option is within smxi anyway? Other console jobs (including shutdown -r now) can be called from a second terminal, before smxi exit (after script tasks are finished). For example I do apt-move update && apt-get clean at the console after DU, while kde is not in my face. Obviously charon's root terminal must be closed clean afterwards, just as one does init 5 && exit when not using smxi; a user doing multiple terminals should know that.
Would it not defeat the point of the script if it were a one-way ticket to init 3? It's pretty damn good as it is bar a little minor tweaking.
I don't know sed well enough to comment on it's finer points at all; I'm sure others here do. I just tried that as an experiment for what seems a good idea for the log, knowing sed to be the stream editing tool. I like to see those colours in the terminal.