Displaying Linux Distro Version and Showing 32Bit or 64Bit OS

Checking whether you have 32bit linux or 64bit linux. From the code below we can see that it is a 64 bit linux.

1
2
[user1@ubuntu 17:14:37 ~/dev]$ uname -mrs
Linux 3.2.0-30-generic x86_64

Checking which distro you’re using:

1
2
3
4
5
6
[user1@ubuntu 17:14:48 ~/dev]$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.1 LTS
Release:    12.04
Codename:   precise
Posted in Linux, Operating System | Comments Off on Displaying Linux Distro Version and Showing 32Bit or 64Bit OS

Find and Replace File Content using Linux Command Line

1
find . -name "*.pgsql" -print | xargs sed -i 's/STRINGTOREPLACE/REPLACERSTRING/g'
Posted in Linux, Operating System | Comments Off on Find and Replace File Content using Linux Command Line

How To List All Users or Groups in Linux

To list all users in Linux:

1
cat /etc/passwd

See only the first word before double colon (:)
for exampple, user “root” will look like: root:x:0:0:root:/root:/bin/bash

And to list all groups do the following

1
cat /etc/group
Posted in Linux, Operating System | Comments Off on How To List All Users or Groups in Linux

How Big is The Big Apple?

Do you know how much bigger is Apple compared to the other big companies in the US?
http://www.greenandredmarket.com/menu/biggestcompanies.htm
And scroll to the lower part of the page.

I’m not an apple fan boy but I can see that our society buys a lot of apple products: iPhones, iPad, iMac, MacBook Pro, Macbook Air.

Posted in Investment, Stock Market | Comments Off on How Big is The Big Apple?

Internet Explorer (IE) friendly table DOM manipulation

Occasionaly we can manipulate HTML DOM easily by accessing the innerHTML of an element. In IE however, the following elements’ innerHTML are read only: COL, COLGROUP, FRAMESET, HEAD, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR.

This code will fail in IE

1
tBodyEl.innerHTML = '<tr height="24px"><td colspan="7">' + 'Some Cell Value' + '</td></tr>';

A more IE friendly solution using DOM table functions.

1
2
3
4
5
var row = tBodyEl.insertRow(0);
row.setAttribute('height', '24px');
var cell = row.insertCell(0);
cell.setAttribute('colspan', '7');
cell.innerHTML = 'Some Cell Value';
Posted in javascript, Web Development | Comments Off on Internet Explorer (IE) friendly table DOM manipulation

Preventing Another Instance of A Program Running in Linux

The code below is my solution for preventing a program from running more than once at the same time in Linux.
The scenario: Consider if you have a cron job (jobA.sh) set to run at 6PM, 7PM, and 8PM.
While the 1st instance of jobA.sh started, it might (or might not) need more than an hour to finish. If it does requires more than one hour, while the 1st instance is still running, a 2nd instance of jobA.sh started.
Now you ended up with two instances of a same program consuming your CPU, files, etc – which will slow things down even more.

So here how I solve it:
When the 1st instance is started, check if there is a temporary lock file, if yes then just exits or quit. If not create the temporary lock file. And do your code/algorithm. After it finishes delete this lock file. When the 2nd instance started it will also do the same thing, checking if there is lock file.
It will be wise to put another cron job that cleans all lock file every 00:00AM. Just to be sure the next execution (at 6PM tomorrow) will be able to run

Since I’m no bash expert, let me know if there is a better way to do this. Cheers!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# =================================
# author: Ronald Pringadi
# sleep command is in seconds
# =================================
vLockFile=/tmp/lockfile1.lock
echo 'start'
# If lockfile does not exist do the followings
if [ ! -f $vLockFile ]; then
    touch $vLockFile
    echo "Lock file doesn't exists, executing code"
    sleep 7 # this can be your big process that takes longer than an hour, but just for proof of concept, I use 7 seconds
    rm $vLockFile
else
    echo "Lock file exists do nothing"
fi
echo 'done'
Posted in Linux, Operating System | Comments Off on Preventing Another Instance of A Program Running in Linux

Extjs – Ext.apply

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
console.clear();
var a = {
    abc : 1,
    def : 2
};
var b = {
    abc : 1.1,
    def : 2.2
};

var c = Ext.apply( a , b ); //Ext.apply overwrites 1st variable (by ref) with the 2nd var

console.log('c');
console.log(c);

console.log('a');
console.log(a);

console.log('b');
console.log(b);

Result:
c
Object { abc=1.1, def=2.2}
a
Object { abc=1.1, def=2.2}
b
Object { abc=1.1, def=2.2}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var a = {
    abc : 1,
    def : 2
};
var b = {
    abc : 1.1,
    def : 2.2,
    gij : 3.3
};

var d = Ext.applyIf( a , b ); //Ext.applyIf overwrites 1st variable (by ref) with the 2nd var, only if it doesn't exists
console.log('d');
console.log(d);

console.log('a');
console.log(a);

console.log('b');
console.log(b);

Result:
d
Object { abc=1, def=2, gij=3.3}
a
Object { abc=1, def=2, gij=3.3}
b
Object { abc=1.1, def=2.2, gij=3.3}

Posted in Ext Js, javascript, Web Development | Comments Off on Extjs – Ext.apply

MySQL Backup and Restore – Using Command Line

Here’s how you back up a database from the command line. The first line is the backup, the second is the restore.

These commands work on both Windows and Linux. On Windows you might want to add the MySQL bin directory to your environment PATH — the easy way is Win + Break → Advanced System Settings → Environment → PATH. Then open cmd (the command prompt). If you already had a command prompt open before adding the PATH, restart it for the change to take effect.

MySQL doesn’t export stored procedures, functions, or triggers by default. If you need those, add –routines –triggers to your command. Even if you don’t currently use stored procedures, functions, or triggers, it’s safer to back up with those options enabled — they’re cheap, and you’ll thank yourself later if someone adds one.

1
2
mysqldump -uusername -ppassword --routines --triggers --lock-tables=false database_name > YOURSQLFILE.sql
mysql -uusername -ppassword database_name < YOURSQLFILE.sql

If you need to back up all databases, the user we typically use is root. This will back up all triggers and functions. You’ll need to pre-create any other users with the appropriate privileges on stored procedures or functions for the restored copy to work end-to-end.

1
2
mysqldump -uusername -ppassword --opt --events --all-databases > alldb.sql
mysql -uusername -ppassword < alldb.sql

A few useful additions.

The password-on-the-command-line problem. The -ppassword form (with no space — that’s important) is convenient but has two real issues: anyone running ps aux on the same machine can see your password while the command is running, and it gets stored in your shell history. Two cleaner options:

1
2
3
4
5
6
7
8
9
10
11
12
# Option 1: prompt for the password interactively
mysqldump -uusername -p database_name > out.sql
# (you'll be asked: Enter password:)

# Option 2: use an option file with restricted permissions
cat > ~/.my.cnf << 'EOF'
[client]
user=username
password=secret
EOF

chmod 600 ~/.my.cnf
mysqldump --defaults-file=~/.my.cnf database_name > out.sql

The ~/.my.cnf approach also helps for scheduled backups, where there’s no human around to type the password. Just make sure the file mode is 600 — MySQL clients refuse to load it otherwise on Linux.

For InnoDB tables: –single-transaction. If your tables are InnoDB (the default since MySQL 5.5), this flag wraps the dump in a transaction and gives you a consistent snapshot without locking the tables. Concurrent writes can keep happening while you back up:

1
2
mysqldump --single-transaction --routines --triggers \
    -uusername -p database_name > out.sql

Don’t combine it with –lock-tables — they fight each other. –single-transaction only works for InnoDB; if you have MyISAM tables, use –lock-tables (the default for that case) and accept that writes block.

The MySQL 8 “Access denied PROCESS privilege” trap. Starting around MySQL 5.7+/8.0, mysqldump tries to query information_schema.tablespaces and fails for non-superusers with a confusing PROCESS-privilege error:

