Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How was my script ran?

by jsprat (Curate)
on Mar 05, 2003 at 01:26 UTC ( [id://240506]=note: print w/replies, xml ) Need Help??


in reply to How was my script ran?

How about testing if STDIN is a tty - if yes, the script is run from the command line. If not, it's a CGI or the e-mail wrapper. CGI sets several HTTP_* variables, check one of these. If set, it's CGI. If not, e-mail wrapper.

# Note - not very thoroughly tested if (-t STDIN) { # command-line } elsif (defined $ENV{SERVER_NAME}) { # CGI - I used SERVER_NAME, as it _should_ be set } else { # e-mail }

Now I can't guarantee how portable this is, as I didn't test the e-mail part at all - and I can only test Win2K and FreeBSD. Good luck!

Replies are listed 'Best First'.
Re^2: How was my script ran?
by LAI (Hermit) on Mar 05, 2003 at 15:33 UTC

    That's a good idea, but if the script is being called by cron or at (or Windows Scheduled Event :o), will STDIN be a tty? I have no idea, come to think of it, as what STDIN would be identifiable in that circumstance. Or is the point moot -- will this script never be automated?

    LAI
    :eof

      Yeah, I thought of that too. Another one - if the script gets its input via a pipe, the -t will return false. I'm sure there are more scenarios that we haven't thought of yet.

      BTW, windows actually starts a shell for a perl script, so -t returns true. On FreeBSD (and other *nix, I'm sure), -t returns false for a cron job.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://240506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found