Browsing all articles tagged with shell script
Oct
4

Using PS to output human readable memory usage for each process using AWK

I ran into a little issue: I wanted to list the memory usage of each process, but I wanted it to be output in human readable form (as in “df -h”).  So, after a little Googling I found a forum thread which put the idea of using awk.  A little bit later I had the solution:

Here’s what you see with normal ps:

$ ps u
 
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
kes1 12394 0.0 0.0 107356 2052 pts/1 Ss 08:15 0:00 -sh
kes1 12648 0.5 3.2 720808 527356 pts/0 S+ 08:16 0:16 /usr/lib64/R/bin/exec/R
kes1 13682 0.0 0.0 107360 2016 pts/3 Ss 08:18 0:00 -sh

Here’s what we want to see:

USER    PID     %CPU    %MEM            VSZ             RSS     TTY     STAT    START   TIME    COMMAND
kes1    12394   0.0     0.0            104.84Mb          2.00Mb  pts/1   Ss      08:15   0:00    -sh
kes1    12648   0.7     3.2            703.91Mb        515.00Mb  pts/0   S+      08:16   0:16    /usr/lib64/R/bin/exec/R
kes1    13682   0.0     0.0            104.84Mb          1.97Mb  pts/3   Ss      08:18   0:00    -sh

And here’s how we make that happen with awk:

ps u | awk '{
for ( x=1 ; x<=4 ; x++ ) { printf("%s\t",$x) } 
for ( x=5 ; x<=6 ; x++ ) {  if (NR&gt;1) { printf("%13.2fMb\t",hr=$x/1024) }  
else { printf("\t%s\t",$x)  } 
}
for ( x=7 ; x<=10 ; x++ ) { printf("%s\t",$x) } 
for ( x=11 ; x<=NF ; x++ ) { printf("%s ",$x) } 
print "" 
}'

What does this awk script do?

There are a few different things going on here.  First, we want to print out all the non-memory fields as tab-separated text.  Second, we want to print out the memory as formatted kb and not bytes.  Also, we want everything to line up. Finally, we want to print out the process name normally (using space and not using tab separation).

First, the script prints the first 4 fields:

for ( x=1 ; x<=4 ; x++ ) { printf("%s\t",$x) }

Then, we check to see if this is the header line or not. If it is the header line, then NR (the number of records read) will be 1. If it’s not, then NR will be greater than 1. For the header, we print out the text (adding some more tabs to line things up). For the memory fields, we use printf to format the byte count as human readable text.

for ( x=5 ; x<=6 ; x++ ) {  
if (NR>1) { printf("%13.2fMb\t",hr=$x/1024) }  
else { printf("\t%s\t",$x)  } 
}

Finally, we print the remaining PS fields as before, but then we switch to space-delimited fields for the process name and arguments, making sure to add a newline at the very end.

for ( x=7 ; x<=10 ; x++ ) { printf("%s\t",$x) } 
for ( x=11 ; x<=NF ; x++ ) { printf("%s ",$x) } 
print ""

Nothing to it!

Oct
31

Use an Accurate Satellite View of the Earth as your GNOME Background

This site provides an accurate satellite view of the Earth; you can see the current areas that are in sunlight and darkness, as well as being able to see the current weather patterns (updated from actual satellite weather imagery). Here’s a script that will download the latest image and set it as your GNOME background.
read more

Oct
30

Reboot or Hibernate into Windows from Ubuntu Easily

It’s often the case that I’m working on something in Ubuntu and need to switch over to Windows. For whatever reason, I got it in my head that it was too difficult to select hibernate or reboot from the power menu, wait for the system to power down, select Windows from the boot menu list, and wait for Windows to boot up. I wanted to be able to click on an icon, go get a sandwich, and walk back to a Windows login.

So, here are two scripts that accomplish this. When run, they will locate the boot entry for your Windows system, select that as the default boot entry, and either reboot or hibernate (with a restart after hibernation).
read more

Oct
29

Converting WMA files to MP3 on Linux (specifically Ubuntu) and retain tag information

I recently needed to convert a bunch of (DRM-free) WMA files to MP3 format on Ubuntu, and there’s no good tool out there to do it.  There are plenty of tutorials that tell you how to convert from WMA to WAV with Lame, then from WAV to MP3 with mplayer, but none of these tutorial talk about how that will cause you to lose all the WMA artist/album/title data.  I needed a tool that would keep the tagged data;  so, I made one.  It requires mplayer, lame, and mutagen-inspect, all of which are available from aptitude.

So, here’s my script: wma2mp3
read more

%d bloggers like this: