Situation: I have a script watching over NumLock status, well not really watching, but turning it On every 1 second. The script is running in the background.
Reasoning: I often accidentally turn off NumLock. And I have no indicator of NumLock status on the keyboard.
OS, DE, DM, WM, xmodmap
:
$ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 18 Sarah
Release: 18
Codename: sarah
echo $DESKTOP_SESSION
cinnamon
cat /etc/X11/default-display-manager
/usr/sbin/mdm
wmctrl -m | head -n 1
Name: Mutter (Muffin)
xmodmap -pm
xmodmap: up to 4 keys per modifier, (keycodes in parentheses):
shift Shift_L (0x32), Shift_R (0x3e)
lock Caps_Lock (0x42)
control Control_L (0x25), Control_R (0x69)
mod1 Alt_L (0x40), Alt_R (0x6c), Meta_L (0xcd)
mod2 Num_Lock (0x4d)
mod3
mod4 Super_L (0x85), Super_R (0x86), Super_L (0xce), Hyper_L (0xcf)
mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)
My original Bash script follows:
#!/bin/bash
while true
do
numlockx on
sleep 1s
done
As you can see, the script does not care about current status of NumLock. It just keeps turning it on.
Goal: I would like to make the script at least somewhat CPU efficient.
Question: What is the most CPU efficient way to ensure that NumLock is On in Linux (Mint 18)?
Best Answer
No, it is not efficient. The problem is, the cost of querying the NumLock state is the same as the cost of setting the NumLock state. So, you would just double the load if you try to query state before setting it.
You could make it a little better by writing compiled C code, as you would avoid fork / exec and interpreting costs, but it would still remain a horrible hack.
What you could do instead, is to set the NumLock on, and then disable NumLock key (or even ignore it's state if all you want is numeric keypad always numeric).
See this SuperUser post for details how to do that with xmodmap(1).