1

My question:

I have an if statement in perl where I attempt to detect via environment variables at the time of launching the perl script whether it was launched via a cron job or via normal execution through the shell.

Is there any way to set an environment variable in the crontab itself that a program launched by cron would be able to access?

Essentially, I want this in the top of my crontab:

COMPANYCRON=1

* * * * * someperl.pl

And in someperl.pl this if statement:

if ( $ENV{'COMPANYCRON'} eq '1' ) {
    $job_creator = "cron";
}

Is there a way to do this? Or a better approach to keeping track of cron jobs?

perlnovice
  • 11
  • 1

1 Answers1

1

Some cron daemons (e.g. Vixie cron on Debian/Ubuntu) allows the exact same syntax you wrote in your example. On other systems (RHEL etc.), use something like this in your crontab:

* * * * * export COMPANYCRON=1; someperl.pl 
Sven
  • 97,248
  • 13
  • 177
  • 225