1
mysqldump: Error: 'Access denied; you need (at least one of) the PROCESS privilege(s) for this operation' when trying to dump tablespaces

The fix is one flag — –no-tablespaces. Most application backups don’t actually need tablespace info anyway:

1
2
mysqldump --no-tablespaces --single-transaction --routines --triggers \
    -uusername -p database_name > out.sql

Compress as you go. SQL dumps compress beautifully — typical 5-10x. For anything larger than a few hundred megabytes, pipe through gzip:

1
2
3
4
5
6
7
# backup
mysqldump --single-transaction --routines --triggers -uusername -p database_name | gzip > out.sql.gz

# restore
gunzip < out.sql.gz | mysql -uusername -p database_name
# or in one shot
zcat out.sql.gz | mysql -uusername -p database_name

Restoring a single database from an –all-databases dump. Common scenario: you have a full-instance dump but only want to restore one schema. mysql has a –one-database flag that ignores statements not for the named database:

1
mysql -uusername -p --one-database wantedschema < alldb.sql

Caveat: –one-database filters by the most recent USE statement, not by table name, so it works cleanly when the dump uses USE schema; blocks (which mysqldump –all-databases emits by default). For surgical restores from arbitrary dumps, sed/awk through the file is sometimes cleaner.

For really big databases: mysql shell. If you’re operating on databases in the hundreds-of-GB range, the modern mysqlsh utility (ships with MySQL 8.0+) does parallel chunk-based dumps and is dramatically faster than mysqldump:

1
2
mysqlsh -uusername -h hostname -- util dumpInstance /path/to/backup/dir --threads=4
mysqlsh -uusername -h hostname -- util loadDump /path/to/backup/dir --threads=4

It also handles compression natively, supports resumable loads, and writes a manifest you can inspect. For everyday small-to-medium dumps, mysqldump is still simpler and the recipes above are all you need; for anything where the backup window matters, it’s worth knowing mysqlsh exists.

Posted in Database, Linux, MySQL | Comments Off on MySQL Backup and Restore – Using Command Line

Browsing without ads on mobile browsers

This post used to recommend a now-defunct Android app called “Ad Block” that turned your phone into its own proxy server. The app is gone, the Play Store link is dead, and the proxy-based approach has been superseded several times over. Mobile ad blocking is genuinely easier now than it was a decade ago — here’s what actually works today.

The four approaches, and which one fits you

Every working option boils down to one of these four:

  1. A browser that blocks ads natively. Easiest. Install the browser, you’re done.
  2. A regular browser plus a content-blocker extension. Familiar if you do this on desktop.
  3. A DNS-based blocker that works system-wide — every app, not just browsers.
  4. Network-level blocking (Pi-hole on your home router). Best in your living room, irrelevant on cellular.

Most people end up with a combination — a content-blocking browser for everyday use, plus a DNS blocker for everything else.

On Android

Browsers with built-in blocking. The simplest path:

  • Brave — Chromium-based, blocks ads and trackers by default, no setup. The most “it just works” option for someone who’s never thought about this before.
  • Vivaldi — also Chromium-based, blocking is on by default with a more granular control panel.
  • Cromite (formerly Bromite) — a privacy-hardened Chromium fork with built-in adblocking. Niche, but well-regarded by people who care about this stuff.
  • Mull — a Firefox-based browser with privacy patches and uBlock Origin pre-configured. F-Droid only, not Play Store.

Firefox for Android with extensions. Firefox is the only major mobile browser on Android that supports a real desktop-style extension system, and that’s a big deal. You can install:

  • uBlock Origin — the gold-standard content blocker. Free, open-source, no “acceptable ads” program. This is the one I’d recommend.
  • AdBlock Plus — works fine but ships with an “acceptable ads” allowlist enabled by default; you can disable it in settings if you want. The original AdBlock that this post used to recommend by name.
  • Ghostery — focused more on tracker blocking than ad blocking, with a nicer UI for seeing what each page is trying to load.

