First thing, i'm surely know my python script is not run at all. Because i have 2 python script called checkDB.py and logDHT.py
this is my cron configuration :
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
1 * * * * pi /usr/bin/python /home/pi/Sensors_Database/logDHT.py
15 * * * * pi /usr/bin/python /home/pi/Sensors_Database/checkDB.py
and sure i had to give each script chmod+x to make sure its executable, and double check if my script realy have an error but sure my python script is running when i execute it manualy.
also i made setting up the cron on superuser. so can you guys tell me whats happen ? or may be what i miss ?
note: i still have not figure it out why is different running cron under root (super user) and without superuser.
i check with this command :
grep cron /var/log/syslog
no cron job is run, but with this command :
/etc/init.d/cron status
my cron status is running
Related
I am trying to execute a command through Django Crontab everyday. Here is what I am doing:
First, I added django_crontab in INSTALLED_APPS
FYI, I have written a Django command sendalerts which is working perfectly fine
Now I am trying to run that command through crontab on regular intervals
This is what I added in my settings.py
CRONJOBS = [
('* * * * *', 'django.core.management.call_command', ['sendalerts']),
]
When I run this command through python manage.py crontab add it doesn't give any error. It also list down cronJob when I check with this command python manage.py crontab show
But problem is it doesn't execute the code which is written in my sendalerts command.
What can I do to check what is that I am doing wrong or what can be the error which I can fix to make it work?
Edit:
Output of crontab -e is
* * * * * /usr/local/bin/python /home/wukla/app/app/manage.py crontab run 455e70156896954803547b6f6d845f9b # django-cronjobs for app
There can be multiple issue with this. The one's I have encountered are:
1) If your script is using any PATH variable from system then that has to be added to crontab manually.
2) You should add a relative path to your script to run.
3) Crontab entry should always have a newline at the end of the file.
These are all I thing I have come across as errors while using crontab.
Hope this might help you.
I've been trying to debug this for a while now and I feel like I've tried everything.
Code is slightly modified with *** for company reasons.
The following executes as expected when run from a session as my local user.
/var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log
Where run.sh is just a wrapper for running Python in a virtualenv:
#!/usr/bin/env bash
wd=$(dirname $0)
source ${wd}/virtualenv/bin/activate
python ${wd}/$1
I have placed a print statement inside of the Python main to show that it's being executed.
if __name__ == "__main__":
print("I got in here...")
When running the command as my local user, the log will contain this printed statement. However, when run in cron as:
*/30 * * * * /var/www/****/***/run.sh path_to_my/script.py 2>&1 >> /var/www/****/***/test.log
I do not get any printed statement, nor do I receive any error output from the 2>&1.
My permissions are 755 on both the .sh and .py scripts.
Everything works as expected except when run via cron.
Am I missing something? Does cron not use .bashrc for the crontab user?
First make sure your local cronjob is running by putting the following in the crontab file and check to see if it get written out at /tmp/env.output after a minute or two
* * * * * env > /tmp/env.output
Second make sure the user running the crontab has permission to write to the /var/www/****/***/test.log file
Third try changing your script to
wd=$(dirname $0)
cd $wd
source activate
python ${wd}/$1
Edited: Anders was able to figure out the answer by himself by adding PYTHONPATH to the cron environment: export PYTHONPATH="${PYTHONPATH}:${wd}"
I checked a lot posts with the same title, but I can't get my python running via cron.
I have several cron scripts already, which execute well, but not python.
Crontab runs as root.
I added following lines to crontab:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
I have this line for the execution in crontab:
* * * * * cd /var/www/usi/; /usr/local/bin/python3.6 /var/www/usi/usi.py
I tried a lot of variations:
added sudo in front of it to run as a different user
added bash to the line
executed the user profile before etc etc.
No results.
No errors in system log.
Any ideas?
Using Debian8
I found the error while putting the cron task into a separate shell script. I executed the script via cron and got an error. Simple typo. Did now saw any error before in syslog. :-(
It executes well with this in crontab now:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
* * * * * cd /var/www/usi/ && /usr/local/bin/python3.6 /var/www/usi/usi.py
I am trying to setup a crontab to run 6 python data scrapers. I am tired of having to restart them manually when one of them fails. When running the following:
> ps -ef | grep python
ubuntu 31537 1 0 13:09 ? 00:00:03 python /home/ubuntu/scrapers/datascraper1.py
etc... I get a list of the datascrapers 1-6 all in the same folder.
I edited my crontab like this:
sudo crontab -e
# m h dom mon dow command
* * * * * pgrep -f /home/ubuntu/scrapers/datascraper1.py || python /home/ubuntu/scrapers/datascraper1.py > test.out
Then I hit control+X to exit and hit yes to save as /tmp/crontab.M6sSxL/crontab .
However it does not work in restarting or even starting datascraper1.py whether I kill the process manually or if the process fails on its own. Next, I tried reloading cron but it still didn't work:
sudo cron reload
Finally I tried removing nohup from the cron statement and that also did not work.
How can I check if a cron.allow or cron.deny file exists?
Also, do I need to add a username before pgrep? I am also not sure what the "> test.out" is doing at the end of the cron statement.
After running
grep CRON /var/log/syslog
to check to see if cron ran at all, I get this output:
ubuntu#ip-172-31-29-12:~$ grep CRON /var/log/syslog
Jan 5 07:01:01 ip-172-31-29-12 CRON[31101]: (root) CMD (pgrep -f datascraper1.py ||
python /home/ubuntu/scrapers/datascraper1.py > test.out)
Jan 5 07:01:01 ip-172-31-29-12 CRON[31100]: (CRON) info (No MTA installed, discarding output)
Jan 5 07:17:01 ip-172-31-29-12 CRON[31115]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Jan 5 08:01:01 ip-172-31-29-12 CRON[31140]: (root) CMD (pgrep -f datascraper1.py || python /home/ubuntu/scrapers/datascraper1.py > test.out)
Since there is evidence of Cron executing the command, there must be something wrong with this command, (note: I added the path to python):
pgrep -f datascraper1.py || /usr/bin/python /home/ubuntu/scrapers/datascraper1.py > test.out
Which is supposed to check to see if datascaper1.py is running, if not then restart it.
Since Cron is literally executing this statement:
(root) CMD (pgrep -f datascraper1.py || python /home/ubuntu/scrapers/datascraper1.py > test.out)
aka
root pgrep -f datascraper1.py
Running the above root command gives me:
The program 'root' is currently not installed. You can install it by typing:
sudo apt-get install root-system-bin
Is there a problem with Cron running commands from root?
Thanks for your help.
First of all, you need to see if cron is working at all.
Add this to your cron file (and ideally delete the python statement for now, to have a clear state)
* * * * * echo `date` >>/home/your_username/hello_cron
This will output the date in the file "hello_cron" every minute. Try this, and if this works, ie you see output every minute, write here and we can troubleshoot further.
You can also look in your system logs to see if cron has ran your command, like so:
grep CRON /var/log/syslog
Btw the >test.out part would redirect the output of the python program to the file test.out. I am not sure why you need the nohup part - this would let the python programs run even if you are logged out - is this what you want?
EDIT: After troubleshooting cron:
The message about no MTA installed means that cron is trying to send you an e-mail with the output of the job but cannot because you dont have an email programm installed:
Maybe this will fix it:
sudo apt-get install postfix
The line invoking the python program in cron is producing some output (an error) so it's in your best interests to see what happens. Look at this tutorial to see how to set your email address: http://www.cyberciti.biz/faq/linux-unix-crontab-change-mailto-settings/
Just in case the tutorial becomes unavailable:
MAILTO:youremail#example.com
You need to add python home to your path at the start of the job, however you have python set up. When you're running it yourself and you type python, it checks where you are, then one level down, then your $PATH. So, python home (where the python binary is) needs to be globally exported for the user that owns the cron (so, put it in a rc script in /etc/rc.d/) or, you need to append the python home to path at the start of the cron job. So,
export PATH=$PATH:<path to python>
Or, write the cron entry as
/usr/bin/python /home/ubuntu/etc/etc
to call it directly. It might not be /usr/bin, run the command
'which python'
to find out.
The 'No MTA' message means you are getting STDERR, which would normally get mailed to the user, but can't because you have no Mail Transfer Agent set up, like mailx, or mutt, so no mail for the user can get delivered from cron, so it is discarded. If you'd like STDERR to go into the log also, at the end, instead of
"command" > test.out
write
"command" 2>&1 > test.out
to redirect STDERR into STDOUT, then redirect both to test.out.
So I'm trying to have my server execute a Python script every hour.
When I go into the directory and run it with python twitter.py, it works fine.
However, I have this entry in crontab and it doesn't work:
0 * * * * /run/twitterparse/twitter.py > /run/twitterparse
I am trying to have it execute every hour, on the hour.
Here's the output to the syslog:
Aug 5 13:00:01 localhost CRON[11474]: (root) CMD (/run/twitterparse/twitter.py >/run/twitterparse/)
Aug 5 13:00:01 localhost CRON[11473]: (CRON) info (No MTA installed, discarding output)
Now what it should be doing is accessing a database and saving information from the web to that database. The script does that fine when run manually, but not automatically.
I fixed my problem by doing this
0 * * * * python /run/twitterparse/twitter.py > /run/twitterparse
and the py file should be executable
chmod +x twitter.py
You probably need to add
#!/usr/bin/python
to the beginning of the main script, with the location of YOUR python interpreter.
and change the permissions of the script to executable
chmod +x <main script name .py>
Hope this helps
Any output of a cron script that goes to the terminal (i.e. stdio or stderr) is sent to the person who's account the script is registered under.
You can see from the error message that it is trying to send this mail but "No MTA is installed" so it is discarding all the diagnostic messages.
Your first step would be to either fix the problem with the MTA so you can receive the mail or to make sure that stdio and stderr are written to a log file.
The obvious problem that I can see is that the output is being directed to a non-file.
/run/twitterparse/twitter.py > /run/twitterparse
Isn't /run/twitterparse a folder? I guess:
/run/twitterparse/twitter.py > /run/twitterparse/ouput.txt
would solve half the issues..