WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Background program stopped because of tty output  (Read 2749 times)

Offline zharr

  • Newbie
  • *
  • Posts: 24
Background program stopped because of tty output
« on: May 23, 2022, 05:46:28 PM »
Hello,
I am trying to get my program constantly running for an embedded application and face some problems.
I'll try to split this in hopes this circumvents the current server issues (another poster did this successfully)
 - Yeah no chance, I'll try attaching my post as txt until I can post it
« Last Edit: May 23, 2022, 05:50:16 PM by zharr »

Offline zharr

  • Newbie
  • *
  • Posts: 24
Re: Background program stopped because of tty output
« Reply #1 on: May 23, 2022, 05:50:44 PM »
Here's my intended post as a txt

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: Background program stopped because of tty output
« Reply #2 on: May 23, 2022, 07:18:51 PM »
Hi zharr
Quote
So testing sudo ./program 2>&1 >> output.log & reveals the problem:
Does it work any better like this:
Code: [Select]
sudo ./program >> output.log 2>&1 &

Offline gadget42

  • Hero Member
  • *****
  • Posts: 657
Re: Background program stopped because of tty output
« Reply #3 on: May 24, 2022, 01:48:10 AM »
worked through your attachment and the server didn't like the tabs(using spacebar worked though)...

Hello,
I am trying to get my program constantly running for an embedded application and face some problems.

I add my program to bootlocal:

Code: [Select]
while :
              do
                      ./program 2>&1 >> output.log
                      sleep 5s
              done

However this does not work - the program is always stopped.

Executing sudo ./program 2>&1 >> output.log in the shell works.

This is in bootlocal, which itself is a background script.

So testing sudo ./program 2>&1 >> output.log & reveals the problem:

[1]+  Stopped (tty output)       sudo ./program 2>&1 1>>output.log

Looking this up implies it's stopped because it has tty output, but is a background process which should not write to stdout.

Now I wonder:
a) How am I writing to stdout? I am explicitly trying to write to a log file.
b) How can I disable this behaviour? A similar question suggests to use stty -tostop but this has no effect.
c) Is there any better/recommended way to create a constantly running, logging-enabled service in tinyCore?

Thanks for any help!


also the
Code: [Select]
[tt] ... [/tt] stuff didn't work(and is depreciated as per https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tt)
The fluctuation theorem has long been known for a sudden switch of the Hamiltonian of a classical system Z54 . For a quantum system with a Hamiltonian changing from... https://forum.tinycorelinux.net/index.php/topic,25972.msg166580.html#msg166580

Offline zharr

  • Newbie
  • *
  • Posts: 24
Re: Background program stopped because of tty output
« Reply #4 on: May 25, 2022, 11:06:59 PM »
Hi zharr
Quote
So testing sudo ./program 2>&1 >> output.log & reveals the problem:
Does it work any better like this:
Code: [Select]
sudo ./program >> output.log 2>&1 &
Sorry for the late reply, got sick.
No, same result unfortunately.

Offline zharr

  • Newbie
  • *
  • Posts: 24
Re: Background program stopped because of tty output
« Reply #5 on: May 25, 2022, 11:18:19 PM »
Interestingly, normal commands that take a while end with another code printed in the console, but always Done(1), e.g. this after about 3 seconds, and these two commands work as expected:
Code: [Select]
find *.conf / >> output.log  & prints stderr, and stdout to file
Code: [Select]
find *.conf / 2>> output.log  & prints stdout, and stderr to file
So writing to tty as a background process is probably not the issue I face, maybe it has something to do with my program after all

Offline zharr

  • Newbie
  • *
  • Posts: 24
Re: Background program stopped because of tty output
« Reply #6 on: May 26, 2022, 09:44:58 AM »
Alright, was my program, namely the following:
Code: [Select]
tcgetattr(STDIN_FILENO, &terminalSettings);
struct termios termSet = terminalSettings;
atexit([]{ // Reset at exit
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &terminalSettings);
});
termSet.c_lflag &= ~ECHO;
termSet.c_lflag &= ~ICANON;
termSet.c_cc[VMIN] = 0;
termSet.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &termSet);
which I set up at some point to correctly receive input in the terminal as well as over SSH.
Though there is one other part later that also triggers the same, so I'll have to search more tomorrow.

Offline zharr

  • Newbie
  • *
  • Posts: 24
Re: Background program stopped because of tty output
« Reply #7 on: May 27, 2022, 04:55:37 AM »
Aand the other trigger was a
Code: [Select]
read(STDIN_FILENO, &cin, 1) to get input.
Of course for background not relevant, still annoying that I have to add a switch to the program for background execution. Maybe there is a way to detect it?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
Re: Background program stopped because of tty output
« Reply #8 on: May 27, 2022, 06:18:30 AM »
Hi zharr
... Maybe there is a way to detect it?
I think you can use  isatty()  for that:
https://man7.org/linux/man-pages/man3/isatty.3.html

Wrapping something like this around your TTY code should fix it:
Code: [Select]
#include <unistd.h>

if(isatty(STDIN_FILENO))
{
    // Place code requiring a TTY here.
}

That should also work if the program gets switched back and forth (foreground, background, foreground, ...) while
it is running.

Offline zharr

  • Newbie
  • *
  • Posts: 24
Re: Background program stopped because of tty output
« Reply #9 on: June 03, 2022, 09:03:14 PM »
Thank you very much, will do that!