Monday, March 5, 2007

Killing jobs and making penguins

This weeks Windows tip is a really long one, so you can skip straight to the examples if you are impatient.

Did you ever want to use a command line or batch file to kill a particular task? It can be done with a whole lot of options with 2 different command line tools.

tskill

It is used as follows:

tskill processed
or
tskill processname

For example:

tskill calc” will kill all instances of calc.exe, or “tskill winword” will kill all instances of Microsoft Word. (add /a at the end of the command and it will kill them for all users on that machine – only if you have administrative privileges)

You could use:
tskill processid

Use “Task Manager” or “tasklist” to find processid.
To find all options, - tskill /?

tskill is a nice tool but it is limited so you may prefer to use –

taskkill.
The syntax is: “taskkill /im imagename” where imagename is the full name of the program you want to kill (like calc.exe) or “taskkill /pid processed” . You can kill a number of processes in one line: taskkill /im calc.exe /im winword.exe /pid 27501 will kill all instances of calc.exe, winword.exe and process number 27501.
The processes can also be filtered out using the /fi parameter. Here you can use various functions such as eq (equals), ne (Not equal to), gt (greater than), lt (less than), ge (greater than or equal to) and le (less than or equal to).
You can now use those parameters against a number of different options: imagename (the name of the program), pid (process id), memusage (amount of memory being used by process), cputime (percentage of cpu being used by process) and more. (type tasskill /?)
By the way, adding the /f parameter will forcefully kill the process.
So, you may ask, “what is all this good for?”. Below are a number of examples and I am sure you can think of more.

Let’s say you want to kill all non-responding processes:
taskkill /f /fi "status eq not responding" (Note: you must include the quotation marks).

or
If you are running low in memory and want to close all processes using more than 35 Megabytes:
taskkill /f /fi "memusage gt 35000" (Again, you must include the quotation marks.)

"But that shut an important program down!!!!! Now I lost my PowerPoint presentation which I was working on for hours!!!!!!"

Well, you should have filtered it out:
taskkill /f /fi "imagename ne powerpnt.exe" /fi "memusage gt 35000"
will close all processes using more than 35 Megabytes except if the name is powerpnt.exe.

Pretty cool, no?

Linux:

