0

I had problem with cron and Python.

I have a code that recognize face live and I need this program to close, so it could write new file with the names that it found, every day.

I'm writing 'crontab -e' in the console and inside contrab I wrote this one:

52 17 * * * /usr/bin/python /home/{usr}/{Folder name}/main.py

I expected, that my program is going to be closed and re-opened, but nothing happened. What's wrong?

jsbillings
  • 24,406
  • Will the Python code try to open or close windows on your screen? If so, then see e.g. https://unix.stackexchange.com/questions/10121 Note that Unix is a multi-user operating system and that any number of users theoretically could be using a machine to run graphical sessions (or a single user could theoretically have multiple GUI sessions running). A cron job would need to know which GUI session to attach to. – Kusalananda Dec 21 '21 at 09:41

2 Answers2

1

The environment of a program running from cron is not the same as the environment in your interactive shell or your desktop login session.

The "standard" solution to this is to write a wrapper script which sets the environment to be what your program needs - e.g. with virtualenv, or just by setting env vars - and run the wrapper script from cron.

That will do for executing the script, anyway.

As for killing it or causing it to take some specific action on certain criteria (e.g. an empty queue or otherwise running out of input data to process; a certain time of day; receiving a specific signal like SIGHUP, SIGSTOP, SIGUSR1, etc; the existence or absence of a semaphore file, etc) that's up to your python program to detect and act upon.

Or you could just have another cron job kill it, or have the cron job that starts it kill any existing instances (or just some of them) before starting a new one - use a PID file or run pkill or something like that.

cas
  • 78,579
0

Looks like there is issue with environment and we can set cron environment from the shell env variables

# Make sure to have python file first line is #!/usr/bin/env python
# Setting cron env like below and setted here the SHELL
SHELL=/bin/bash
52 17 * * * /home/{usr}/{Folder name}/main.py