Python without a tty

Nobody nobody at nowhere.com
Fri Sep 30 22:09:54 CEST 2011


On Thu, 29 Sep 2011 11:53:12 +0200, Alain Ketterlin wrote:

>> 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.

setsid() requires that the calling process isn't a process group
leader; this can be achieved by fork()ing. The setsid command does this
automatically.

As you say, this ensures that the process has no controlling terminal
(i.e. it won't get signals from the tty driver for ^C, ^Z, hangup, etc).
It won't detach stdin etc from a terminal. Also, open()ing a tty will
result in it becoming the controlling terminal unless O_NOCTTY is used.

I suspect that the OP just wants e.g.:

	script.py &>/dev/null <&-

which will redirect stdout and stderr to /dev/null and close stdin.




More information about the Python-list mailing list