Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.