Crontab job works only once

I created 60 crontab jobs to run python script every second and the python scripts it was just a test and it only contain the following:

import notify2
from datetime import datetime

notify2.init('')

n = notify2.Notification('test', datetime.now().strftime('%I:%M:%S'))
n.set_timeout(1000)
n.show()

and crontab job is:

* * * * * DISPLAY=':0' XAUTHORITY='/run/user/1000/gdm/Xauthority' python3 /path/to/my/script.py
* * * * * ( sleep 1 && DISPLAY=':0' XAUTHORITY='/run/user/1000/gdm/Xauthority' python3 /path/to/my/script.py )
* * * * * ( sleep 2 && DISPLAY=':0' XAUTHORITY='/run/user/1000/gdm/Xauthority' python3 /path/to/my/script.py )
.
.
.
.
.

the first time it waits 1 minute and the start running them, but not all of them will be executed and they never executed again.

I add a crontab job to test if it will work (* * * * * env > /home/hadi/Desktop/env.output) and worked fine. Here is the output file content:

POWERSHELL_TELEMETRY_OPTOUT=1
DOTNET_CLI_TELEMETRY_OPTOUT=1
HOME=/home/hadi
COMMAND_NOT_FOUND_INSTALL_PROMPT=1
LOGNAME=hadi
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
JAVA_HOME=/usr/lib/jvm/jdk-15.0.2
PWD=/home/hadi

So I guess the problem is with the notification, how to fix this?

and one more thing, DISPLAY=':0' some times I should set it to 1 not 0 I do not know why


Solution 1:

With a postfix &, you may start your python script in the background. Even if an instance is blocked and hangs forever or finishes in a small fraction of a second, the next job will start a second later.

#!/bin/bash

while true 
do
  sleep 1
  python3 /path/to/my/script.py &
done 

Cron is not suitable for finer time granulation than minute. Systemd timers are usable for finer granulation, but if you don't want to restrict the time at all, the job should simply be started when appropriate in the startup procedere.

Since your code uses DISPLAY=:0, it might be started as a systemd unit, depending on graphical.target, to avoid calling it, before the graphical system is up.

The looping script might accumulate larger amounts of time discrepancies over time, and 3597 times per hour instead of 3600, but maybe "roughly once per second" is good enough. If not, you have to measure the time yourself and correct yourself.

Good introduction to systemd are available on YT, for instance.