powershell bash loops are randomly stuck waiting for keyboard input - bash

I have bash script I am running from powershell in windows that does a for loop. Every once in a while, one of the loop iteration hangs until I hit enter on the keyboard.
This doesn't happen all the time, in fact, it happens pretty rarely, but it still does.
The interesting thing is that my loop innards is basically time _command_ and so after I hit enter, it'll tell me how long the command took to run. The command actually takes way less time to execute than the loop iteration takes - because it's waiting for keyboard input for some odd reason.
It's pretty annoying to leave a script running overnight and come back in the morning to see that it didn't get very far.
Does someone knows WHY this happens and WHAT to do to get around it?
Thanks,
jbu

I have encountered the same problem several times. Now I guess I have found the reason!
If you ever press the mouse within the powershell, it might get stuck and need user to press "enter" to continue. So the get-around-way is to make sure that you didn't accidentally press your mouse within the shell window while you are already running some program...

Goto the powershell properties and unselect 'Quick Edit'/'Insert' check boxes. If these are selected, the console pauses output and resumes only when an Enter key is pressed ( You can identify this by monitoring the console title bar- it will switch from "Administrator:Windows PowerShell" to "Select Administrator:Windows Powershell"

Until you post the script, there's little we can do to help.
However, in general, one of your commands probably returns a null once in a while as input to stdin of another command which, upon seeing null looks to the terminal as stdin. Or something along those lines.

Related

Waiting for closing of previously started program

I would like to ask you how to specify waiting for closing of specific program which was started before. I am showing here an example with command waitfor but unfortunately I don't know how to write it correctly, therefore I am asking you for help.
system('"C:\Program Files\Google\Chrome\Application\chrome.exe" &');
waitfor "closing of chrome.exe"
You should follow the advice in this other Q&A to find the PID of the new program right after you start it. Then, in a loop, check to see if that PID is still running (using again the same process as before), and pause(1) to avoid checking too frequently.
I guess you can do something like:
time_in_seconds=60
while ~isfile("output_folder/file.db")
pause(time_in_seconds)
end
Note that this program, as is, requires that file to exist at some point, otherwise infinite loop. Make sure you put safeguards to end it in case the file does not get created (like a time limit).
But I am not sure it makes sense to have MATLAB waiting for 40 minutes for a script to finish...

How get back to the last command input postion in PowerShell?

You want to view the return information of the last command from the beginning, because that information is relatively long. If I look for it manually, it will be very troublesome. So if I want to quickly go back to where typed last time,.
There are alternatives, of course
Use "less" or "more" program to make the message shorter
Use the history function to re-enter the command
However, the two solutions have their own problems."less" and "more" are not provided in the PowerShell, and they need to be provided additionally. In addition, sometimes the command has been executed and cannot be added "less" or "more" again. If you re-enter the command, you may not get the same result as last time.
It can't be found in the' cursor movement functions' of 'psreadline'. You can't find the result by Google searching for 'PowerShell return to last input position'.
It seems that this demand is very basic. Is there really no way to realize it?

How can I track down why my zsh session is starting with a non-zero RC?

In my ~/.zshrc, I have my PROMPT set up like this:
PROMPT='%? '
This displays the exit status (return code) of the last command executed as the prompt (in actuality the PROMPT is more sophisticated, but I've simplified it for this question).
I have quite a complex ~/.zshrc and ~/.zprofile - I use several plugins via zplug - and somewhere, in the last few months, a change has crept in that causes every new zsh session that is opened, or at least every new login session (I run it inside iTerm2 on OS X with "login session" enabled), to display an initial return code of 1. Hitting return or typing "zsh" to start a nested session does not display a 1 return code in the subsequent prompt, but 0 as expected.
I assume this means that somewhere in my ~/.zshrc or ~/.zprofile something is failing, and making its way through to the prompt. Unfortunately I've long since lost track of the change I made which might have caused this. I've tried semi-systematically removing entries from those files to try and narrow it down, but I haven't had much luck.
Are there any tricks I can apply to trying to find what is causing the problem? For example, can I show the last command which was executed that resulted in a non-zero return code?
To be clear, this is nothing more than a little obtrusive; as far as I can tell it doesn't have any other negative side effects.
In the end I figured this out. It turned out that some rogue install process had put commands I wasn't expecting in ~/.zlogin. I figured it out by progressively commenting out content in my ~/.z* files.

Listen and react to keypresses while another process is running?

I have this nifty couple of lines in my .bashrc that clears my unix terminal when I press shift+alt+c:
alias cls='printf "\033c"'
bind -x '"\eC":"cls"'
However, as you could probably guess, it only works when I'm actually at the prompt. If I'm in the middle of a program such as, let's say, tail -f, then I have to get back to the prompt, press shift+alt+c, then go back to the program (or ctrl+z, shift+alt+c, then fg).
I'm thinking it would be cool if I could somehow listen for keys while this other program is running. It's probably impossible, but I figured I'd ask here before I gave up. Any ideas?
Thanks!
Not going to work. That's your shell doing that.
Your shell isn't active when something else is.
You could do this sort of thing with your terminal if you really wanted to though (assuming it allows you to control it sufficiently) since your terminal sees the keys first.

What is wrong with running unix processes with &?

I have seen tools to daemonize processes.
But I have seen that:
some_command &
Runs process in a daemonized way, is this way bad? how is this way called?
Update
My doubt is that I am calling that command inside an ssh session, will the process last after closing ssh session?
That's not daemonized, that's simply running it as a background process.
A true dameon is a lot more involved; see e.g. http://en.wikipedia.org/wiki/Daemon_(computer_software)#Creation.
The & is an important little character in UNIX; it means "run the command in the background"; i.e., detach it from the window it was started from, so it does not block the command line.
Should the program ever try to read from the terminal window, it will be suspended, until the user "brings it to the foreground"; i.e., brings it to the state it would have had without the & to begin with.
To bring a program to the foreground, use "fg" or "%". If you have more than one background job to choose from ("jobs" will show you), then use for example "%2" to choose the second one.
Important:
If you forget to give the & at the end of line, and the process blocks the command input to the terminal window, you can put the process in the background "after the fact", by using Ctrl-Z. The process is suspended, and you get the command prompt back. The first thing you should do then is probably to give the command "bg", that resumes the process, but now in the background.
http://www.astro.ku.dk/comp-phys/tutorials/background.shtml

Resources