Script executed by Cron or commandline

Hello all,

I have a question regarding the difference betwen cron and command line.

What I would like to do is to print a statement into a logfile if a script has been executed from cron or from command line.

It should be as:

#!/bin/bash
 
if <Check if this script has been executed by cron or commandline>; then
  echo "Cron" >> $logfile
else
  echo "Commandline" >> $logfile
fi

You could test if you are on a terminal or pseudo terminal which would indicate a command line user. Having neither would suggest a scheduler.

The output from ps -otty= -p $$ will give you this.

I hope that this helps,
Robin

1 Like

Hi Robin,

yes, this works fine. Tx for your quick response. :slight_smile:

You might check for your shell's -i (interactive) option by testing the $- parameter.

1 Like