DEV Community

Discussion on: Automating your keyboard's backlit with Bash

Collapse
 
justabaka profile image
Just a baka • Edited

That's what cron is for. 2 lines, 2 commands:


0 6 * * * root /path/to/xset -led named 'Scroll Lock'
0 18 * * * root /path/to/xset led named 'Scroll Lock'

Collapse
 
raymelon profile image
Raymel Francisco

Cool. That is way simpler. Thanks!
I have a question though. Can we use cron to make the keyboard decide whether to turn on/off its backlit at startup? Any suggestions?

Collapse
 
justabaka profile image
Just a baka

You may run it using @reboot in the same crontab or take advantage of your init system (sysvinit / systemd / etc). But you'll now have to calculate the desired led state (on/off) in your run command or script, which can be achieved by simply comparing the current 24-h formatted hour value with 6 and 18.

#!/bin/bash

current_hour=`date +%H`
led_value="led"

if [ $current_hour -ge 6 ] && [ $current_hour -lt 18 ];
then
    led_value="-led"
fi

/path/to/xset $led_value named 'Scroll Lock'

This can be compacted to an one (and ugly) one-liner and put everywhere. A bit more civilized way would be to leave it as a shell script.

# notice the cron.d syntax
@reboot root /path/to/our_super_script.sh
0 6,18 * * * root /path/to/our_super_script.sh