0

I created a python script which is usually run by a cron job, but the script can at times be run manually by a human. Is it possible to determine who ran the script and saved it in a log file?

I'm using python's logging library. It seems the LogRecord attributes name only shows the root as being the logger used to log the call.

log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Rahoul
  • 35
  • 6
  • 3
    Maybe you can add a flag parameter to your script when calling the command in cron ? There's some answers [there](https://stackoverflow.com/questions/4213091/detect-if-python-script-is-run-from-console-or-by-crontab) – Plopp Mar 09 '22 at 15:15
  • Nice, i'm going to have a look at it. Thank you – Rahoul Mar 09 '22 at 15:21

1 Answers1

2

How about using command line options?

https://docs.python.org/3/library/argparse.html

When triggering the script from cron use a special argument that defaults to something else if not explicitly set.

LTJ
  • 1,197
  • 3
  • 16