I want to run a program which continues after logout from an ssh session.
I've tried:
nohup myProg > /tmp/logfile 2>&1 &
disown %1
and
systemd-run --scope --user myProg -p > /tmp/logfile 2>&1 &
In both cases, the process receives a SIGTERM from the bash process which launched it.
(The program uses sigaction for signal handling, which identifies the PID that sent the signal)
struct sigaction sigActionInfo;
sigActionInfo.sa_sigaction = DvsRebuildInfoFiles::signalHandler;
sigActionInfo.sa_flags = SA_SIGINFO;
sigemptyset(&sigActionInfo.sa_mask);
sigaction(SIGINT, &sigActionInfo, 0);
sigaction(SIGTERM, &sigActionInfo, 0);
and
DvsRebuildInfoFiles::signalHandler(int sigNo, siginfo_t *sigInfo, void *context)
{
pid_t sendingPid = sigInfo->si_pid;
printf("DvsRebuildInfoFiles: Received Signal %d from process %d\n", sigNo, sendingPid);
}
Watching the log file from another terminal session confirms that it is bash which sends SIGTERM.
# ps -lf
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
0 S root 858355 858354 0 80 0 - 59839 - 03:40 pts/0 00:00:00 -bash
0 S root 858530 858355 12 80 0 - 944937 - 03:40 pts/0 00:00:01 ./DvsRebuildInfoFiles -p
0 S root 858906 858530 0 80 0 - 55622 - 03:40 pts/0 00:00:00 sh -c /usr/local/dvstor/bin64/DvGetProgramList /mnt/das.a/202307
0 D root 858907 858906 0 99 19 - 35888 - 03:40 pts/0 00:00:00 /usr/local/dvstor/bin64/DvGetProgramList /mnt/das.a/20230719.085
etc. etc.
Why does it do that and can it be prevented?
NOTES
I don't know if this is relevant but the program itself launches many threads, each thread in turn executes another program via
system().Recoding the signal handler is not an option because the program must be able to be stopped with a signal.
/etc/systemd/logind.confhas the line commented outAccess is via ssh. No GUI.