python


Python script runs in bash, but not in cron?


I have written a python script that goes to reddit (using PRAW), goes to a specific subreddit, and downloads episodes of a podcast from Youtube using youtube-dl. I placed the script in my /bin folder, so I can run it from anywhere is bash. The problem is that it runs fine in bash, but not in cron at all. I've set it up in cron as like this:
0 */1 * * * PKA.py
Here is a copy of the stacktrace that cron puts in /var/mail:
Traceback (most recent call last):
File "/bin/PKA.py", line 48, in <module>
call(myCall)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
The crash happens at the system call. It clearly gives the error 'No such file or directory', but all of the directories are correct. I'm only using absolute paths, so that shouldn't be a problem. I'm having a really hard time figuring out the problem, and I'd appreciate any help you guys could give. Here is the full script:
#!/usr/bin/env python
import praw
import os
from subprocess import call
#Directories
PKA_DIR = '/home/chris/Storage/750g/PKA/'
PKA_DOWNLOADED = '/home/chris/Storage/750g/PKA/pka-downloaded'
PKN_DIR = '/home/chris/Storage/750g/PKN/'
PKN_DOWNLOADED = '/home/chris/Storage/750g/PKN/pkn-downloaded'
#Create the reddit instance
reddit = praw.Reddit(Deleted for privacy)
subreddit = reddit.subreddit(Deleted Subreddit Name)
for submission in subreddit.new(limit=5):
isDownloaded = False
with open(PKN_DOWNLOADED, 'r') as downloaded:
for line in downloaded:
if submission.url in line:
isDownloaded = True
if line in submission.url:
isDownloaded = True
if isDownloaded == False:
title, episode = submission.title.split()
if title == 'PKN':
myTitle = 'PKN #%s'%(episode) + '.%(ext)s'
myCall = ['youtube-dl', '-f ' 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o' '%s%s'%(PKN_DIR,myTitle), submission.url]
call(myCall)
with open(PKN_DOWNLOADED, 'a') as writeOut:
writeOut.write(submission.url + '\n')
isDownloaded = False
with open(PKA_DOWNLOADED, 'r') as downloaded:
for line in downloaded:
if submission.url in line:
isDownloaded = True
if line in submission.url:
isDownloaded = True
if isDownloaded == False:
title, episode = submission.title.split()
if title == 'PKA':
myTitle = 'PKA #%s - '%(episode) + '%(title)s.%(ext)s'
myCall = ['youtube-dl', '-f ' 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o' '%s%s'%(PKA_DIR,myTitle), submission.url]
call(myCall)
with open(PKA_DOWNLOADED, 'a') as writeOut:
writeOut.write(submission.url + '\n')
The programs signs into the bots account (I know this isn't necessary, I just wanted to try out the full features of PRAW), and navigates to the subreddit. It considers the most recent 5 posts, and pulls the youtube link from it. It checks the link against a text file database of previously downloaded episodes. If the episode has not been downloaded, it downloads it and adds it to the list. It puts all the necessary arguments into a list, and does a system call with call. I suspected the issue was due to permissions, but my searching has told me that cron runs jobs as root. The folder it's downloading to (`/home/chris/Storage/750g') are all on seperate drives, so that's why I thought permissions might have been a problem.
Any suggestions?
The simplest solution is to use full absolute path for youtube-dl. In your case, use /usr/local/bin/youtube-dl instead of youtube-dl.
The reason:
Some Linux distributions, like Ubuntu, changes the original PATH variable to include commands in /usr/local/bin when user accessed the shell. However, cron passes a minimal set of environment variables to the job and keeps the original PATH[1].

Related Links

Having trouble getting my calculator app to print the result I want
Why am I getting this ValueError?
How to recognize single digit in string to insert leading zero?
How can I configure a test environment with Falcon
Computing the gradient for a custom TensorFlow op with py_func()
get all files in drive via REST API
Setting an image as a tkinter window background
Displaying A File Using StringVar() // Tkinter Python 3.5
How are features ranked in RFECV in scikit learn(sklearn)?
Solr requests hang when started via python subprocess
How to extract internet email headers from outlook emails?
Tensorboard get blank page
Python Matplotlib Streamplot providing start points
Reduce inner points in numpy coordinate dataset (speed up concave hull)
Blitting several layers pygame
floating and integer power difference in Python

Categories

HOME
performancecounter
firebase-app-indexing
jbehave
wxwidgets
reportportal
formal-verification
phpstorm
sbt-assembly
is-empty
websphere-liberty
desktop
aruco
nixos
dryioc
ejs
quartus
aptana
attask
android-fragmentactivity
plsqldeveloper
iup
binutils
source-maps
bcrypt
jflex
activepython
scalajs-react
visual-c++-2017
unpack
helper
winscp
jboss5.x
honeysql
jboss-esb
swagger-editor
agent
code-composer
control-flow-graph
filepath
amazon-clouddrive
rapidweaver
fqdn
taskmanager
walmart-electrode
jags
applepayjs
spell-checking
ticker
forerunnerdb
mercury
teiid
activeweb
ruby-2.0
freetype2
instruments
gherkin
asp.net-mvc-partialview
fortran90
bluesnap
except
affix
on-duplicate-key
manjaro
sparse-file
uos
web-component-tester
mdm-zinc
jai
ejabberd-saas
kallithea
cpu-speed
google-admin-audit-api
suffix-array
sid
ember-addon
yahoo-boss-api
planetary.js
android-looper
visual-studio-addins
nservicebus4
facebook-sdk-3.1
vertical-rhythm
shiva3d
mailcore
database-permissions
socketstream
tfs-power-tools
shim
rose-db-object
viewdidload
ubuntu-11.04
wchar
nemerle
gethashcode
out-of-browser
getresource
surefire
winverifytrust
openwysiwyg
configurable
data-execution-prevention
dotproject

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App