How to make a paper penguin. (Thanks to http://linuxreviews.org )
Download the following .pdf file, print the first page and follow the instructions on the second page.
http://linuxreviews.org/howtos/penguin/penguin.pdf
(If you are asking what this has to do with Linux, then maybe this blog is not for you )

Don't hesitate to leave a comment. (And click the google ads if they look interesting)

Thursday, February 22, 2007

Irritating or kewl feature? Recently used files

First of all, apologies for skipping 2 weeks. I was a bit under the weather.
I am back on track with some kewl tips.

Windows:
Whenever you install a new program in Windows XP, a very irritating balloon pops up next to the "START" button and in the “All Programs” menu, the new programs are highlighted. If you find this useful, cool but if, like me, this annoys you, you can disable it.
1. Right-click the “Start” button.
2. Select “Properties”.
3. Click the “Customize” button.
4. Choose the “Advanced” tab.
5. Under “Start Menu Settings”, uncheck the “Highlight newly installed programs” checkbox.
6. Press the “OK” button and then click the “OK” button again.
If you want to re-enable this, follow the instructions and check the “Highlight newly installed programs” checkbox in step 5.

Linux: (There is a nice bonus hint at the bottom)

Do you want to find the most recently changed files in a folder with lots of files and subdirectories?
One way is to use the “ls” command and combine it with a few flags. In addition, you can pipe it into “head” to see the 10 most recently modified files. The -t flag sorts the entries by the date they were last modified.
For example:
ls -lFt ¦head (The -F flag distinguishes different types of files and directories by adding extra characters (such as '/' for directories, '*' for executables, etc.)
There are many more flags to be used with “ls” so see the man pages – “man ls”.
To find all files which were modified within the last twenty-four hours, try the following: find / -mtime 1 or in a specific folder find . –mtime 1
If you want to find all files modified since midnight: find / -daystart -mtime 0

Bonus tip: (Supplied by the one and only Brian Hatch). http://www.ifokr.org/bri/
Do you use /usr/bin/less as your pager? Do you find yourself on machines where the screen clears when you exit it, for example when you're looking at a man page? Try setting your LESS variable to export LESS=MQieX
and that man page won't disappear, thwarting your attempts to reference the arguments you just looked up.

Thursday, February 1, 2007

system tools and schedules

Windows:
Windows XP Pro has a command line tool called systeminfo, which gives a whole lot of information on the system including motherboard info, the date the OS was installed, what patches have been installed, what processors you have, how much memory and virtual memory and lots more.

To access it, open a command promt and run “systeminfo”. You can output the data to a file by typing “systeminfo > c:\myfilename.txt” and then open “c:\myfilename.txt” to see all the info in a text editor.

You can also run a System Information window by clicking Start > All Programs > Accessories > System Tools > System Information. This also includes some cool system-checking tools in the “TOOLS” menu

Linux:

“at” what time do you want this one time job to run?

If you want a job to run once only, you could put it in “CRON” and then delete it later – or you could use the “at” command.

Example:

at 12:30
When you click Enter, you get the at> prompt

You then type in the command or script you want to run at the given time (in this example 12:30), hit enter and then Ctrl+D.

at> /usr/bin/improvemylife.sh

The above example will run the /usr/bin/improvemylife.sh program at 12:30. (Unless you are a script wizard, don’t expect this to actually improve your life)

You can see a list of all your at jobs with:
at -l

This will list all pending jobs including the job number.

You can remove a pending job with atrm

atrm 2
There are other ways to input the date and time you want the job to run. Just check the man page.

(By the way, you can also use the at command in windows. From a command prompt type “at /?” for the syntax)

Wednesday, January 31, 2007

Windows:
Adding line numbers to a word document: (This is not bullets and numbers. This is just in the view. In the printed document they are not shown.)
1. Go to File -> Page Setup and click the Layout tab.
2. Under Preview, click in the “Apply To” box and select “Whole Document”.
3. Click the “Line Numbers” button.
4. Click the “Add Line Numbering" check box.
5. Click “Restart Each Page” and click OK.
Each line of text will be numbered (except those in tables, footnotes, endnotes, text boxes, frames, and headers and footers), starting with number 1 on each page. If you want the numbering to be continuous and not start at 1 on each page, then in step 5, click the “continuous” box.
In some cases, you may not want certain paragraphs numbered. For example, highlighted notes for the reader that are not intended for the final document. To prevent Word from numbering those lines, follow these steps before turning on Line Numbering in Page Setup.
1. Highlight the paragraphs that you do not want numbered.
2. Go to Format -> Paragraph and click on the “Line And Page Breaks” tab.
3. Click the “Suppress Line Numbers” check box and click “OK”.
(This will also work when formatting an email in outlook)

Linux:

Use the “pr” utility to format text files so they are more suitable for viewing or for printing to a printer. The "pr"takes a file and breaks it up into pages, adding a header to each page.
For example, “pr” would convert a file that contains 150 lines into a file that contains three pages of text that you could then send to the printer.
By default, each page contains 66 lines of text. You can change this by using the -l switch.
The default header at the top of each page is the filename. You can customize this using the –h switch.
For example:
$ pr -h "My Homework" filename.txt
Rather than using "filename.txt" as the header, this will format the filename.txt file and use "My Homework" as the centered heading on each page.
pr” prints to standard output so it can be piped into another tool such as “more”, “less” or to redirect it to the “lpr” tool to print the file. If you want to keep the resulting output in a file, you need to redirect it.
For example:
$ pr filename.txt > newfilename.txt
Use the man pages or “pr –-help” to find all the available parameters such as formatting into columns, changing page width etc….
Bonus Tip:
To add the line numbering (like in the windows tip above) when using “vi” :
When in vi, press “Esc” and then “: set nu”

Feel free to leave a comment at the "comment" link below.

Sunday, January 28, 2007

Windows:


Network diagnostics tool.


Start --run-- netsh diag gui


Will open up the network diagnostics tool. You can set the parameters of what to check and then run the check. You can then save the output to a file and email the file to your admin or network guru to make it easier to troubleshoot a network problem.

Bonus Tip: (hey, I should charge extra for all the bonus tips)

To display a list of commands entered in a current DOS prompt (similar to the "history” command in Unix):

enter in the following command at the DOS prompt:

DOSKEY /history



Linux:


Did you ever ssh into a machine and then get stuck probably due to a break in the connectivity and your ssh client shell is still stuck? Ctrl C and Ctrl D does not seem to get you out of it.

You could open a new console, then ps –ef ¦grep ssh to find the PID of the ssh process and kill it but that is a bit complicated.

Instead, try to type “~.” (You may have to hit Enter). This should kill the ssh session on the client side.

Bonus tip:

nohup” command. (nohup = no hangup). If you need to run a script or command on a remote box but are worried that your ssh / telnet session will disconnect and disrupt your running process, use the nohup command and it will continue to run even if you get disconnected. Standard output is redirected to the “nohup.out "file.

Example: you want to find all files beginning with blabla on the remote box and write the list to the file /tmp/filelist.txt.

You could do

find / -name “blabla*” print >> /tmp/filelist.txt .

This will start a search and could take a long time. If your ssh / telnet session is disconnected, the "find" process will also die.

Instead, type:

nohup find / -name “blabla*” print >>/tmp/filelist.txt & (The & sign puts it in the background).

Now if your ssh / telnet session dies for any reason, your find process will continue. You can then reconnect and see the results in the /tmp/filelist.txt file.


Feel free to send me comments with any hints and tips to enlighten everyone with.

Monday, January 22, 2007

Formatting documents

Windows:
Adding line numbers to a word document: (This is not bullets and numbers. This is just in the view. In the printed document they are not shown.)

1. Go to File -- Page Setup and click the Layout tab.

2. Under Preview, click in the “Apply To” box and select “Whole Document”.

3. Click the “Line Numbers” button.

4. Click the “Add Line Numbering" check box.

5. Click “Restart Each Page” and click OK.

Each line of text will be numbered (except those in tables, footnotes, endnotes, text boxes, frames, and headers and footers), starting with number 1 on each page. If you want the numbering to be continuous and not start at 1 on each page, then in step 5, click the “continuous” box.

In some cases, you may not want certain paragraphs numbered—for example, highlighted notes for the reader that are not intended for the final document. To prevent Word from numbering those lines, follow these steps before turning on Line Numbering in Page Setup.

1. Select the paragraphs that you do not want numbered.

2. Go to Format -- Paragraph and click on the “Line And Page Breaks” tab.

3. Click the “Suppress Line Numbers” check box and click “OK”.

(This will also work when formatting an email in outlook)





Linux:


Use the “pr” utility to format text files so they are more suitable for printing to a printer or viewing. It takes a file and breaks it up into pages, adding a header to each page.

For example, “pr” would convert a file that contains 150 lines into a file that contains three pages of text that you could then send to the printer.

By default, each page contains 66 lines of text. You can change this by using the -l switch.

The default header at the top of each page is the filename. You can customize this using the –h switch.

For example:

pr -h "Tip-o-the-day" filename.txt

Rather than using "filename.txt" as the header, this will format the filename.txt file and use "Tip-o-the-day" as the centered heading on each page.

pr” prints to standard output so it can be piped into another tool such as “more”, “less” or to redirect it to the “lpr” tool to print the file. If you want to keep the resulting output in a file, you need to redirect it.

For example:

pr filename.txt >newfilename.txt

Use the man pages or “pr -–help” to find all the available parameters such as formatting into columns, changing page width etc….

Bonus Tip:

To add the line numbering (like in the above windows tip) when using “vi” :

When in vi, press “Esc” and then ": set nu"

Monday, January 15, 2007

DOS is not dead.

Windows:
Anyone remember DOS? It is still useful today.
To create a few folders with one command, use the “md" command:
Open a command prompt.
md this is more than one folder – will create 6 folders – this, is, more, than, one, folder
now delete them – rmdir this is more than one folder
To create a folder several folders deep, use:
md this\is\more\than\one\folder – will create a folder called this with a folder called is in it etc….
Creating a single folder with spaces in the name requires adding a double-quote at the beginning:
md "this is one folder" – will create one folder called “this is one folder

Bonus tip: At the command prompt, you can use forward slash (unix style) "/" as a folder divider when using the cd command to change your present working directory:
cd Windows/system32/drivers/etc


Linux:
Quick method for copying an entire directory including subdirectories and files to another directory.
(cd /source/directory && tar cf - . )¦(cd /dest/directory && tar xvfp -)

(My friend Richie pointed out that this could be done using:
cp -av /source/directory /destination/directory
I find that using tar will do the job quicker)

Monday, January 8, 2007

When did this windows box boot last?

Windoze:


Ever wonder when a windows box was last booted. In linux we can use “who –b” (in most flavours) but what about windows?

Try from the command prompt:

"net statistics workstation ¦ more "

(the for those unfamiliar "¦" is the "pipe" symbol. It is usualy depicted on the keyboard as two vertical lines one above the other)

The output of this command will show the date and time of last startup of windows. (The rest is mostly irrelevant info unless troubleshooting connections to servers)


Linux:

Did you ever have a file with a weird name including control characters which you could not delete? (ab^Hrd[ or something like that) (It could happen when naming a file you do a backspace which is sometimes input as ^H) You can’t do rm ab^Hrd[ because the shell will read part of the name as a control character.

One way to do it is to use the files inode number.

ls –il will show you the inode number of the files in the directory so you find what the inode number of that file is.

then you just type
find . -inum <inode-number>-exec rm -i {} \;
(Of course, replace <inode-number> with the actual inode number)
You will be asked to confirm the deletion and voila. File deleted.

Monday, January 1, 2007

1.Windows:
Remote desktop from a command line or batch file:

You can script a remote desktop connection, as Windows XP's remote desktop connection has an executable file that can accept command line parameters and be run from a batch file. The remote desktop connection executable file is MSTSC.EXE, and the following are some of the most common parameters:
· /v:<computername>--specifies the name of the computer to connect to.
. /v:<ipaddress> -- specifies the ip address of the computer to connect to.
· /f--starts the connection in a full screen.
· /w:<width>--specifies the width of the remote desktop screen.
· /h:<height>--specifies the height of the remote desktop screen.
· /console—if someone is connected and there are no more valid connections, this will connect to the console session.

For example, to remotely connect to a computer named pinky in a 640 x 480 remote desktop window, you would use the following command:
mstsc /v: pinky /w:640 /h:480

You can type this command line in the Start --> Run dialog box, as well as use it in a batch file.
(This will only work from XP pro and above)

2. Linux:

Resolving netbios name from an ip address
nmblookup –A <ipaddress>
e.g. nmblookup –A 192.168.170.39
will give the netbios name and group (domain) of the windows station with that ip address.