Score:1

Crontab job works only once

fi flag

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

za flag
You shouldn't run 60 cronjobs. If you can't do it in Python, start a script, which starts the 60 jobs (by a loop, for instance). Better: Don't start it with cron, but xinitrc, keep it running and pause therein for 1s, and restart the job on failure.
HaDi AyOuB avatar
fi flag
what does that mean? @userunknown
HaDi AyOuB avatar
fi flag
I want the same script to be executed in each second.if I used a loop the execution will block the loop until execution of the current job is finished
za flag
No, you can start the core command in the background.
HaDi AyOuB avatar
fi flag
Ok, how to do that? could you explain more please?
Score:1
za flag

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.

HaDi AyOuB avatar
fi flag
if I executed my python3 script in a loop and this script uses the `request` module, then the loop will be blocked until the scripts is finished. I want to execute the same script exactly each second., is that possible?
za flag
I don't know Python, so I don't know the request module and don't know how to test it. IMHO, if the python is started as a thread, it shouldn't be able to block something, no matter what and the loop should continue. Can you provide a minimalistic python script to call in the loop, to prove your point?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.