Błażej Piotrowski
Błażej Piotrowski

Reputation: 21

Bash script which works in the background, waiting for the key

I need script which works in the background, waiting for a key to be pressed. I have a script which works when terminal is on. When I try to use & - it's not working correctly. The script works in background but keypress doesn't do anything. When I try use nohup then I have error of read - bad descriptor. Can someone help me?

Upvotes: 1

Views: 556

Answers (1)

Jamil Said
Jamil Said

Reputation: 2093

When the terminal is on (and is selected, in case of a terminal emulator running on x server), the input (key strokes, etc.) is directed to the terminal; hence, your program works.

Now, consider an example: let's say that you created a script that runs on the background, and it is activated by pressing the letter "a". If pressing "a" triggered your script (with terminal closed), then the user would never be able to (for example) type the letter "a" in a word document or a web search without triggering the script!

Therefore, what you are looking for is a key bind, or a keyboard shortcut, which would bind a key (combination) such as ctl+j+k to launch the script you want.

In Linux, that can be done somewhat easily, see this for Ubuntu or this for Lubuntu.

Important: if your script needs to be root to work, then you generally will have to evoke it via gksu or gksudo, otherwise it will not run

Upvotes: 1

Related Questions