0

I am using Strawberry Perl v5.22. I'd like to be able to determine when a script is being run in the console manually vs as a scheduled task, so as to alter the behaviour (e.g. change the log level to be more verbose and output to STDERR).

I've tried IO::Interactive but it always seems to say that the script is running in interactive mode. I think because Windows always runs a script in a new console, even when the task is run as the SYSTEM user.

And I can't rely on the script running as the SYSTEM user, because in a few odd cases a script needs to run as Administrator.

Rob
  • 781
  • 5
  • 19

1 Answers1

1

Under a normal interactive console the PROMPT environment variable is defined, while it is not when .PL started from Windows Explorer (double click).

I don't know if this will be the same behaviour under Windows scheduler as it is when directly using Windows Explorer, but you might try this (which works differentiating between interactive console and double-click from Windows Explorer under W7 and W10, professional editions, at least):

BEGIN {
    $^O =~ /MSWin/ or die "This programme must be run under Windows.\n";
};

use strict;
use warnings;
use 5.016;

if ($ENV{PROMPT}) {
    print "This programme was started from an interactive console.";
}
else {
    say "This programme was started directly from Windows Explorer.";
    print 'Press <return>...'; <>;
};

I suspect it might even be used under Unix: it seems to remember that, back in the days, the prompt was not defined at cron job when using Korn shell under OSF1 (True64 Unix) from Digital.

NB: if this does not work you might try to encapsulate your Perl job in a .CMD batch file and test the difference between %CMDCMDLINE% and %COMSPEC%, this is the usual way to check if a batch file is run from a console or not. But this is beyond the scope of your question.

Dharman
  • 30,962
  • 25
  • 85
  • 135