I want to run pyROOT with crontab. I import ROOT in .py file and when the crontab is running,
an error message shows up:
ModuleNotFoundError: No module named ‘ROOT’
This is my crontab’s content:
1 * 14 11 12 * /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 /Users/hiendoan/work/test_bacutga/testPython.py > /Users/hiendoan/work/test_bacutga/out.log
And this is my python code:
Could you please tell me how to solve this problem?
Thank you very much!
ROOT Version: 6.22/08
Compiler: Not Provided
Services are run with a minimal environment, i.e., they don’t use your user environment, so you need to specify all in your job. I don’t use macOS but it should work similar to Linux, and on Linux you can put all the environment variable definitions (if you know which ones you really need) in the crontab file, or just source your .bashrc when running the job.
Here is one way that worked for me on Ubuntu on a simple test (also just importing ROOT and printing a “hello” message):
1 - Copy your .bashrc (where you are defining your ROOT environment, i.e. sourcing the bin/thisroot file) to a .sh file (e.g. /home/user/myscript.sh) and give it executable permission (
chmod +x myscript.sh); if you manually run myscript.sh, the system may complain about some lines that don’t really work this way, so you may want to do some cleanup if you know what you’re doing, although the cron job was working for me, so I didn’t bother, but your mileage may vary.
2 - At the end of the same .sh file, add the command you want to run; sourcing myscript.sh did not work inside cron, saying it could not find “source”, and “which source” did not return any path, so to avoid this just run this .sh file in step 3, and run your code from this file too; in your case, add this line:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3 /Users/hiendoan/work/test_bacutga/testPython.py > /Users/hiendoan/work/test_bacutga/out.log
3 - Add as cron job (
crontab -e to edit) the sh script only:
1 * 14 11 12 * /home/user/myscript.sh
4 - Restart the cron(tab) service (on WSL the service to run is cron, not crontab, but in your case I suppose you already know what you need :))
Thank you very much for your solution.
I can run crontab now.