Reputation: 1877
I have the following crontab:
22 * * * * cd /home/work/ui && /home/work/.jumbo/bin/python test.py >> result.log &
And the test.py
have the following codes:
#!/home/work/.jumbo/bin/python
#coding=utf-8
import datetime
import hashlib
import logging
import os
import Queue
import signal
import sys
import threading
import time
import traceback
#注释
if __name__ == "__main__":
print 'Begin'
print 'End'
OK, the codes can run right, but I add import requests
after, it will not run right, I think that it can not find lib path.
So, I use sys.path.append
, but it still can not run right.
#!/home/work/.jumbo/bin/python
#coding=utf-8
import datetime
import hashlib
import logging
import os
import Queue
import signal
import sys
import threading
import time
import traceback
sys.path.append('/home/work/.jumbo/lib/python2.7/site-packages/requests')
print sys.path
import requests
#注释
if __name__ == "__main__":
print 'Begin'
print 'End'
And then, how do I it?
BTW, I can run it right on OS command. So my code is OK.
Upvotes: 0
Views: 141
Reputation: 9065
first a tip: you can redirect stderr as well as stdout to a log file in crontab like this:
22 * * * * cd /home/work/ui && /home/work/.jumbo/bin/python test.py >> result.log 2>&1
I think the path to be appended should be
sys.path.append('/home/work/.jumbo/lib/python2.7/site-packages')
Upvotes: 0
Reputation: 2185
Cron jobs start with a very limited set of environment variables. There's likely one or more environment variables you get from your ~/.profile or ~/.login (or the like).
Try moving the whole job into a script and invoke it like this:
22 * * * * /home/work/ui/mycronjob
In mycronjob
you'd have something like this:
#!/bin/bash
source ~/.bash_profile
cd /home/work/ui && /home/work/.jumbo/bin/python test.py >> result.log
That assumes your $SHELL is bash and you have a ~/.bash_profile. If you have some other shell or other init file (~/.login, ~/.profile, etc) use that instead.
Upvotes: 3