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!
Related
I would like to add date with filename to my bash script which would be added to crontab file via script. The problem is entry in crontab file is already having the date appended. But my requirement is to have the date command in crontab.
crontab -l > "$FILENAME"
if grep -i cron "$FILENAME"; then
echo "Cron Job already present in User's crontab file"
else
echo "*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_`date +\%Y\%m\%dT\%H\%M\%S`.log 2>&1 " >> mycron
crontab mycron
echo "Crontab added to User's Crontab"
fi
Actual :
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_20211217053830.log 2>&1
Requirement :
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_`date +%Y%m%d%H%M%S`.log 2>&1
I have also tried to add escape characters but didn't seem to work
Use cat
cat << '==CRONTAB_LINE==' >> mycron
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_$(date +%Y%m%dT%H%M%S).log 2>&1
==CRONTAB_LINE==
Result
crontab -e
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_$(date +%Y%m%dT%H%M%S).log 2>&1
It looks like your crontab entry is close. Since %'s are special (newline) characters in a crontab file, escaping them should give the desired result:
*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_`date +\%Y\%m\%d\%H\%M\%S`.log 2>&1
The echo-line that accomplishes this is:
echo "*/5 * * * * bash -x /home/cronjob/cron.sh > /home/cronjob/myjob_\`date +\\%Y\\%m\\%dT\\%H\\%M\\%S\`.log 2>&1 " >> mycron
I verified that this works in Ubuntu.
Additionally, adding the line SHELL=/bin/bash to the crontab file (above the entries) ensures that bash interprets the crontab entries. While you likely will not need it it this case, it would enable more possibilities.
To insert the command output you have to replace
`date +\%Y\%m\%dT\%H\%M\%S`
with $(date +\%Y\%m\%dT\%H\%M\%S)
I have a sqlite query written in python:
dibbs5.py
cursor.executemany ( """
INSERT INTO dibbs_spider_dibbs_fields(hash,nsn,nomenclature,technical_documents,solicitation,
status,purchase_request,issued,return_by,file)
VALUES (?,?,?,?,?,?,?,?,?,?)
""" , records )
This works fine in local, in production when I execute this from terminal, it executes. But when I schedule this in cronjob, it shows the error:
cursor.executemany ( """
sqlite3.OperationalError: no such table: dibbs_spider_dibbs_fields
There is a table name dibbs_spider_dibbs_fields
Crontab:
31 09 * * * /usr/bin/python3 /root/spider/manage.py makemigrations > /root/spider/migration.log 2>&1
32 09 * * * /usr/bin/python3 /root/spider/manage.py migrate > /root/spider/migration2.log 2>&1
33 09 * * * DISPLAY=:0 /usr/bin/python3 /root/spider/dibbs5.py > /root/spider/dibbserorr3.log 2>&1
Did you run the dibbs5.py using root in the production terminal? Does it work? If Yes, please change the crontab using this command
sudo crontab -u root -e
31 09 * * * /usr/bin/python3 /root/spider/manage.py makemigrations > /root/spider/migration.log 2>&1
32 09 * * * /usr/bin/python3 /root/spider/manage.py migrate > /root/spider/migration2.log 2>&1
33 09 * * * DISPLAY=:0 /usr/bin/python3 /root/spider/dibbs5.py > /root/spider/dibbserorr3.log 2>&1
I have a feeling that you are not editing crontab for root. The program that you encountered is because the table is absent from the file as environmental variable(s) were not correct setup, resulting in accessing wrong file.
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
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
Can't get crontabs with variables working on Amazon Linux. It's weird cause those variables do work in the command line of the system, but won't work inside the cronjob. I know the variables are the problem, cause when I remove the part from --argstill >, the cronjob will be executed.
Worked fine on Ubuntu:
0 7 * * * /usr/bin/python /path/to/cronjob.py --method all --args $(date -d "1 day ago" +%Y%m%d),$(date +%Y%m%d) > /dev/null 2>&1
Also tried:
47 16 * * * python /path/to/cronjob.py --method all --args `date -d "1 day ago" +%Y%m%d`,`date +%Y%m%d` > /dev/null 2>&1
#tedder42 solution worked for me, I only needed to add the backslashes before the %:
47 16 * * * python /path/to/cronjob.py --method all --args `date -d "1 day ago" +\%Y\%m\%d`,`date +\%Y\%m\%d` > /dev/null 2>&1