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

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.

Related

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?
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

Mongo shell stops responding to commands

I am playing around a little with the mongo shell, but I have quite a tedious problem.
After I've switched to the database I'm working with using "use nameOfDatabase", and I issue my first command, the shell won't respond to any further commands after giving me the first response. I have to CTRL + C or CTRL + Z out of it, reconnect, switch to the db again and then issue another command to keep working.
Also, accessing commands from the history does not work since this just moves the cursor around the shell window (this also makes the shell stop responding).
Anyone have any ideas what might be wrong?
Ctrl+Z is for 'suspend'. Try Ctrl+D to exit the mongo prompt.

Run script in background?

Simple question: Is there a way to run a script in the background with out terminal running?
More detail and background: I had an app that read an apps .log file and puled information from it, then provide information and statistics from the information in the log.
An update to the app changed the way the .log file was written and delete information and duplicates the log in a manner that i have been unable to predict.
the app that was designed to interface with the log was not coded to check for such changes. so when it attempts to gather information after the log change it stops working.
A "hack" has been devised to run a tail -f, then hexed the app to point at the new file.
(The "hack" works)
I would like to run the tail in the background so that the user doesn't interrupt it... breaking it...
-sorry for the (possibly) longer than needed description. BUt i figured a more detailed question would get me a precise answer.
Thanks in advance!
~¥oseph
The answer depends on if you need to be able to re-connect to the process after exiting the shell. If the process is non-interactive and can simply be left alone, then "nohup program &" should do the trick. But that won't let you continue to interact with the program after you've closed the shell.
If it's a interactive program, then your best bet is to use screen or one of the other terminal-multiplexers. You start "screen" which gives you a new shell, in this you start whatever program you want, the usual way, say "nano myfile.txt".
When you want to close the shell, but leave the program running, you press C-a d ('Detach') to detach from screen. it keeps running, but in the background, and will keep running even if you log out.
When you then later want to reconnect to screen you open a new shell and type "screen -r" (reconnect), this leaves you right where you where.
Screen also lets you run several different shells in a single terminal-window and is a neat tool overall. Check it out.

Suspending a process in bash running inside console2

In other terminals I would press Ctrl+z to suspend an application, often to then issue bg to send it into background.
In console2 Ctrl+z does nothing, probably because the key combination has a different meaning in Windows. But is there a way to achieve the same effect, save for 'process &'?
(I know I should use & and it works, but sometimes I would setup shell, start an editor, begin editing, then return to the console just to find that I forgot the & and I can't use the shell. It annoys me that I then have to either open a new shell and set it up again, or quit the editor, start it with & and set it up again).
In Console2, ctrl-c is bound by default to copy (text) you have to press ctrl-shift-c to cancel job.
You could also try to remove the hotkey that is conflicting with ctrl+z.
I was having trouble using ctrl+c to cancel the execution of a script. As soon as I removed that hotkey, ctrl+c had its default restored and I was able to cancel executions properly.
This annoying issue surely is a console2 bug. You may find something about at its issue tracker.
Anyway, what ctrl+z does is send a SIGSTOP signal to current process. So, you can still send that signal from another session/tab. (If it is less annoying than stop and start with &).
To do that, you can use the kill command.
kill -s SIGSTOP pid
(pid is the process PID number)
Hope it helps.

My Ruby files don't run correctly

When i click on a .rb file to run it a CMD window pops up for a brief second and then closes again. This is probably a really nooby question thats easily fixed but i've looked everywhere for help. But like i said it pops up for a brief second and i THINK while its up its executing the code but when the codes done it closes so i don't know if i'm making mistakes in code or other important things like that.
Run the program through the command prompt (CMD), that way you can see the result, otherwise the window closes when the program exits.
Alternatively, you can prevent the program from exiting by putting some sort of blocking instruction at the end of the script, such that the program waits for user input before exiting.
Press Windows_Key+R and then type CMD. Browse to the location of the file and then type ruby your_ruby_file.rb. The program is running, but Windows automatically closes the window upon completion of the command.
To get Windows to run your *.rb files through Ruby when you click on them in the UI, you have to associate the .rb extension with the ruby.exe executable. Such an association is called a "Windows File Association." Here's a Microsoft Knowledge Base article that'll tell you how to create such a thing.
http://support.microsoft.com/kb/307859

Resources