On my raspberry pi device with ubuntu 20.04 , when I manually run the start.py code after reboot the device within the folder /home/okay
python3 start.py
both scripts are running one after the other. But when I put this code in @reboot in crontab,
@reboot sleep 300 && python3 /home/okay/start.py
only the first script runs, the second one does not.
the py script is below
import os
os.system ("python3 /home/okay/avadot.py & ")
os.system ("python3 /home/okay/main.py &")
The reason I wrote the code with os.system is that both py files run in a while True: loop every 10 minutes. Therefore, it is not possible to start the other when one is finished, both must start working at the same time.
CodePudding user response:
It doesn't seem to make much sense to use Python for the cron
job at all.
os.system("cmd &")
will run cmd
as a background job of the shell it spawns, but the shell will hang around until the background job finishes. You can detach by various means, but the simplest solution is to run both jobs from the same shell, and not use Python at all.
#!/bin/sh
python3 /home/okay/avadot.py &
python3 /home/okay/main.py &
If you desperately want to spend one process more, you can run this from Python:
import subprocess
subprocess.run("""
python3 /home/okay/avadot.py &
python3 /home/okay/main.py &
""", shell=True, check=True)
or refactor your code so you can import
these two scripts and run them with multiprocessing
.
Returning to the simpler proposal, you can launch both processes from one cron
job
@reboot sleep 300 && python /home/okay/avadot.py & python3 /home/okay/main.py &
but cron
already runs in the background, so you could split it up into
@reboot sleep 300 && python /home/okay/avadot.py
@reboot sleep 300 && python /home/okay/main.py
There may be additional complications; one common beginner conundrum is attempting to run tools which require a graphical display or user interaction, which of course are not trivially available from cron
at all. Perhaps see CronJob not running for more information and troubleshooting tips.
CodePudding user response:
First things is cron needs full paths to the executables you want to use.
You can find them with command -v <cmd>
.
I guess you're looking for something like
@reboot /usr/bin/sleep 300 && /usr/bin/python3 /home/okay/start.py
Then, the script you wrote runs sub processes. If you look at the documentation of os.system()
, you can read:
The
subprocess
module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in thesubprocess
documentation for some helpful recipes.
So you should probably do that:
import subprocess
subprocess.Popen(["python3", "/home/okay/avadot.py"])
subprocess.Popen(["python3", "/home/okay/main.py"])
Which will start both processes, but note it's a bad idea starting processes and without plans for their termination (see Popen.wait()
or Popen.poll()
).
CodePudding user response:
Maybe this worked
import subprocess
subprocess.call('reboot sleep 300 && python3 /home/okay/start.py', shell=True)