Open Firefox for Android, tap the menu, choose “Add-ons,” and install the one you want.

System-wide DNS blocking. The trick that works for every app on your phone — not just browsers — is to point your DNS at a service that returns NXDOMAIN for known ad/tracker hosts. No root required, no proxy, no app permissions to worry about:

  • NextDNS — a configurable DNS service with a generous free tier. You enable it once via Android’s Private DNS setting (Settings → Network & internet → Private DNS) and pick which blocklists to apply.
  • AdGuard DNS — similar idea, also free for personal use.
  • AdGuard for Android (the standalone app, not the DNS service) does this plus content filtering. Note: not in Play Store because Google’s policies forbid system-wide ad blockers; install via the AdGuard website.

DNS-based blocking is the closest spiritual successor to the proxy trick from this post’s original incarnation — same idea (intercept network requests before they fetch ads), better implementation (kernel-level, supports HTTPS, no app to babysit).

On iOS

iOS is more constrained, because Apple’s WebKit policy means every browser on iOS is just a Safari skin with branding. Firefox on iOS doesn’t run Gecko; Chrome doesn’t run Blink. Real desktop-style extensions don’t exist there. So the techniques are different:

  • Safari content blockers. Apple has a first-party API for this. Install 1Blocker, AdGuard for Safari, or Wipr from the App Store, then enable it in Settings → Safari → Extensions. These work in Safari only.
  • Brave on iOS — still WebKit under the hood, but Brave’s built-in shields work because they’re applied via Apple’s content-blocker API, same plumbing as the standalone blockers.
  • System-wide DNS blocking works exactly like on Android: Settings → General → VPN & Device Management → DNS, or install the NextDNS/AdGuard configuration profile. Same NXDOMAIN trick, blocks across all apps.

Firefox for iOS does not support uBlock Origin or any other extension in the desktop sense. If you came here looking for that specifically, the answer is “not possible on iOS — use a Safari content blocker plus a DNS blocker.”

What I’d actually do

If I were setting up a phone from scratch and wanted no ads with minimum fuss:

  • Android: Install Brave for casual browsing, Firefox + uBlock Origin for anything where you want fine control, and configure NextDNS or AdGuard DNS as Private DNS for system-wide coverage of in-app ads.
  • iOS: Install AdGuard for Safari (or Wipr if you want something simpler), and add the NextDNS configuration profile.

The setup is reversible and free, and you’ll save a meaningful chunk of mobile data along the way. 🎉

Two notes worth knowing. Some sites detect ad blockers and refuse to load — that’s between you and the publisher, but most blockers have a one-tap “disable on this site” button for the rare cases where you decide it’s worth it. And remember that if you’re paying for a service (a subscription, a one-time purchase), you’re often the customer and not the product; the case for blocking ads on those sites is weaker, and many will let you disable ads in settings if you ask.

Posted in Android | Comments Off on Browsing without ads on mobile browsers

Benchmark How Long a Program Runs In Linux Using Bash

The following bash code might come in handy if you want to benchmark how long a program runs. The example assumes you want to pass two parameters along to the program. A timestamp is captured at the start and end of execution, and the difference (the processing time) is printed in seconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
##################################################
# Benchmark the processing time when a program
# executes with two required parameters: parameter1 parameter2
##################################################

if [[ $1 = "" || $2 = "" ]]; then
        echo "Usage: `basename $0` parameter1 parameter2"
else
        _start_time=`date +%s`
        _parameter1=$1
        _parameter2=$2
        ### YOUR COMMAND HERE WITH parameter1 AND parameter2 ###
        _end_time=`date +%s`
        _processing_time=$((_end_time-_start_time))
        echo "Source File: $_parameter1"
        echo "Destination File: $_parameter2"
        echo "Start time: $_start_time"
        echo "End time: $_end_time"
        echo "Processing time is: $_processing_time"
fi

A few useful additions.

