I am trying to run a python script on Linux via WSL.
First of all I run service --status-all
and both atd
and cron
are marked as [+]
. Just to make sure that it works i typed in crontab -e
the following:
*/2 * * * * /home/myname/Task_scripts/example.sh
This is what the script file example.sh looks like:
#!/bin/sh
echo "Hello World - Generated every 2 mins" >> ~/hello1.txt
The file indeed runs as set every 2mins so everything fine so far.
Let's move to the Python part now. I follow the exact same process ie crontab with a script and cannot get it run no matter what. In specific these are the two alternatives (among the many i tried) that i expected to work but apparently do not. FYI I am using an anaconda environment.
Alternative 1:
What i have in crontab: */2 * * * * /home/myname/Task_scripts/email_example.sh
How the file looks like:
#!/bin/sh
/home/myname/anaconda3/envs/py36env/bin/python /home/myname/Production/example_email.py
Here are the rights in the .py file after running chmod +x email_example.sh
:
-rwxrwxrwx 1 myname myname 99 Nov 7 21:26 email_example.sh
Alternative 2:
Set the crontab as follows:
*/2 * * * * /home/myname/anaconda3/envs/py36env/bin/python /home/myname/Production/example_email.py
The rights on the .py are same as above.
Executing manually the:
/home/myname/anaconda3/envs/py36env/bin/python /home/myname/Production/example_email.py
runs as it is supposed to do.
你有没有将 example_email.py 作为可执行文件?
chmod +x /home/myname/Production/example_email.py
Well, after a few tries I added an export to a log file in crontab command and was able to backtrack the issue. It was a python issue and in specific PYTHONPATH which I had to insert in the first line of my crontab.
For the story, WSL works fine just like a real linux OS.
Friendly tip: always make sure to include a log file. For the beginners (including me) this can be done by the following example in crontab:
* * * * * /home/myname/Task_scripts/example.sh >> /home/myname/Logs/example.txt 2>&1
For organizational purposes created a Logs directory to collect such files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.