Issue
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?
Solution
I didn't get it at first, but it seems like your system is deciding to put the program into a suspended (Stopped
state). jobs
shows you all of the jobs stopped or running in the current terminal session. bg
/fg
can 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
. The strace
should dump all of the system calls to stderr.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:
/usr/bin/nohup ./a.out </dev/zero
On my system, it automatically runs it with this instead:
/usr/bin/nohup ./a.out </dev/null
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
(or nohup ./a.out &
), I get the following message, which indicates the automatic input suppression:
nohup: ignoring input and appending output to `nohup.out'
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 nohup
with &
is to restart the Stopped
program as a background task with something like the following:
/usr/bin/nohup ./a.out & bg
Clearly, nohup
is meant to be used with &
, so your problem is a bit strange.
Answered By - Brent Bradburn Answer Checked By - Dawn Plyler (WPSolving Volunteer)