Browsing all articles tagged with tip
Jan
17

How to Copy Terminal Session into a File

Have you ever wondered how to copy the output of your terminal into a text file? Or maybe you teach Linux and you want to see what your students typed in and as well as the output? You think that running history is not enough? Then you need the script command.

Running script command

Open the man page of script command and you will see this:

Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

In a nutshell, it is history and tee all rolled into one. It will record everything you see on your screen, even the color. So if you typed in an invalid command, you will see the error in the log or if you run it correctly, you will have the output. But commands like top that refreshes the screen at an interval will most likely ruin the session or the log, so try to avoid similar commands.

To use it, just type the command script and it will begin recording the session. Once you are done, just type exit.

This is script in action:


rai@host1:~> script -a /tmp/script_test.log


Script started, file is /tmp/script_test.log
rai@host1:~> ls /home
R20 r200 R21 rai xx19
rai@host:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit
Script done, file is /tmp/script_test.log
rai@host1:~> cat /tmp/script_test.log
Script started on Mon 17 Jan 2011 06:24:12 PM PHT
rai@lhost1:~> ls /home
R20 r200 R21 rai xx19
rai@host1:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit

Script done on Mon 17 Jan 2011 06:24:54 PM PHT

The example above shows that script was started with -a option meaning it will append the output the specified file.

A better way to do this is to use it together with mkfifo command:

On Terminal 1 (Student’s terminal):

rai@host1:~> mkfifo /tmp/script_test.fifo
rai@host1:~> script -f /tmp/script_test.fifo

On Terminal 2 (Teacher’s terminal, same machine):

rai@host1:/tmp> cat /tmp/script_test.fifo

The above scenario will perform the following:
1) On the Student’s terminal, it will create an named pipe /tmp/script_test.fifo (man mkfifo) then run the script command with the -f option that ‘flushes’ out the output after each run. The Student’s terminal will look like it is not responding at this point, but don’t worry, it is perfectly normal.
2) On the Teacher’s terminal, the command cat will read the output file. Once you run the cat command, the session will be started.

Try the above steps and see how each screen behaves. Check also if doing the script command will create a populated output file.

Jun
8

How To Reset MySQL Password

If you are like me who tend to forget passwords almost every time, then this tip might help you reset your MySQL password just in case.

To begin, you must have access to the server, one that can stop and start the MySQL process, such as a root account. Sudo account can also be used, just as long that the user can control the MySQL service.

Begin the process by stopping the MySQL process. This can be done by executing /etc/init.d/mysqld stop command, but this can vary depending on how you setup your server. Also, be careful of users who might be using the MySQL service as stopping the service might disrupt their work.

Once the MySQL process is killed, execute the following command:

mysqld-safe --skip-grant-tables

This will run the MySQL process so you need to open another terminal to be able to reset the password. On the new terminal, you will now be able to login to MySQL to reset the password.

On the shell prompt of the new terminal, open the MySQL console:

mysql

mysql> use mysql;
mysql> UPDATE user SET Password=PASSWORD(‘YOUR_NEW_PASSWORD’) WHERE Host=’localhost’ AND User=’root’;
mysql> exit

This will change the password for the user ‘root’. Kill the MySQL process running in the first terminal, either by killing the PID or executing CTRL+C. Then start the process again, this time with the proper procedure:

/etc/init.d/mysqld start

Try logging in to the MySQL console using the new password:

mysql –uroot -p

You should now be able to use the new password for your MySQL.

Dec
13

Tip: Remove Duplicate Lines in File Using Perl

Here is little nifty code I found to snip or remove duplicate lines in a file using Perl. An example of a file that has duplicate lines looks like this:

/home/index.php
/home/links.php
/home/index.php

The result we want to achieve is this:

/home/index.php
/home/links.php

And here goes the code:

$ftmp = 'file.txt';
my %match = ();
{
local @ARGV = ($ftmp);
local $^I = '.tmp';
while(<>){
$match{$_}++;
next if $match{$_} > 1;
print;
}
}

The original file, file.txt, has its duplicate lines removed.

Nov
19

Tip: How to Use watch Command

Author Rai    Category Linux     Tags , , , , , ,

I have never known that there is a command in Linux called watch. Basically, watch runs a program in a period of time, and displaying the output in real time.

watch command syntax is very simple. One typical use of the command watch is running it with traceroute. This is one example:

watch 'traceroute google.com'

This will run the traceroute command, display the output and when it is done, it repeats the process again.

Here a couple more examples of the watch command:

watch -n 10 free -m

This command will run the command free in ten-second intervals, and display the output.

watch -n 3 'cat /proc/interrupts'

This command will display the interrupts in 3-second intervals.

Jul
21

Tip: Prevent SSH Session from Disconnecting

The network connection at office keeps my SSH session from running smoothly. Well, it means I keep on being disconnected from the server when my SSH session turns idle for a certain period of time. It gets annoying especially if I am in the middle of a script running silently.

One alternative I have written before here is using screen command or by editing the SSH config file to prevent SSH from disconnecting its connection.

The process is very simple: your SSH session consistently sends packets over the connection to let the remote computer know that the session is still active and there is no need for termination. This is what they call Keep Alive packets. For me, it means Keep My Sanity packets.

Anyway here is what you need to do. Edit your ssh_config file, usually in /etc/ssh/ directory:

# vi /etc/ssh/ssh_config

And put this line in the file:

ServerAliveInterval 60

Save and exit.

Open the ~/.ssh/config file (or create it if not present) and put this line in it:

Host *
ServerAliveInterval 60

Don’t forget the indent at the second line. Save and exit.

Lastly, reload your new SSH config file by doing:

# /etc/init.d/sshd reload

This should do the trick of fooling the remote server into thinking that your SSH connection is active, even if it is not.

Did this tip worked for you? Let me know in the comments section.

Powered by 1and1.comDomain Registrations starting at $9.98* Earn with Your BlogAdvertise @ PinoyTux

Search PinoyTux

Subscribe to Email Feeds

Enter Email Address:

Blog Lounge

Popular Posts

Recent Posts

Recent Comments

Site Stats