Various unix processes canproduce fast growing logs thatsometimes need to be trimmedinstead of deleted, for referenceor troubleshooting. And youlikely have no desire to edit thefiles. Here is a handy kshscript that will quickly trimthe log so it keeps recentinformation and lets you keep asmany lines as you think you mightneed.
I call it trimlog:
#! /bin/sh
# trimlog
filesize=`cat $1wc -l`
trim=`expr $filesize - $2`
if [ $trim -gt 0 ]
then
sed "1,$trim d" $1 > /tmp/$1
mv /tmp/$1 $1
echo $1 trimmed by $trim lines
else
echo "Invalid argument"
fi
Use it by feeding in the name of the log you want to trim followedby the number of lines you want to keep:
# trimlog oracle_listener.log 10000
Of course you can't use it in the /tmp directory.
There are probably newer and more efficient ways to do this, but this works.
A unique blog for Unix, Linux based Tips, tricks and Shell Scripts. This is intended to be one-stop Information Center for all your Unix, Linux needs.
Wednesday, September 17, 2008
kill really does not KILL
kill is not really meant only to kill a process. But mainintention is to send a signalto process. And the processhandles these signals to dovarious tasks.
kill -INT 1234
where 1234 is the Process ID is really, this set an interrupt signal, Ctrl-C is an example of interupt signal givento shell.
kill -HUP 1234
some UNIX daemons handle this, to do refreshing business.
kill -KILL 1234
This is the sure kill,which cannot be handled by any process. Also know as,
kill -9 1234
Just, check the in the file /usr/include/sys/signal.h for all signals on the system or you can use:
kill -l
to get the complete list of signals.
Note: More about this can be found onkill and signal man pages.
kill -INT 1234
where 1234 is the Process ID is really, this set an interrupt signal, Ctrl-C is an example of interupt signal givento shell.
kill -HUP 1234
some UNIX daemons handle this, to do refreshing business.
kill -KILL 1234
This is the sure kill,which cannot be handled by any process. Also know as,
kill -9 1234
Just, check the in the file /usr/include/sys/signal.h for all signals on the system or you can use:
kill -l
to get the complete list of signals.
Note: More about this can be found onkill and signal man pages.
Labels:
Kill,
KnowledgeBase,
TIPS,
Unix
AUTOMATIC LOGIN TO REMOTE HOSTS w/o PASSWORD
Consider two hosts.host1.domain.comhost2.domain.com
>From hosts (user:deepak), you wish to logon tohost1 (user:paul).
Paul would create a .rhostsfile in his directory
chmod 744 .rhosts
(No write permission forothers)
The first line would contain
host1.domain.com deepak
Now, deepak should be able to logon to host2.domain.comas user paul by using rlogin.
Deepak will type
e.g.
rlogin hosts.domain.com -l paul
And he should be in.
>From hosts (user:deepak), you wish to logon tohost1 (user:paul).
Paul would create a .rhostsfile in his directory
chmod 744 .rhosts
(No write permission forothers)
The first line would contain
host1.domain.com deepak
Now, deepak should be able to logon to host2.domain.comas user paul by using rlogin.
Deepak will type
e.g.
rlogin hosts.domain.com -l paul
And he should be in.
ELIMINATING MULTIPLE SPACES
Here is a simple script whichhelps in eliminating multiplespaces when one wants toextract a particular columnin a set of records from afile.
Here is an example to provethe same:
ps -u $LOGNAME tr -s " " : cut -d: -f
The above script line can beused to extract a column fromthe output of a ps command for a particular user ($LOGNAME).
Here is an example to provethe same:
ps -u $LOGNAME tr -s " " : cut -d: -f
The above script line can beused to extract a column fromthe output of a ps command for a particular user ($LOGNAME).
Labels:
eleminating,
one liner,
Shell Script,
tr
Monday, September 15, 2008
CRYPT AN ASCII FILE
An ascii file can be easily encrypted and decrypted.
To encrypt simply pipe the STDOUT ofthe file to "crypt" and redirect it toa new file name. Enter a passowrd whenprompted with "Enter key".
$ cat foo crypt > foo.e
Enter key:
To unencrypt simply pipe the STDOUT ofthe encrypted file to "crpyt" andredirect it to a new file name. Entera passowrd when prompted with"Enter key".
$ cat foo.e crypt > foo.new
Enter key:
To encrypt simply pipe the STDOUT ofthe file to "crypt" and redirect it toa new file name. Enter a passowrd whenprompted with "Enter key".
$ cat foo crypt > foo.e
Enter key:
To unencrypt simply pipe the STDOUT ofthe encrypted file to "crpyt" andredirect it to a new file name. Entera passowrd when prompted with"Enter key".
$ cat foo.e crypt > foo.new
Enter key:
Thursday, September 4, 2008
Getting access to your linux box without password
For BSD:
In order to recover your root password, you must boot FreeBSD in single user mode, mount the filesystems read/write and issue the passwd command. The passwd command will not actually recover your root password, but will allow you to change it to something new.
Boot in single user mode
As the operating system is starting, it will display the following message:Hit [Enter] to boot immediately, or any other key for command prompt.
Booting [kernel] in 10 seconds...
You should now press the space bar, and you will see the following message:
Type '?' for a list of commands, or 'help' for more detailed help.ok>
Now type boot -s and press the enter key to start FreeBSD in single user mode.
ok> boot -s
After the system boots, you should see the statement:
Enter full pathname of shell or RETURN for /bin/sh:
Press the enter key and you will have a # prompt and you can change the password using passwod command
For RH Linux
Booting Single-User Mode
You may be able to boot single-user mode directly. If your system boots, but does not allow you to log in when it has completed booting, try single-user mode.
If you are using GRUB, use the following steps to boot into single-user mode:
If you have a GRUB password configured, type p and enter the password.
Select Red Hat Linux with the version of the kernel that you wish to boot and type e for edit. You will be presented with a list of items in the configuration file for the title you just selected.
Select the line that starts with kernel and type e to edit the line.
Go to the end of the line and type single as a separate word (press the [Spacebar] and then type single). Press [Enter] to exit edit mode.
Back at the GRUB screen, type b to boot into single user mode.
After boot, you can change the password of root using passwd command
In order to recover your root password, you must boot FreeBSD in single user mode, mount the filesystems read/write and issue the passwd command. The passwd command will not actually recover your root password, but will allow you to change it to something new.
Boot in single user mode
As the operating system is starting, it will display the following message:Hit [Enter] to boot immediately, or any other key for command prompt.
Booting [kernel] in 10 seconds...
You should now press the space bar, and you will see the following message:
Type '?' for a list of commands, or 'help' for more detailed help.ok>
Now type boot -s and press the enter key to start FreeBSD in single user mode.
ok> boot -s
After the system boots, you should see the statement:
Enter full pathname of shell or RETURN for /bin/sh:
Press the enter key and you will have a # prompt and you can change the password using passwod command
For RH Linux
Booting Single-User Mode
You may be able to boot single-user mode directly. If your system boots, but does not allow you to log in when it has completed booting, try single-user mode.
If you are using GRUB, use the following steps to boot into single-user mode:
If you have a GRUB password configured, type p and enter the password.
Select Red Hat Linux with the version of the kernel that you wish to boot and type e for edit. You will be presented with a list of items in the configuration file for the title you just selected.
Select the line that starts with kernel and type e to edit the line.
Go to the end of the line and type single as a separate word (press the [Spacebar] and then type single). Press [Enter] to exit edit mode.
Back at the GRUB screen, type b to boot into single user mode.
After boot, you can change the password of root using passwd command
CASE TRANSLATION
If you have occasion whereyou need to translate uppercase to lower and viceversa, and happen to run aksh, enter the following functions into your .bash_profile:
trans ()
{
tr '[:upper:]' '[:lower:]' < $1 > $2
}
TRANS()
{
tr '[:lower:]' '[:upper:]' <$1 > $2
}
The first function trans will translate all uppercaseletters in the first file(argument) to lowercase in the second file.
The second function TRANS does the exact opposite.
trans ()
{
tr '[:upper:]' '[:lower:]' < $1 > $2
}
TRANS()
{
tr '[:lower:]' '[:upper:]' <$1 > $2
}
The first function trans will translate all uppercaseletters in the first file(argument) to lowercase in the second file.
The second function TRANS does the exact opposite.
Tip within unix
tip is a unix utility for establishing a terminal connection to a remote system via a modem
The tip command line options are as follows tip [-v] [-speed-entry] {hostname phone-number device}
A simple example is a script that establishes a terminal session:
spawn tip modem
expect "connected"
send "ATD$argc\r"
set timeout 30
expect "CONNECT"
The tip command line options are as follows tip [-v] [-speed-entry] {hostname phone-number device}
A simple example is a script that establishes a terminal session:
spawn tip modem
expect "connected"
send "ATD$argc\r"
set timeout 30
expect "CONNECT"
Subscribe to:
Posts (Atom)