[Tutor] Running a script in the background

Steven D'Aprano steve at pearwood.info
Mon Sep 3 03:03:02 CEST 2012


On Sun, Sep 02, 2012 at 03:14:53PM -0700, Ray Jones wrote:
> This is only tangentially related to the thread. Someone mentioned that
> so long as a script didn't require user input or output to the user, it
> could run silently in the background. But is there a way for a Python
> (2.7.3) script to determine whether it was called by the user or called
> by something like cron or kalarm? That way user inputs could be used
> when called by a user, but defaults could be used if run by a bot.

The usual way to detect this is by checking whether or not there is a 
terminal available.

os.isatty(sys.stdout.fileno())

If the script is running directly in a console, isatty will return True; 
if it is running from cron, or via a pipe or similar, then it will 
return False.



-- 
Steven


More information about the Tutor mailing list