Hi, I didn't work with Perl for ages but recently I needed it for a small task. So basically the script will run once every day by a cronjob (no HTML output). It also will have some info to show when called from a browser like https://www.example.com/cgi-bin/myscript.cgi

So my question is if it is possible to reliable distinguish - inside the script - if it was called by crontab or by HTTP request (some envs or something)? It will be running at Apache.

(Update) Thank you all for your help. I eventually went by if (exists $ENV{REQUEST_URI}) {web call} else {not web call} By "some definition of "reliable" it is plenty good as I see it. Unless some sysadmin goes nuts with system configs - but it is in any case a battle one can fight but cannot win. Because if a sysadmin mangles with REQUEST_URI or GATEWAY_INTERFACE - he/she equally may install Perl to /usr/bin/foo/bar/current_day to make the shebang dynamic - or anything equally crazy. My current hosting provider is not of kind, so everything is fine.

A more bulletproof solution as I read it: to have 2 separate scripts (modules). One outside of /public_html or its equivalent so cannot be reached by a web-call (for crontab), other inside /public_html (for web access). Also some file for interscript communication and maybe some higher level script for the main business logic. The pay back for this: a violation of the KISS principle - instead of of one place where something may go wrong there will be 3-4 places where something may go wrong. And for outside of /public_html something may go really wrong. So the pay off doesn't cover the back back, IMO.


In reply to How to distinguish a crontab call and a web call by VK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.