When i compile the following C++ code with g++ under Linuxit works fine, however when i try to run
nohup ./a.out &
it stops immediately with info
[7]+ Stopped nohup ./a.out
the source code is:
#include <iostream>
#include <fstream>
using namespace std;
int main(void){
ofstream file("hm.txt",ios::out | ios::binary | ios::app);
file << "123";
file.close();
return 0;
}
Can you point out my mistake?
I didn’t get it at first, but it seems like your system is deciding to put the program into a suspended (
Stoppedstate).jobsshows you all of the jobs stopped or running in the current terminal session.bg/fgcan be used to restart stopped jobs, so you might check to see if that caused the files to be written. Another idea for further probing the cause is to run/usr/bin/nohup strace ./a.out 1>stdout.txt 2>stderr.txt. Thestraceshould dump all of the system calls tostderr.txt— so you might be able to see an indication as to why it stopped.It appears like a common cause for this type of problem is that the program wants some input and since (with
nohup) there is no terminal to provide it, the system stops your program. My first suggestion was to try the following in order to provide some auto-generated input:On my system, it automatically runs it with this instead:
I don’t know why your system wouldn’t automatically be providing a null input, and I also don’t know why your program would be waiting for input. Your original attempt worked fine for me (I can’t reproduce your problem).
When I run
nohup ./a.out(ornohup ./a.out &), I get the following message, which indicates the automatic input suppression:So another line of investigation is: why don’t you see this message?
Another line of attack, since it seems like your system just doesn’t like
nohupwith&is to restart theStoppedprogram as a background task with something like the following:Clearly,
nohupis meant to be used with&, so your problem is a bit strange.