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}"
Related
This is how I configure crontab (by using crontab -e)
* * * * * /home/jeff/Desktop/scripts/job_pull_queue.sh >> /home/jeff/Desktop/scripts/log.txt
This is the content of /home/jeff/Desktop/scripts/job_pull_queue.sh
#!/bin/bash
echo "Running job_pull_queue.sh # $(date)"
cd /home/jeff/Documents/code/some_project
echo $(printenv)
/home/jeff/miniconda3/bin/python -m util.main
Now the problem is, when running ./job_pull_queue.sh in the terminal, it works, but I can tell from the log file that crontab never executes that last line /home/jeff/miniconda3/bin/python -m util.main (I can see the result from the previous echo in the log file, but not the python script itself), what happened? How do I fix it?
Update: here's the result from printenv when ran by crontab
SHELL=/bin/sh PWD=/home/jeff/Documents/code/some_project LOGNAME=jeff HOME=/home/jeff LANG=en_US.UTF-8 SHLVL=0 PATH=/usr/bin:/bin OLDPWD=/home/jeff _=/usr/bin/printenv
Ok...
My Python script reads several env variables from my user profile, and of course, these variables don't exist when crontab is running the script...
And I don't have detection/logging in place so I didn't know env variables are missing.
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 have a bash script that I'm using to execute a python file with a specific version of Python (3.6). The Bash script is currently located on my desktop (/home/pi/Desktop/go.sh)
#!/bin/bash
python3.6 /home/pi/scriptDir/myScript.py
Here is my crontab entry, when I do crontab -l (note, I've deleted my other jobs)
* * * * * bash /home/pi/Desktop/go.sh # JOB_ID_3
When I run this file using the command line or from the GUI it executes properly.
When I have crontab do it, nothing happens.
Both my python file and the bash script are executable. chmod +x
Is there something obvious I'm missing?
**my python script does depend on other files in the same script directory, could that be the issue?
Here's what got it working for me. I was not using the full path to my python install. Unless you log the bash file there's no indication that you have an issue.
This is my bash file now. echo's were just to determine that I was indeed running the bash file.
#!/bin/bash
echo started
/home/pi/Python-3.6.0/python home/pi/myScriptFolder/myScript.py
echo finished
To break down the line executing the script:
/home/pi/Python-3.6.0/python - is where python 3.6.0 is installed on my Pi, it could be different for you. home/pi/myScriptFolder/myScript.py is the script I want to run.
And here is my cron statement:
*/15 * * * * bash /home/pi/Desktop/go.sh > /home/pi/Desktop/clog.log 2>&1 -q -f
Breaking down this line:
*/15 * * * * is the cron time, in this case every 15 mins. bash /home/pi/Desktop/go.sh specifies to run a bash file and the directory of that file. > /home/pi/Desktop/clog.log 2>&1 -q -f this last section creates a log file named clog.log so you can see what's going on.
The key here was not just logging the go.sh bash file execution, but adding the 2>&1 -q -f to the end of the log request. Before I did that there was no indication of a problem, afterwards I was getting the python files error returned into the log file.
Cron jobs are surprisingly tricky. Aside from working directory ( which you'd have to set somewhere), you also need to handle environment setup ( $PATH, for example).
Start by redirecting your shell script's standard output and error to a log file so you can get feedback.
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
Trying to run my first crontab job (a python script) on Mac OS X El Capitan.
In the terminal I run: crontab -e
which opens a crontab.xyz123 file in Sublime Text (as I set it up). I put in the following cron syntax:
30 * * * * /Users/user.name/anaconda/bin/python /Users/user.name/Desktop/python_script.py
I just want it to run every half hour. The python script opens a youtube video and I've included it below:
#!/Users/user.name/anaconda/bin/python
print("this is a script")
When I run crontab -l I get the following error:
crontab: no crontab for user.name - using an empty one
crontab: no changes made to crontab
How can I get this to run properly? Is something wrong with the cron job? With the script?
Other note: I've changed the script permissions to be executable and I'm using only full paths in the crontab itself. What else could be wrong? I've run the script by itself in command line and it works fine.