[Tutor] interactif or not

Michael P. Reilly arcege at gmail.com
Sat Jun 4 19:32:46 CEST 2005


On 6/3/05, Cedric BRINER <work at infomaniak.ch> wrote:
> 
> hi,
> 
> How can I know if a script is launched interactively or not because I'd 
> like to make a script verbose or not depending if it is executed as 
> interactive or not.
> 
> eg.
> 
> If I invoke it in a shell.. then it can be verbose
> 
> If it is launched from a crontab.. then it is less verbose.
> 
> Ced.
> 

If you want to know if your program has been launched from an interactive 
terminal or from a system background process like cron or init, then you 
look to see if stdin is a tty. Unix is usually very careful about who has a 
"controlling tty" and who does not. In Python, file objects have an isatty() 
method that will return True or False.

import sys
isinteractive = sys.stdin.isatty()
if isinteractive:
...
else:
...

How you make a program this way manually (i.e. to make a program into cron) 
is slightly more complicated, but still all the tools are there in Python.

-Arcege
-- 
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050604/6fbefcc4/attachment.html


More information about the Tutor mailing list