0

I'd like to use a cron job to run a task which involves a python script.

The bash script for the task looks about like:

#!/bin/bash

...
python ./script.py &
...

Which is called via crontab like:

* 8 * * * ./task.sh

This works perfectly for my needs, except this pesky icon when appears in the dock and steals the current input device focus, interrupting anything I might be typing when the task fires.

Screen Capture: Python icon

Is there a way to tell python to run in the background, similar to having run the python script.py command via Terminal.app?

Edit: It looks like Matplotlib is the culprit. https://github.com/matplotlib/matplotlib/issues/12918/ Using matplotlib.use("Agg") to disable the interactive environment while running from CLI prevents the issue.

0

1 Answer 1

0

This is NOT an answer to your problem, but I can't put this detail in a comment, so adding here, and maybe it will help you debug a bit...

I run a Perl script (to backup one disk to another) every night, and I've never seen this behaviour. I just modified the cron to run while I'm watching, and I didn't see this for the Perl script.

So, I tried again with these Bash and Python scripts:

#!/bin/bash

echo Starting Bash...
python3 /Users/jim/Documents/scripts/hello.py &
sleep 3
echo Ending Bash...

and

#!/usr/bin/env python3

import time

print ("Hello, world!\n")
time.sleep(60)
print ("Goodnight, moon!\n")

And a cron job:

04  *   *   *   *   /Users/jim/Documents/scripts/hello.sh >> ~/Library/Logs/test.log 2>&1

I do not get the behaviour you're showing. There is no dock icon for Python (or Perl), in fact I've almost never seen dock icons show up for regular Unix executables like that.

If you right-click/ctrl-click on the Python icon and choose Options -> Show in Finder, which Python is getting called? Is it different than the one that gets called when you run the script manually?

6
  • You’re not doing exactly the same thing which means your differing results aren’t very helpful. I don’t use MacOS but it’s possible, for example, that the fact that you choose to write output to file creates a different behavior.
    – pzkpfw
    Dec 30, 2020 at 14:32
  • It could be, but I don't think so. Besides, all cron jobs should redirect STDOUT/STDERR to a file. You will never know if something is wrong if there isn't a log... More likely, though, would be the different way I use Python 3, but even that's a long shot. It's more likely something that's been configured on the Op's Mac such that Python is a Dock-visible app. That's why I want to know which Python is running when it's visible in the Dock, then we can figure out if that exact Python executable is always visible when run in a similar manner outside of a cron.
    – jimtut
    Dec 30, 2020 at 15:17
  • That's very interesting! I'm running python (3.8.0) via pyenv. From a terminal, if I call python ./script.py I don't get the dock icon. If I call ./script.sh (which itself calls script.py) I also don't get the dock. But for some reason, via cron, if I call either script, I get that python icon which auto-focuses.
    – user124522
    Jan 1, 2021 at 23:46
  • @jimtut I found the issue (with your help reassuring me that python doesn't seem to do this on its own.) One of my dependencies (matplotlib) added this side effect.
    – user124522
    Jan 1, 2021 at 23:55
  • Glad you figured it out. Do you know why matplotlib causes that effect, but only when run under cron? My best guess would be that since cron does not inherit ANY of your environmental variables, that you have some env var in your shell which affects that behavior, and you beee to explicitly add that to script.sh. If so, please share the details here (maybe in a separate answer) so that it can help others.
    – jimtut
    Jan 2, 2021 at 14:33

You must log in to answer this question.