How do I handle Ctrl-C interrupt in nim? - nim-lang

If I press Ctrl-C while my program is running it exits and prints SIGINT: Interrupted by Ctrl-C.
How do I ignore the Ctrl-C interrupt in Nim on Linux?
Thanks in advance.

You can control the behaviour of Ctrl+C with setControlCHook:
proc ctrlc() {.noconv.} =
echo "Ctrl+C fired!"
setControlCHook(ctrlc)
Now CtrlC calls the ctrlc procedure. It's up to that procedure to ignore the SIGINT, or to clean the house and exit with quit.

Related

Exit a shell script immediately with subprocess

I have a while loop in a shell command:
sp = subprocess.Popen("while [ 1 ]; do say \"hello world\"; done;").
However, when I send sp.kill(), the loop does not terminate immediately. Rather, it finishes the iteration it is on and then exits. I notice that the version without the loop, "say \"hello world\", will terminate immediately. I've tried sending the various C codes using signal, but nothing seems to work. How can I immediately exit from the loop?
I think the best way is killing the process using kill command with -9 option for sending SIGKILL signal. This doesn't let the process to handle the signal and it terminates it entirely. There are other ways for sending this signal. Like os.kill()
You just need to figure out what is the UID of the process and then kill it.

A script wrapper that turns SIGINT into SIGHUP

I use Leiningen REPL that uses SIGINT to interrupt currently running code and to output a new prompt. The REPL can be stopped using SIGHUP or SIGKILL. I don't actually run anything in the REPL - I just use it for some pre-defined side-effects.
The problem is that IntelliJ IDEA can only send SIGINT when it exits to the processes that it has started. So if I forget to kill a REPL started from IDEA, there'll be a dandling process that I have to kill manually.
Is it possible to write a shell script that starts the REPL, gives it some dummy stdin/stdout (otherwise, REPL immediately quits), and waits for the process to end, while also forwarding it all signals, transforming SIGINT into SIGHUP or SIGKILL?
Open a terminal window and start the REPL inside of it. The process will terminate when you close the terminal window or when you press Ctrl+D (Ctrl+Z on Windows).
You can then minify the terminal window, so it doesn't annoy.
This Python code does what's needed:
from subprocess import call
import sys
try:
call(sys.argv[1:])
except KeyboardInterrupt:
pass
All its parameters are the command line that you want to be executed. The running application will always be closed by SIGINT.

How to start C Program on Server to run in background

I have programmed a C program and tested it on my linux computer.
I have uploaded it to my server now and want it to run there the entire time as a background progress.
How is it possible to start a program which will continue to run after i close putty?
Thanks for any help!
you can leave the program running by adding a & to the call
So when you have been executing your code before like this in SSH:
./mycode
you would to it like:
./mycode &
If you want running your program in background you must add the '&' character to command.
./program &
But the process will be associated to the current tty. If you close the current tty (in your case the SSH session) the SIGHUP signal is sent to all processes associated with the tty, causing them finish. You can see the associated tty to one process with ps command.
The disown command can be used to mark jobs so that a SIGHUP signal is not sent to them if the parent shell receives it. Before closing the current tty you can write:
disown %jobID
You can see the jobID enclosed in brackets when you run the program in background. Also you can see the jobID with jobs command.
You can run the command directly so that a SIGHUP signal is not sent to him. For this you can use nohup command.
nohup ./program &
Another way to avoid ending the program with the SIGHUP signal is to catch this signal in the program. In a C program you can do this with signal function that is in signal.h
But if you want run the program as a daemon or server, better write a Systemd service or a System V init script.
In the middle of the executing your program, if you want to run your program in background.
First press ctrl + z then type bg.

How to remove ^c mark from own shell?

In normal terminal, If we run command like 'top' and when we pass interrupt signal through ctrl+c, command terminates But in my own shell it also terminate but also print ^c in the terminal. How can I prevent to print ^c. I need system call to do this.
This should do it:
stty -echoctl
Edit (after OP specified that a system call is needed): See man tcsetattr and the non-POSIX ECHOCTL terminal attribute.

Kill runhaskell process on Windows

I've got a haskell program I'm executing with runhaskell on Windows. The program sits in an infinite loop listening on a network socket. I can't kill the program with ctrl-c, ctrl-d or ctrl-z and my keyboard doesn't have a break key.
Is there anything else I can try to kill the process without having to resort to task manager?
taskkill /IM runhaskell
I'm guessing at the process name here; if not runhaskell replace it with what it really is.

Resources