Thursday, August 18, 2022

[FIXED] How to kill/stop a running process in a terminal without losing its output?

Issue

I try to run a simple c++ code in a terminal and try to store its output in output.txt.

CODE-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(1)
    {
        cin>>n;
        cout<<n;
    }
    return 0;
}

using a terminal commands:

g++ code.cpp
./a.out > output.txt

and input:

1 
2
3
4
5

but when I terminate the program by Ctrl+C, output.txt is empty. Thanks for the help in advance.


Solution

for me even it is working as you expect. I think maybe your task is not closed! so try two things: 1- try to close the terminal after crtl+C. after again check the content of output file 2- please give a good name instead of a.out(testfile.out) and after crtl+c try below command in shell:

sudo ps -aux | grep testfile

to see that your process either was killed or not



Answered By - SpongeBob
Answer Checked By - David Marino (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.