0

I have the necessity to run a script in background, now it is in etc/init.d directory and run at the OS start up, that acting like follow:

For X seconds nothing happens then run a command, if any key is press on the keyboard it will run another command then restart the waiting time. Just to make a common example is like when the keyboard light goes off if nothing is pressed for a certain amount of time, then goes back on as soon as the keyboard has been pressed or the mouse moves.

This is my actual code:

#!/bin/bash

while true; do
    sleep 1

    read -rsn1 input
        if [ "$input" = "ł" ]; then
            echo "this is a key that you can't press"
        else
            echo "you press " "$input" 
            # command to run here if key has been pressed
        fi
        sleep 20
        # command to run here after 20 secodns if key has not been pressed
    done

trap control_c SIGINT

control_c()
{
    stty sane
}

exit 0

When i tested the code on the terminal it works like ok. But if I run in background the program loops like a keyboard is pressed immediately after the sleep 20 line.

I running this script on Linux Mint Victoria 21.2 is based on Ubuntu 22.04

I didn't add the mouse yet because the keyboard's condition doesn't run as expected alredy.

edit:

As per @user1934428 suggestion i try to use evemu-recor. This is the result,the test on the terminal was fine, but when run as background process doesn't get the keyboard feedback.

#!/bin/bash

start=$SECONDS
while true; do
sleep 1
input=$(sudo timeout 5s  evemu-record  /dev/input/event3  | grep "EV_KEY / KEY")
duration=$(( SECONDS - start ))
    if [ "$input" = "" ]; then 
        if
            (( $duration > 30 )); then
            # command to run here
        fi
    else
        # command to run here
        input=""
        duration=$(( SECONDS - duration ))
    fi
done

trap control_c SIGINT

control_c()
{
    stty sane
}
User-285
  • 1
  • 3
  • 1
    I don't see how this is supposed to work when started from `init.d`. You want to monitor some keyboard, but since you are not in an interactive shell, there is no keyboard associated with your process. Furthermore, a Linux system - by design - could have more than one keyboard attached (since it can support more than one physical terminal). I think keyboards can be found on Linux somewhere below `/dev/input`. – user1934428 Aug 04 '23 at 10:34
  • @user1934428 - I don't see why do I have to run it in `/dev/input`. it runs in `init.d` because is the designed place to put shell command that has to start on OS start up for Linux Mint Victoria. Also the `input` has 0 bite file not accessible just as links to see the address of the devices. Anyhow actually I work on a sigle keyboard, if necessary to point to this keyboard I can add the line in case. But the `read` command it works when run in the terminal itself, so actually it read and return the correct key pressed also. So far it looks works correctly for tha part. Am I wrong? – User-285 Aug 04 '23 at 10:42
  • Because in the terminal, you have an interactive shell which **is** associated with a keyboard. To be more precise: Stdin in your terminal session is bound to a keyboard, plus you can also access the keyboard directly using `read`. For your case, [this](https://wiki.archlinux.org/title/Keyboard_input) perhaps helps. A solution to a similar problem, but using `C` instead of `bash`, is shown [here](https://stackoverflow.com/questions/20943322/accessing-keys-from-linux-input-device). – user1934428 Aug 04 '23 at 10:53
  • @user1934428 I tryied. In Linux Mint 21.2 `evtest` is in maintenace mode and is suggeste to be use as stated [here](https://community.linuxmint.com/software/view/evtest) `evemu-record`. This package is not even present, so I installed `evemu-tools` package. I try but I fail. I tested the `C` example as you suggested. I can see the feed back of the keyboard but I am not capable to have it run in background, loop, and so on. – User-285 Aug 07 '23 at 08:07
  • Well, in the background, you don't have a keyboard available for your process. If you are talking about a *hardware* keyboard connected to your computer, you need to read from the the device driver for this keyboard to intercept what someone is typing on it. – user1934428 Aug 07 '23 at 09:18

0 Answers0