Kill runhaskell process on Windows - haskell

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.

Related

Process don't response to signals

Using CLI I try to close/send to background a process which loads at startup - the first thing I see is that process and not the regular shell.
Ctrl x/c/z don't work. Kill -9 does (connecting with ssh and send the signal)
If I kill the process (which loads at startup) and then execute it again , it will reapond to each signal.
I try to figure out what could be the problem
I can't post the program's source (related to workplace). Lets say it's a simple printf("hello world\n") , scanf and return. since this program autostart during kernel startup (with inittab script..) is it possible that it starts before the bash shell and thats why it can't recieve these signals?

How do I terminate a command that runs infinitely in shell script?

I have this command in my shell script that runs forever- it wouldn't finish unless I do ctrl-c. I have been trying to look up how to send ctrl-c signal to script and all the answers have been some sort of kill $! or kill$$ or such. My problem is that the command never finishes, so it never goes on to the next command like my "kill" commands or anything else. I have to manually hit the ctrl-C in my terminal for it to even execute kill $!. I'm sure there is a way to work around this but I am not sure what. Thanks in advance!
There are several approaches to this problem. The simplest (but not most robust) is (perhaps) to simply run your long running command in the background:
#!/bin/sh
long-running-command & # run in the background
sleep 5 # sleep for a bit
kill %1 # send SIGTERM to the command if it's still running

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.

What if we close the terminal before finishing the command?

Let me explain better. What is gonna happen if I run a command in Linux and before it's done and you could enter another command I close the terminal. Would it still do the command or not?
Generally, you must expect that closing your terminal will hangup your command. But fear not! Linux has a solution for that too!
To ensure that your command completes, use the nohup argument first. Simply place it before whatever you are trying to do:
nohup ./some_program
nohup ./do_a_thing -frx -file input_file.txt
nohup grep "something" giant_list_of_files/* > temp_file.txt
The nohup command stands for "no hangup" and it will ensure that the command you execute continues to run, even if you close your terminal.
It depends on the process and your environment (job control shell options, VNC, etc). But typically, no. The process will get a "hangup" signal (message) from the operating system, and upon receiving that, will quit.
The nohup command, for example, arranges for processes to ignore the hangup signal from the OS. There are many ways to achieve the same result.
I would say it will abort att the status you are in just before the session close.
If you want to be sure to complete the job, you will need to use the nohup command.
http://en.wikipedia.org/wiki/Nohup
Read about nohups and daemons (-d)...
A good link is [link]What's the difference between nohup and a daemon?
Worth look at screen command, Screen command offers the ability to detach a long running process (or program, or shell-script) from a session and then attach it back at a later time.

Resources