Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 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 
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.