One thing to know about the script above: the ### YOUR COMMAND HERE ### line is just a comment — bash doesn’t run anything. If you copy-paste and try it, you’ll get Processing time is: 0 because the timer fires at _start_time, immediately advances to _end_time, and there’s nothing in between. Replace that comment with the actual command you want to measure, e.g.:

1
rsync -a "$_parameter1" "$_parameter2"

The simpler answer: time. If “how long did this take?” is the only question you’re asking, bash already has a builtin for it:

1
2
3
4
time ./myprogram param1 param2
# real    0m1.234s
# user    0m0.045s
# sys     0m0.012s

The three numbers are: wall-clock time (real), CPU time spent in user-mode code (user), and CPU time spent in kernel calls (sys). When user + sys is much less than real, your program was waiting on something (disk, network, sleep). When they’re roughly equal, it was CPU-bound.

The time keyword also wraps a whole pipeline or compound command:

1
time { ./step1 && ./step2 | tee out.log; }

The richer answer: /usr/bin/time. The standalone GNU time binary (different program, same name — note the explicit path or you’ll get the bash builtin) reports a lot more:

1
2
3
4
5
6
7
8
/usr/bin/time -v ./myprogram param1 param2
# Command being timed: "./myprogram"
# Maximum resident set size (kbytes): 12480
# Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.23
# Percent of CPU this job got: 87%
# Major (requiring I/O) page faults: 0
# Minor (reclaiming a frame) page faults: 1532
# ...

That’s the one to reach for when you care about memory usage, page faults, or context switches alongside timing. Some distros don’t ship it by default — apt install time or dnf install time if it’s missing.

Sub-second timing without date +%s. The original script’s resolution is one second, which is too coarse for anything fast. A few ways to get finer numbers:

1
2
3
4
5
6
7
8
9
10
11
# Nanosecond resolution via date (still spawns a subprocess each call)
_start=$(date +%s%N)
# ... work ...
_end=$(date +%s%N)
echo "$(( (_end - _start) / 1000000 )) ms"

# Bash 5+ has $EPOCHREALTIME — sub-second, no subprocess at all
_start=$EPOCHREALTIME
# ... work ...
_end=$EPOCHREALTIME
awk -v s="$_start" -v e="$_end" 'BEGIN { printf "%.3f s\n", e - s }'

$EPOCHREALTIME is a string like 1715000123.456789 with microsecond resolution. It’s free — no fork, no exec — so it’s safe to call inside a hot loop. The catch: bash arithmetic is integer-only, so you need awk or bc to do the floating-point subtraction.

When you want statistical confidence: hyperfine. If the question is “is my new version actually faster?” — you want averages over many runs, with warmup, and ideally a confidence interval. hyperfine is the modern tool for this:

1
2
3
4
5
6
hyperfine --warmup 3 --runs 20 \
    './build_old.sh input.txt' \
    './build_new.sh input.txt'
# Summary
#   './build_new.sh input.txt' ran
#     1.42 ± 0.03 times faster than './build_old.sh input.txt'

It runs each command repeatedly, discards the warmup runs (which are usually slower because of cold caches), reports mean ± standard deviation, and gives you a relative-speedup figure. If you’re benchmarking anything where one-off variance matters — and that’s most things — this beats running time three times and squinting at the numbers.

Two gotchas worth knowing.

  • System clock changes break date +%s. If NTP slews the clock backwards mid-run, your _processing_time can be negative or inflated. $EPOCHREALTIME has the same problem (both read CLOCK_REALTIME). For benchmarking, the correct clock is CLOCK_MONOTONIC, which never goes backwards — but bash doesn’t expose it directly. Use time or hyperfine for anything that needs to survive a clock change.
  • Disk caches make repeated runs misleadingly fast. The first run of a program reads cold data from disk; the second run hits the page cache and looks 10x faster. Either drop caches between runs (echo 3 | sudo tee /proc/sys/vm/drop_caches on Linux) or — easier — let hyperfine handle the warmup phase for you.
Posted in Linux, Operating System, Ubuntu | Comments Off on Benchmark How Long a Program Runs In Linux Using Bash