Robert Kock
Robert Kock

Reputation: 6028

Detect java program started from crontab

On a Redhat OS, I have a script that launches a Java program. This script can be started from the command line but is launched (periodically) by crontab as well.
Within this program, I need to know how the program was started. This because the output is written either to STDOUT (if started from the command line) or in a logfile (if launched by crontab).

First I thought I could use System.console().
The problem is that this method returns null also if the program was started from the command line but with STDIN and/or STDOUT redirected.

Any idea how to resolve this?
I tried How can I check if a Java program's input/output streams are connected to a terminal? but that doesn't answer my question.

Upvotes: 0

Views: 138

Answers (2)

Jim Garrison
Jim Garrison

Reputation: 86774

Lots of options:

  1. Add a -Dcron=1 command line option when running from cron, to set a property that can be checked
  2. Add a simple argument to the command line when running from cron and check it by looking in the args[] array
  3. Set an environment variable in the script and check it in the program.

Upvotes: 3

kutschkem
kutschkem

Reputation: 8163

Use an environment variable that you set in the cron job before starting the java program. Query the environment variable inside your program.

Upvotes: 0

Related Questions