Page 1 of 1

Ravas coding goodies

Posted: 16 Apr 2016, 16:40
by Rava
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:


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 (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;
    } < /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

Re: Ravas coding goodies

Posted: 20 Apr 2016, 06:31
by Rava
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.

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.

Resuming watching a movie…

Posted: 06 Jan 2019, 01:31
by Rava
Long time having posted in this thread; maybe I should just stop doing so, no replies from no one tells me no one is interested in my coding stuff anyway. :)
Still today I share something else. :D

Ever had to pause a video in the middle, cause RL appointments got in the way? :shock:

First I made me a hidden text file storing the info about e.g.

title.avi 0:40:35

telling me, I watched title.avi up to approx 0:40:xx and that I want to continue watching at 0:40:35.

So, I thought of a solution having the file name and the time to start watching coded into one script, and then just start the script. It asks via a GUI if the user really wants to watch "title.avi" from "time"?

Code: Select all

# read a .weiter kind of setupfile that defines the $spiele and $to instead of hardcoding this info in each and every
Xdialog --title ".weiter mpv"  --fixed-font --under-mouse --yesno "Spiele:\n${spiele}\nYes:Abspielen, No:Abbrechen\n\n( Abspielen ab $to )" 0 0
if [ $? -eq 0 ]; then mpv --player-operation-mode=pseudo-gui --video-sync=display-resample --no-osd-bar --audio-file-auto=fuzzy --start $to "$spiele" ; else
	aplay "$wavfile" 2>/dev/null
In this example, the movie file is "/mnt/sda2/video/Filme/Sintel.2010.720p.mkv"
The time to start watching is set to "0:10:21"
The user is told that pressing "Yes" is Abspielen, German for: Play (the video file), while No is Abbrechen = Aborting (the playing of the file), the program then plays a sound file to give an additional info that the video was not played (file /usr/share/sounds/uget/notification.wav)

If course, for all to work as it should, the video must exist as coded into the script, and the notification.wav must be found at the given location. And you need to have mpv installed on your Porteus system. ;)

(weiter is German for "continuation", "carrying on" or such.)

Finally this is how it looks like:
Clicking "Yes" opens the mpv window, playing the video at the told position:
(click for full view)

Re: Ravas coding goodies

Posted: 09 Jan 2019, 08:29
by Rava

Code: Select all

# suspend-s3b2
suspend-s3b2 V0.1
(sleep 3s; beep2)
09.01.2019 07:27:34 ____________________________________________________________
              total        used        free      shared  buff/cache   available
Mem:           3880        2402         490         217         986         859
Swap:          3018         385        2632
●●●●● pm-suspend took 01:37:30 … ●●●●●●●●●●●●●●●
09.01.2019 09:05:05 ____________________________________________________________
              total        used        free      shared  buff/cache   available
Mem:           3880        2449         406         217        1023         812
Swap:          3018         383        2634
Aaand another one. Since I am too lazy to break down my scripts to post these here when no one seemingly appreciates these anyway, I will not change my code to make it more easy to adapt to the user's own needs.

So, in the above case, I use some centralized code for my script colour handling that is Linux variant independent. First, when I started with Linux it was via Suse Linux many years ago (Suse 5.1 when I recall right). I coded my stuff so that local files are all in /usr/local/bin because, the /usr/local/bin part says so. But then I encountered for some months only a live OS that had the whole hierarchy of /usr/local in its read-only live system, so I had to think of a solution which is independent of the actual files to load into the scripts and came up with having the file name in a system wide variable. Usually the file is still in /usr/local/bin/, but in case I work with the above mentioned live thingy again or come across yet another one that blocks the write access to the hierarchy of /usr/local, I can alter the path to the needed dependency files accordingly.

Why did I code suspend-s3b2 and what does it stand for?
s3 stand for 3 seconds pause, the grace time I give the user (aka usually me, myself an I :D ) time to abort the suspend if the need be, and b2 stands for beep2, my somehow volume reduced variant of my beep command.

And what does it do? It shows free -m with an added time and date ruler line prior suspend and after suspend, it calculates the time while the system was in suspend into HH:MM:SS and it prints the start and end of suspend into /var/log/messages as well. What you see above in not all that the script does, most of what gets written into /var/log/messages is missing.

