working on windows 10 using putty. I want to schedule a task using cron but its not working for the python code(sowfinal.py) which I want to run but I am to execute a simple python code(sowtest.py)... is it because of the libraries I am using ?
** * * * cd home/db2inst1/TicketAnalytics /usr/local/bin/python sowtest.py /tmp/listener.log 2>&1
** * * * cd home/db2inst1/TicketAnalytics /usr/local/bin/python sowfinal.py /tmp/listener.log 2>&1
Try without cd
** * * * /usr/local/bin/python /home/db2inst1/TicketAnalytics/sowtest.py /tmp/listener.log 2>&1
** * * * /usr/local/bin/python /home/db2inst1/TicketAnalytics/sowfinal.py /tmp/listener.log 2>&1
Related
I'm trying to execute a python script using crontab. I want to run this script every 2 hours. I use:
export EDITOR=nano
crontab -e
then:
0 */2 * * * cd /Users/myname/Desktop/CoronaFR/ && /opt/anaconda3/bin/python3 CoronaFR.py
But it doesn’t work. Any idea? Maybe a path problem?
Thanks in advance!
You can remove cd command and directly call the script by this command:
0 */2 * * * /opt/anaconda3/bin/python3 /Users/myname/Desktop/CoronaFR/CoronaFR.py
Try
0 */2 * * * /opt/anaconda3/bin/python3 /full/path/to_the_script/CoronaFR.py
I am trying to run python script via a cron job, but have had no luck the last two days, and I am running out of hairs to pull out.
Some info:
-->I have approximately 20 hours experience using Linux, so I might have missed something very basic.
-->Linux server (Ubuntu) on Linode.com
-->Script runs in terminal
-->Script has the permissions "0644"
-->#!/usr/bin/env python3.7
is added to the beginning of the script
-->The script belongs to the user "adamsavage" and I have tried to add it to both this users cron file and the cron file belonging to root using crontab -e and sudo crontab -e respectively
-->The cron files looks like this, and has a newline at the end:
* * * * * /usr/bin/python3 /home/adamsavage/python-scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1
-->sudo grep CRON /var/log/syslog returns this:
Apr 2 15:25:01 noeluddig CRON[7728]: (adamsavage) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7729]: (root) CMD (/home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7730]: (adamsavage) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7731]: (root) CMD (echo 'Yo' >> /home/adamsavage/log.txt)
Apr 2 15:25:01 noeluddig CRON[7732]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
-->I have also tried the following:
* * * * * cd /home/adamsavage/python_scripts/ && /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
and
* * * * * cd /home/adamsavage/python_scripts/ && /usr/bin/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
-The script is supposed to send me an sms with some info, which again works when it is ran from terminal.
-I should also mention that just having echo "message" >> /home/adamsavage/ouput.txt does actually run and prints "message" to that file.
What am I missing? Suffice to say, help will be greatly appreciated!:)
You should run your python script with python command:
* * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
For example:
* * * * * /usr/bin/python3 /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt
Try to add 2>&1 at the end of your cron line:
* * * * * path/to/python /home/adamsavage/python_scripts/send_new_sessions.py >> /home/adamasavage/log.txt 2>&1
I have no idea why this is, but what made it work was to remove the absolute path to the log file. If anyone can explain this, that would be great!
I'm trying to run a python script through crontab, but it can't import any of the libraries that it needs when it is run this way. When I run the scripts outside of crontab, there is no issue, and I know that I have these libraries installed.
Do I need to specify a path to them or something?
Many thanks
crontab file:
SHELL=/bin/bash
MAILTO=jess.chambers#gmail.com
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local
*/1 * * * * cd ~/Downloads/guichets && python newRdvChecker.py -G1 -S0 >>/tmp/stdout.log 2>&1
*/1 * * * * cd ~/Downloads/guichets && python newRdvChecker.py -G2 -S20 >>/tmp/stdout.log 2>&1
*/1 * * * * cd ~/Downloads/guichets && python newRdvChecker.py -G3 -S40 >>/tmp/stdout.log 2>&1
Error log:
Traceback (most recent call last):
File "newRdvChecker.py", line 2, in <module>
import requests
I'm running this setup on my Linux Mint computer, if that makes a difference
Determine what exact python executable you use when running the script from CLI (with which python) and specify the full path to python in your crontab.
I am looking to use crontab in Bash to run a python script. What I have below does not work.
SHELL=/bin/bash
11 22 * * * username /usr/lib/python2.7 /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
Nor did this:
SHELL=/bin/bash
PATH=/usr/lib/python2.7
5 22 * * * username python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
You can try logging the output and error of the execution of the command in crontab using :
11 22 * * * username /usr/lib/python2.7 /mnt/c/Users/Eric/Documents/Feedparser/crontab.py > /tmp/crontab.log 2>&1
This may give you the idea about what the problem is.
you need to remove the username in your cron command. Otherwise, it will try to run it as an executable with arguments python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
just put :
5 22 * * * python /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
You may wish to consider making your python file executable and calling it directly. Your crontab would then say:
5 22 * * * ./mnt/c/Users/Eric/Documents/Feedparser/crontab.py
or
5 22 * * * cd /mnt/c/Users/Eric/Documents/Feedparser && ./crontab.py
To do this, make your file executable:
chmod +x /mnt/c/Users/Eric/Documents/Feedparser/crontab.py
And add a shebang to the first line of your python file:
#!/usr/bin/env python
I have tried many many solutions to fixing this, including using django's custom management commands, python scripts with path configuration and setting the DJANGO_ENV_VAR, etc. Everything breaks when I try to import something from django specifically, like:
from django.db
or
import django.utils
Here is an example of one of my error tracebacks:
Traceback (most recent call last):
File "/home/username/webapps/folder/Project/scripts/standalone.py", line 29, in
<module> from Model.models import Model File "/home/username/webapps/folder/
Project/App/models.py", line 15, in <module> from django.db import models
ImportError: No module named django.db
In the crontab I followed all the general procedures. All of the time configs are correct, all the scripts are being called with their full paths, ie. /usr/bin/python2.7 or /usr/bin/sh.
When any of these scripts are run in the shell and not in crontab, it works just fine:
like:
python2.7 manage.py killModels
or
sh scrapper.sh >> log.log
Here is the crontab:
8,38,58 * * * * ~/webapps/Folder/apache2/bin/start
#*/1 * * * * /bin/sh ~/webapps/Folder/Project/scripts/unpack.sh >> ~/webapps/Folder/Project/logs/unpack.log 2>&1
*/25 * * * * ~/bin/indexer Model_Model --rotate --config
~/webapps/Folder/Project/misc/sphinx/sphinx.conf >> ~/webapps/Folder/Project/logs/searchd_log.txt
*/25 * * * * /bin/sh ~/webapps/Folder/Project/scripts/check_sphinx.sh >> ~/webapps/Folder/Project/logs/searchd_log.txt
0 2 * * * mysqldump --defaults-file=~/db_backups/Project_db.cnf -u Project_db Project_db > ~/db_backups/Project_db-`date +\%Y\%m\%d`.sql 2>> ~/db_backups/cron.log
*/1 * * * * /usr/local/bin/python2.7 ~/webapps/Folder/Project/manage.py UpdateModels >> ~/webapps/Folder/Project/logs/unpack.log 2>&1
Anyone have any ideas? Thanks.
Replace:
*/1 * * * * /usr/local/bin/python2.7 \
~/webapps/Folder/Project/manage.py UpdateModels \
>> ~/webapps/Folder/Project/logs/unpack.log 2>&1
by:
*/1 * * * * (cd ~/webapps/Folder/Project/; \
/usr/local/bin/python2.7 manage.py UpdateModels) \
>> ~/webapps/Folder/Project/logs/unpack.log 2>&1
With this change manage.py would be able to locate django applications.