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

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?

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

Related

OSX ITerm2/ZSH Terminal Application Execution Requires User to Press Enter to Resume

I am having an issue with ITerm2 on OSX Mojave. I have a long-lived python script running in the background and for some reason the OS or ITerm/ZSH keeps pausing the application and a Key Icon appears in the terminal -- which you can only bypass by pressing the return key. This application is going to take roughly 10 hours to finish processing and I can't just sit there and press enter every time the icon appears. Does anyone have an idea of what could be causing this and how to get around it?
Your Python script is prompting for input of some kind. It's impossible to say what hitting return actually does (is input being used simply to pause before continuing, or is it asking for some actual value, but an empty input accepts some hard-coded default), but you can simulate it.
Instead of running
python myScript.py &
run
yes "" | python myScript.py &
yes will provide an infinite stream of empty strings for your script to read each time it tries to read input.

Bring Terminal in the front with Ruby

I am using Ruby on a mac to open a dozen URLs one at a time with Nokogiri etc.
For each URL I need to let my ruby program know
whether to keep the URL window for further inspection or close it.
But I cannot see the terminal window and its prompt, it is hidden behind
the last URL window.
I have to click on the terminal window in order to bring it to the front, in order to enter my decision on the keyboard.
puts "close webpage?"
if gets =~ /^y/i then 1 ; else; 0; end;
I would like the terminal window to come to the front before it prompts
me for an answer.
I think the question is two fold
Is there a terminal command that tells a terminal window to become
the active one (the one in the front) that would work with mac iTerm.
The Apple script "bringiTermtofront" works in the applescript editor.
tell application "iTerm" to activate
Is there a way to execute a terminal command from ruby.
the ruby code
system "osascript bringiTermtofront.scpt"
brings the iTerm to the front.
For Question 1, one approach would be to write an Applescript to handle the switch, and then use the Terminal command osascript to run it from your Ruby code. You could also check if rb-appscript is still usable (it's no longer supported, but might work).
For Question 2, you have a few choices. Using backticks around the command will let you capture the output of a brief command, if you want to store the result of the command in a variable. (E.g. use grep or something similar).
The system method in Kernel is probably your best choice, though, as it will execute shell commands as if at the terminal.
Per the edit showing what script you're using, you need to execute the script as Applescript, not as a terminal script. You don't even need a separate file as it's just one line. This would be :
command = %q[osascript -e "tell application \"iTerm\" to activate"]
system(command)
You could also put the Applescript in a file and execute it using just
system("osascript bringiTermtofront")
See "Running shell commands from Ruby" for a little more help on how to interact with these methods.

Run mode not there (IDLE Python 3.6)

Probably a very simple question. I just thought, after someone suggested it here, of trying (and installing) Python 3.6 on a Mac - I've been happily using 2.7 since now. I've never used the IDLE before having done everything via the command line + ATOM to write the program.
I see that 'normally' you should be able to write your program in the shell and then run it in the RUN window. However, I don't see a RUN mode in window, just the possibility of using, which you are anyhow, the shell window. I hope that makes sense!
Is this normal, or have I missed something?
p.s. I'm using OS X 10.8, if that's of any importance.
I am not exactly sure what you are asking, and whether it has anything to do with OSX, but I can explain IDLE. IDLE has two types of main window: a single Shell and multiple Editor windows.
Shell simulates python running in the interactive REPL mode that you get when you enter 'python' (or 'python3') in a console or terminal window. (The latter depends on the OS.) You enter statements at the >>> prompt. A single-line statement is run when you hit Enter (or Return). A multi-line statement is run when you hit Enter twice. This is the same as in interactive Python.
Editor windows let you enter a multi-statement program. You run the programs by selecting Run and Run module from the menu or by hitting the shortcut key, which by default is F5 (at least on Windows and Linux). This runs the program much the same as if you enter python -i myprogram.py in a console. Program output and input goes to and is received from the Shell window. When the program ends, Python enters interactive mode and prints an interactive prompt (>>>). One can then interact with the objects created by the program.
You are correct that Run does not appear on the menu bar of the Shell. It is not needed as one runs a statement with the Enter key.

Close all open X-windows with a command in UNIX

I have variable stand alone shell routines which I execute one after another automatically. Since every routine produces several X-windows figures I want to close all of them at the end without modifying every single routine. Is there a certain command?
Cheers!
Based on answers to this question. xdotool looks like what you need.
To kill an X11 window given it's title, you can use:
xdotool search "Your window title here" windowkill
Windows are owned by processes. One way to close windows is to kill their owner processes.
Another way is to start another X server, run your scripts with DISPLAY environment variable referring to the display of that X server, then terminate that X server with all windows.

Can I Force MATLAB to quit after user presses Control-C?

I'm running MATLAB (command line version) from a shell script, and I'd like it to preserve shell behavior where if you press Ctrl-C it exits. But instead it wants to keep control of the terminal and I (or my poor users after me) have to type quit(1) to make it quit and tell the shell it failed.
You can't intercept Ctrl-C with a try/catch block... any other ideas? Anything I could do from the shell side to intercept the keystrokes before they get to MATLAB?
onCleanup seems like an option, but then I'd have to make the whole script thing into a function (it's already a dynamically generated try/catch block thing in a Makefile). But if that's the only thing that will work, then I'll do it...
Use onCleanup:
I wanted to do the same thing but after I read this thread I used onCleanup successfully. My problem was I had a server in Matlab that when pressing CTRL+C would keep listening on the port it was started on -> second run I would get a bind error.
You can try:
stty quit ^C
but I have no matlab to test it.

Resources