And why did I code it? It was during the rc phases orf Porteus 4.0 and some of my machines had severe issues with suspend, taking u8p to 45or 50 seconds to go into suspend, so I wanted a script that logs the suspend in a way that it can be easily found browsing /var/log/messages; you might have guessed it, its what the "●●●●●●" are for.

So, all in all, no coding goodies this time, just an explanation what the next one will be about, the suspend-s3b2 command, and why I have to first give you the code to the colour handling and sound handling for my scripts. :)

Re: Ravas coding goodies

Posted: 10 Jan 2019, 13:36
by Rava
Since I don't want my shy reading but never commenting fans not going dry till I start with the pre-codes for the suspend-s3b2 script. lets have an one liner, shall we.

The question being "Hey, Rava! I know of a list of programs that use either most of my CPU or Memory after my system runs for a while. I would like to save usage info of these programs into a log so that I can review it later"

Now, lets put the log and regular checking stuff aside for now, lets just concentrate on the listing of %MEM and %CPU part of a list of preselected "known to be potential critical" programs.

Lets presume the programs are the following, separated by "|" divider.


So, we are running X, using Xorg, most probably we are running Openbox cause that uses SpaceFM as file manager, and we use mpv to watch videos or listen to music, and the audio is maintained by pulseaudio. The browser is palemoon (a fork of Firefox) and the email client is interlink, the slim and efficient fork of Mozilla Thunderbird. And viewnior is used for watching and browsing images.

Here is the one liner that uses top and grep to get the info we need:

Code: Select all

$ echo '  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND';/usr/bin/top -bn 1 | grep -E 'palemoon|Xorg|pulseaudio|mpv|spacefm|viewnior|interlink'
 1459 root      20   0  508748 186104  32580 S   6.2   4.7  73:11.82 Xorg
 9542 guest     20   0 3181392 873072 101128 S   6.2  22.0 176:59.98 palemoon
 1498 guest     20   0 1396352  98532  24588 S   0.0   2.5   3:42.15 spacefm
 1574 guest     20   0 1190044   8628   6132 S   0.0   0.2  12:29.78 pulseaudio
 4039 guest     20   0 2069084  69096  19624 S   0.0   1.7  10:50.67 mpv
15948 guest     20   0  307896  77496  34096 S   0.0   2.0   0:11.98 viewnior
18213 guest     20   0 2152328 336100  30188 S   0.0   8.5  38:28.19 mpv
32075 guest     20   0 2059764 242792  47452 S   0.0   6.1   8:23.75 interlink
Now we would need a header line giving us date and time, or else a log would make not much sense, as it would make a listing like above, say, every 10 minutes, but not tell us when palemoon used 300% CPU or any such high %CPU or %MEM usage.

We also might want to add the x most CPU using programs (aside from the ones we watch above since there could be another unsuspecting culprit hogging most of the %CPU or %MEM.), and also the memory and swap usage, like so (listing the 4 most CPU using programs, hence its name):

Code: Select all

$ top4x;fx;sx
10.01.2019 13:51:47 ____________________________________________________________
 1459 root      20   0  509644 185056  31532 S  12.5   4.7  73:24.64 Xorg
 9542 guest     20   0 3178320 864592 101096 R   6.2  21.8 179:21.08 palemoon
25132 guest     20   0    6560   2992   2416 R   6.2   0.1   0:00.02 top
    1 root      20   0     240      0      0 S   0.0   0.0   0:02.40 init
10.01.2019 13:51:47 ____________________________________________________________
              total        used        free      shared  buff/cache   available
Mem:           3880        2319         870          57         690        1101
Swap:          3018         665        2352
10.01.2019 13:51:47 ____________________________________________________________
Filename				Type		Size	Used	Priority
/dev/zram0                              partition       970     665     100     
/mnt/sda6/linuxswap                     file            2047    0       -2    

As you can see, the functions or alias of top4x, fx and sx already have the date and time stamp divider line we would need for our above log suspicious programs thingy.

But since one of these is part of suspend-s3b2, we will get into top4x, fx and sx later. (And also add top9x and dx sometimes.)