Issue
I have a cronjob that runs a python script every 2 hours. The script itself can run longer than 2 hours. Thats why I try to detect if the script is already running. The system is FreeBSD machine. I try to do the detection with the following function:
def script_already_running(script_name,console_logger,parameter=None):
"""
checks if the script is already running, this is done by syscall and parsing the output. Parameters of the script can be distinguished as well
"""
ps = subprocess.Popen("ps aux", shell=True, stdout=subprocess.PIPE)
process_strings = ps.stdout.read().decode(encoding="utf-8",errors="strict")
ps.stdout.close()
ps.wait()
console_logger.info(process_strings)
# print(process_strings)
# print(script_name)
if script_name in process_strings and parameter is None:
script_running = True
elif script_name in process_strings and parameter is not None:
if parameter in process_strings:
script_running = True
else:
script_running = False
else:
script_running = False
return script_running
this function is called from another script. script_name
is the name (with path) of the calling script and console_logger
is a python logging
object. When I execute everything directly from the shell everything works just fine. The function prints the output of ps aux
which looks like that (I removed the unnecessary rest)
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
mysql 7225 95.8 1.1 530948 190652 - IJ Sun12 3750:03.47 /usr/local/libexec/mysqld --defaults-extra-file=/var/db/mysql/my.cnf --basedir=/usr/local --datadir=/var/db/mysql --plugin-dir=/usr/local/lib/mysql/plugin --log-error=/var/db/mysql/test_jail.err --pid-
user 56261 22.8 0.4 195936 60040 0 S+J 22:14 0:02.74 python3 testscript.py (python3.6)
user 50756 0.6 0.5 220976 78068 - SsJ 21:20 3:25.17 python3 /home/user/immoquery/src/testscript.py (python3.6)
So you can see it would never really run because when it starts it would always detect itself running. This is another problem I need to fix in the future. But when I look into the logs that the cronjob creates the output of ps aux
looks like this:
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
mysql 7225 92,4 1,3 530948 224528 - IJ So.12 3179:35,23 /usr/local/li
user 80177 24,9 0,4 196016 60184 - SsJ 09:20 0:02,83 python3 /home
user 68285 0,5 7,2 1343792 1198784 - SsJ 07:20 6:46,38 python3 /home
An the check delivers false
because it cannot find the script name in the string return of ps aux
. The string is somehow cut off.
Here is the content of the cron file:
SHELL=/usr/local/bin/zsh
LANG=de_DE.UTF-8
LC_CTYPE="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
# used to execute immobilienscout24 kaufangebote
#minute hour mday month wday command
20 7,9,11,13,15,17,19,21,23 * * * python3 /home/user/immoquery/src/testscript.py >> /home/user/testscript.log 2>&1
Has anyone an idea how to fix this? Why is the process output cut off? Is there a virtual screen size or something involved?
Solution
You could keep it simple by doing something like:
0 */2 * * * if ! pgrep -f your-script 2>&1 >/dev/null; then your-script; fi
The way it works is by checking if the process still running by using pgrep
and if not it will run your script.
Answered By - nbari