Python without a tty

RJB rbotting at
Fri Sep 30 14:34:37 EDT 2011

On Sep 29, 3:52 am, Steven D'Aprano <steve
+comp.lang.pyt... at> wrote:
> Alain Ketterlin wrote:
> > Steven D'Aprano <steve+comp.lang.pyt... at> writes:
> >> I have a Python script which I would like to test without a tty attached
> >> to the process. I could run it as a cron job, but is there an easier way?
> >> I am running Linux.
> > Isn't os.setsid() what you're looking for? It makes the calling process
> > have no controlling terminal. There's also a user command called setsid
> > that should have the same effect.
> It doesn't appear so to me.
> [steve at sylar ~]$ tty
> /dev/pts/16
> [steve at sylar ~]$ setsid tty
> /dev/pts/16
> [steve at sylar ~]$ python -c "import sys,os; print os.isatty(sys.stdout.fileno())"
> True
> [steve at sylar ~]$ setsid python -c "import sys,os; print os.isatty(sys.stdout.fileno())"
> True
> If I run the same Python command (without the setsid) as a cron job, I
> get False emailed to me. That's the effect I'm looking for.
> --
> Steven

You could try the old UNIX "nohup ... &" technique for running a
process in the background (the &) with no HangUP if you log out:

$ nohup python -c "import sys,os; print
os.isatty(sys.stdout.fileno())" &
appending output to nohup.out
$ cat nohup.out

But that is over kill I guess.

One worrying detail.... the definition of a running process in UNIX
implies is that it has standard input/output files open.
You'd be wise to make sure that they are connected to things that are
safe.... /dev/null.

Even so /dev/tty can be opened any way...

Hope this helps.

More information about the Python-list mailing list