Ravas coding goodies

For discussions about programming and projects not necessarily associated with Porteus.

Ravas coding goodies

Postby Rava » 16 Apr 2016, 17:40

In this thread I will share over time some of my coding goodies, nuggets, aliases,functions and scripts.

Lets start with the 3 ones that I use daily, the ones I use at times every hour or such, especially when RAM and CPU gets used up with my palemoon and 3 windows & 30 tabs minimum each, x versions of an image viewer, x open files in at least 2 mousepads, at times an gmplayer or mtpaint, or even gimp.
Oh, and for email alpine, and for coding either mcedit or geany. So you see, even a system with 3 1/2 GB RAM and Dual Core CPU gets into stress.


So, here my top 3: aliases/functions:

top4;fx;sx

Code: Select all
rava@porteus:/mnt/DL$ top4;fx;sx
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
 6728 rava      20   0 19748 2476 2032 R    4  0.1   0:00.02 top               
13074 root      20   0  596m 171m  20m S    2  5.1   1284:17 X                 
29350 rava      20   0 4209m 2.1g  10m S    2 64.3   1056:19 palemoon           
    1 root      20   0   240   16    0 S    0  0.0   0:18.38 init               
16.04.2016 17:15:42 ____________________________________________________________
             total       used       free     shared    buffers     cached
Mem:          3393       3237        155          0         19        338
-/+ buffers/cache:       2879        513
Swap:         6326       2063       4263
16.04.2016 17:15:43 ____________________________________________________________
Filename            Type      Size   Used   Priority
/dev/zram0                              partition       678     0       1000   
/dev/sda1                               partition       2047    0       100     
/mnt/sda3/pagefile.sys                  file            3599    2063    -1   


What do they do?

top4 gives me the topmost 4 CPU using processed (utilizing, of course, top)

fx is my "free eXtended, aka in MB. With dividing ruler. And date/time stamp. For later / former comparisons.

sx is the same for swap, my "swap eXtended". (also with dividing ruler. And date/time stamp. For later / former comparisons.)
And since /dev/swaps or swapon -s not have an "MB option", I had to code it myself. I asked for some help on that coding over at http://www.linuxforums.org/ (I would have preferred stackoverflow, but it fails in registering when using palemoon, and since it has a large part of its site especially for coding.... still sadly they are not willing to fix that issue, they just tell me (use firefox or chrome for setting up the account"
Yeah thanks, real coder brilliance! (Or lazyness)


Code: Select all
rava@porteus:/mnt/DL$ type top4 fx sx
top4 is aliased to `top -bn 1|head -n 11|tail -n 5'
fx is aliased to `echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;free -m'
sx is a function
sx ()
{
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    {
        read firstLine;
        echo "$firstLine";
        while read f t s u p; do
            let "s2 = $s / 1024";
            let "u2 = $u / 1024";
            printf '%-40s%-16s%-8s%-8s%-8s\n' $f $t $s2 $u2 $p;
        done
    } < /proc/swaps
}


And when we are at sx and fx, why not give you dx as well?


Maybe someone guesses what dx could be, seen what sx and fx do?
Code: Select all
# dx
16.04.2016 17:34:42 ____________________________________________________________
Filesystem     Type     1M-blocks  Used Available Use% Mounted on
aufs           aufs          1528   906       622  60% /
/dev/sda2      ext3          3904  3858        45  99% /mnt/sda2
/dev/sda3      ntfs         32022 31483       539  99% /mnt/sda3
/dev/loop18    ext2            25    22         3  89% /Lsfind


Did you guess it?
dx is my dfree eXtended, (and again, also with dividing ruler. And date/time stamp. For later / former comparisons.)
I know, once again a lame coded defaulting to 80 $COLUMNS so far, I was too lazy to code for a wider display than 80, are a less wide or "more narrow" display would screw up the info for sf, fx or dx almost always, and it not bothers me when the divider ruler is only 80 chars wide since none of the printed info usually is any wider.

Okay, dfree's output could be longer, depending on the mounted path length, but usually that's in the form of /mnt/sdXn and not any longer.


But maybe you realize that I not just run dfree -mT, but my dx also omits two lines I usually am never interested in.
Lets ask dfree -mT for comparison, shall we?
Code: Select all
$ df -mT
Filesystem     Type     1M-blocks  Used Available Use% Mounted on
aufs           aufs          1528   908       620  60% /
devtmpfs       devtmpfs      1696     1      1696   1% /dev
/dev/sda2      ext3          3904  3858        45  99% /mnt/sda2
/dev/sda3      ntfs         32022 31483       539  99% /mnt/sda3
/mnt/live/run  none          1697     3      1695   1% /run
/dev/loop18    ext2            25    22         3  89% /Lsfind

See what I mean?
I am not interested in devtmpfs or /mnt/live/run, usually these FS never get any more full than 1% regardless for full my local disk, or my RAM and swap is. And that's, as far as I understand it, the normal way since that disk space is used differently and not for use for standard files and such, like storing some more ISOs or MPEGs [1]. (Look it up what they do please if you want, its part of either how /dev works, or how a live Linux works (/mnt/live/run)

So, you maybe want to know how I coded that, aka dx omitting the unneeded 2 lines? [If your system should misbehave, please also do look at standard dfree -mT and see if, maybe, these two show strange full stats or such... just in case. :) ]

Do you, or don't you? Please do tell me if anyone is interested in my mini coding stuff at all.

Thanks for your time!Image
Cheers!
Yours Rava
User avatar
Rava
Contributor
Contributor
 
Posts: 1234
Joined: 11 Jan 2011, 03:46
Location: Germany
Distribution: Porteus 3.1.0 x86-64 XFCe

Re: Ravas coding goodies

Postby Rava » 20 Apr 2016, 07:31

It's neat how easy it is to look up if a certain program or script is currently running:
Code: Select all
# ps ax|grep swapx
30161 pts/0    S+     0:00 /bin/sh /usr/local/bin/swapx
30166 tty1     S+     0:00 grep --color=auto swapx


What annoys me is that the grep that looks for the name is always listed as well. Is there are way to undo that? Sure, we are at a *IX here, and a GNU *IX to boot...

Killed the swapx script, but that calls come programs successively, the first one being mcedit.
So, mcedit still runs:

Code: Select all
ps ax|grep mcedit|grep -v grep
30162 pts/0    S      0:00 mcedit /etc/fstab


Now I used the grep tweak -v "-v, --invert-match select non-matching lines"
Or in other words, omit all matching lines.

Just what we want here, looking if a process runs - by name - since no one is interested that the grep that searches for the running program will also listed, especially since it will always be listed.

Code: Select all
# ps ax|grep mcedit|grep -v grep
#

Seems I killed the mcedit now as well...


Using the same trick with the already killed swapx script, we get a much neater result: when the program or script is not currently running, we get no results.

Now sure we can put that into a script itself, calling it e.g. grepps or psgrep (or even greps) but I leave that to you.

Cave!
Searching for "grep $psname" can get you into trouble, when $psname being the program or script we look up.

Some users, so me, use an alias (or even a function) to expand grep's usual behaviour.
E.g. on my Porteus "grep whatever" gets expanded to "grep --color=auto whatever"

So, omitting it via
Code: Select all
ps |grep whatever |grep whatever
would still
print "grep --color=auto whatever" since that was what was executed here.
Cheers!
Yours Rava
User avatar
Rava
Contributor
Contributor
 
Posts: 1234
Joined: 11 Jan 2011, 03:46
Location: Germany
Distribution: Porteus 3.1.0 x86-64 XFCe


Return to Programming



Who is online

Users browsing this forum: No registered users and 1 guest