Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crontab -e (Ubuntu) not working with auto update library code #80

Closed
timmehtimtims opened this issue Jun 6, 2020 · 9 comments
Closed

Comments

@timmehtimtims
Copy link

I use the following line of code to run a library scan and update of the TV Trending library
cd /opt/appdata/pydpl/&& sudo python plexlibrary tv_trending - this works perfectly.

In sudo crontab -e, I have entered:
@daily cd /opt/appdata/pydpl/&& sudo python plexlibrary tv_trending - however crontab does not seem to be running this script and I am having to do this manually.

Please could someone advise what I'm doing wrong?

@doob187
Copy link

doob187 commented Jun 8, 2020

python3 not python

copy paste it and well done edit the name of the folder

python-plexlibrary in the script and it will works withoutany problems

#!/bin/bash
##USERSIDE EDITS !!
FOLDER="/opt/appdata"
CRONTIME="daily"

###
###dont edit below !!!
CRONTABFILE="/tmp/mycron"
PATH='/usr/bin:/bin:/usr/local/bin'
export PYTHONIOENCODING=UTF-8

sudocheck() {
  if [[ $EUID -ne 0 ]]; then
    tee <<-EOF
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⛔️  You Must Execute as a SUDO USER (with sudo) or as ROOT!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EOF
    exit 0
  else
     owned
     plexpython
  fi  
}
owned() {
 if [[ ! -f "$CRONTABFILE" ]]; then
    sudo chmod +x ${FOLDER}/python-plexlibrary/scripts/plexlibrary.sh
    crontab -l > /tmp/mycron
    echo "@${CRONTIME} bash ${FOLDER}/python-plexlibrary/scripts/plexlibrary.sh" >> /tmp/mycron
    crontab /tmp/mycron
    echo "Python-Plexlibrary ${CRONTIME} Crontab is added" 
 else
    echo "Python-Plexlibrary  ${CRONTIME} Crontab is already added" 
 fi
}

plexpython() {
truncate -s 0 ${FOLDER}/python-plexlibrary/plexlibrary.log
echo $(date) | tee -a ${FOLDER}/python-plexlibrary/plexlibrary.log
echo "" | tee -a ${FOLDER}/python-plexlibrary/plexlibrary.log

for file in ${FOLDER}/python-plexlibrary/recipes/*
do
    if [ ! -d "${file}" ]; then
        /usr/bin/python3 ${FOLDER}/python-plexlibrary/plexlibrary/plexlibrary.py $(basename "$file" .yml) | tee -a ${FOLDER}/python-plexlibrary/plexlibrary.log
        echo "" | tee -a ${FOLDER}/python-plexlibrary/plexlibrary.log
    fi
done
}
####### FUNCTIONS LINE
sudocheck
####### FUNCTIONS LINE

@doob187
Copy link

doob187 commented Jun 8, 2020

in your case here

#!/bin/bash
#########################################################################
# Title:         Cloudbox: Python-PlexLibrary Helper Script             #
# Author(s):     desimaniac                                             #
# URL:           https://github.com/cloudbox/cloudbox                   #
# --                                                                    #
#         Part of the Cloudbox project: https://cloudbox.works          #
#########################################################################
#                   GNU General Public License v3.0                     #
#########################################################################
#                 CUSTOM SCRIPT WITH CRONATB AUTO ADD                   #
#########################################################################
##USERSIDE EDITS !!
FOLDER="/opt/appdata"
CRONTIME="daily"

###
###dont edit below !!!
CRONTABFILE="/tmp/mycron"
PATH='/usr/bin:/bin:/usr/local/bin'
export PYTHONIOENCODING=UTF-8

sudocheck() {
  if [[ $EUID -ne 0 ]]; then
    tee <<-EOF
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⛔️  You Must Execute as a SUDO USER (with sudo) or as ROOT!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EOF
    exit 0
  else
     owned
     plexpython
  fi  
}
owned() {
 if [[ ! -f "$CRONTABFILE" ]]; then
    sudo chmod +x ${FOLDER}/pydpl/scripts/plexlibrary.sh
    crontab -l > /tmp/mycron
    echo "@${CRONTIME} bash ${FOLDER}/pydpl/scripts/plexlibrary.sh" >> /tmp/mycron
    crontab /tmp/mycron
    echo "Python-Plexlibrary ${CRONTIME} Crontab is added" 
 else
    echo "Python-Plexlibrary  ${CRONTIME} Crontab is already added" 
 fi
}

plexpython() {
truncate -s 0 ${FOLDER}/pydpl/plexlibrary.log
echo $(date) | tee -a ${FOLDER}/pydpl/plexlibrary.log
echo "" | tee -a ${FOLDER}/pydpl/plexlibrary.log

for file in ${FOLDER}/pydpl/recipes/*
do
    if [ ! -d "${file}" ]; then
        /usr/bin/python3 ${FOLDER}/pydpl/plexlibrary/plexlibrary.py $(basename "$file" .yml) | tee -a ${FOLDER}/pydpl/plexlibrary.log
        echo "" | tee -a ${FOLDER}/pydpl/plexlibrary.log
    fi
done
}
####### FUNCTIONS LINE
sudocheck
####### FUNCTIONS LINE

with fixed apth from python-plexlibrary to pydpl

beware all recipes under python-plexlibrary/recipes
will run now on automode each day 00:00

@timmehtimtims
Copy link
Author

timmehtimtims commented Jun 9, 2020

Thanks so much, I’ll have a play this weekend.
Is it straight forward to only refresh individual recipes or are we straying into more complex waters?
Or could I just add another folder with those recipes in:
for file in ${FOLDER}/pydpl/recipes/NEW FOLDER/*

@doob187
Copy link

doob187 commented Jun 10, 2020

yes its works ;-)
image

@timmehtimtims
Copy link
Author

Great, I’ll have a stab at that 👍

@timmehtimtims
Copy link
Author

Hey man, I've had a stab and I think I've failed. I've googled around over the weekend but couldn't get it to run.
In sudo crontab -e I added the following line on Friday evening: @daily cd /opt/appdata/pydpl && sudo /opt/appdata/pydpl/pydplauto.sh. That didn't work so on Saturday I then tried @daily bash /opt/appdata/pydpl/pydplauto.sh but that didn't work either. Is there another way to point to the bash file?

The bash file is located /opt/appdata/pydpl/pydplauto.sh and the recipes I want it to run are located in /opt/appdata/pydpl/recipes/auto/ and the final bash file looks like this:

`#!/bin/bash
#########################################################################

Title: Cloudbox: Python-PlexLibrary Helper Script

Author(s): desimaniac

URL: https://github.com/cloudbox/cloudbox

--

Part of the Cloudbox project: https://cloudbox.works

#########################################################################

GNU General Public License v3.0

#########################################################################

CUSTOM SCRIPT WITH CRONATB AUTO ADD

#########################################################################
##USERSIDE EDITS !!
FOLDER="/opt/appdata"
CRONTIME="daily"

###dont edit below !!!
CRONTABFILE="/tmp/mycron"
PATH='/usr/bin:/bin:/usr/local/bin'
export PYTHONIOENCODING=UTF-8

sudocheck() {
if [[ $EUID -ne 0 ]]; then
tee <<-EOF
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⛔️ You Must Execute as a SUDO USER (with sudo) or as ROOT!
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EOF
exit 0
else
owned
plexpython
fi
}
owned() {
if [[ ! -f "$CRONTABFILE" ]]; then
sudo chmod +x ${FOLDER}/pydpl/scripts/plexlibrary.sh
crontab -l > /tmp/mycron
echo "@${CRONTIME} bash ${FOLDER}/pydpl/scripts/plexlibrary.sh" >> /tmp/mycron
crontab /tmp/mycron
echo "Python-Plexlibrary ${CRONTIME} Crontab is added"
else
echo "Python-Plexlibrary ${CRONTIME} Crontab is already added"
fi
}

plexpython() {
truncate -s 0 ${FOLDER}/pydpl/plexlibrary.log
echo $(date) | tee -a ${FOLDER}/pydpl/plexlibrary.log
echo "" | tee -a ${FOLDER}/pydpl/plexlibrary.log

for file in ${FOLDER}/pydpl/recipes/auto/*
do
if [ ! -d "${file}" ]; then
/usr/bin/python3 ${FOLDER}/pydpl/plexlibrary/plexlibrary.py $(basename "$file" .yml) | tee -a ${FOLDER}/pydpl/plexlibrary.log
echo "" | tee -a ${FOLDER}/pydpl/plexlibrary.log
fi
done
}
####### FUNCTIONS LINE
sudocheck
####### FUNCTIONS LINE`

I feel like I'm missing something obvious, hence I'm hoping that you'll spot it. Sorry for the hassle!

@timmehtimtims
Copy link
Author

The error message I'm getting is:

`Traceback (most recent call last):
Running the recipe 'movies_trending'
Retrieving the trakt list: https://api.trakt.tv/movies/watched/weekly?limit=100
Getting data from TMDb to add weighted sorting...
File "/opt/appdata/pydpl/plexlibrary/plexlibrary.py", line 67, in
main()
File "/opt/appdata/pydpl/plexlibrary/plexlibrary.py", line 61, in main
r.run(args.sort_only)
File "/opt/appdata/pydpl/plexlibrary/recipe.py", line 696, in run
missing_items, list_count = self._run()
File "/opt/appdata/pydpl/plexlibrary/recipe.py", line 94, in _run
item_list = self.weighted_sorting(item_list)
File "/opt/appdata/pydpl/plexlibrary/recipe.py", line 758, in weighted_sorting
details = self.tmdb.get_details(m['tmdb_id'], self.library_type)
File "/opt/appdata/pydpl/plexlibrary/tmdb.py", line 77, in get_details
cache = shelve.open(self.cache_file)
File "/usr/lib/python3.6/shelve.py", line 243, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "/usr/lib/python3.6/shelve.py", line 227, in init
Shelf.init(self, dbm.open(filename, flag), protocol, writeback)
File "/usr/lib/python3.6/dbm/init.py", line 88, in open
raise error[0]("db type could not be determined")
dbm.error: db type could not be determined

Running the recipe 'tv_trending'
Retrieving the trakt list: https://api.trakt.tv/shows/trending?limit=5
Retrieving the trakt list: https://api.trakt.tv/shows/watched/weekly?limit=20
Retrieving the trakt list: https://api.trakt.tv/shows/watched/monthly?limit=50
Retrieving the trakt list: https://api.trakt.tv/shows/watched/yearly?limit=100
Getting data from TMDb to add weighted sorting...
Traceback (most recent call last):
File "/opt/appdata/pydpl/plexlibrary/plexlibrary.py", line 67, in
main()
File "/opt/appdata/pydpl/plexlibrary/plexlibrary.py", line 61, in main
r.run(args.sort_only)
File "/opt/appdata/pydpl/plexlibrary/recipe.py", line 696, in run
missing_items, list_count = self._run()
File "/opt/appdata/pydpl/plexlibrary/recipe.py", line 94, in _run
item_list = self.weighted_sorting(item_list)
File "/opt/appdata/pydpl/plexlibrary/recipe.py", line 758, in weighted_sorting
details = self.tmdb.get_details(m['tmdb_id'], self.library_type)
File "/opt/appdata/pydpl/plexlibrary/tmdb.py", line 77, in get_details
cache = shelve.open(self.cache_file)
File "/usr/lib/python3.6/shelve.py", line 243, in open
return DbfilenameShelf(filename, flag, protocol, writeback)
File "/usr/lib/python3.6/shelve.py", line 227, in init
Shelf.init(self, dbm.open(filename, flag), protocol, writeback)
File "/usr/lib/python3.6/dbm/init.py", line 88, in open
raise error[0]("db type could not be determined")
dbm.error: db type could not be determined`

It would seem like the database isn't being identified correctly - yet if I run these recipes individually then they work fine...

@timmehtimtims
Copy link
Author

Solved.

For anyone else having this issue I downgraded the python version to 2.7.
Edit this line:
/usr/bin/python3 ${FOLDER}/pydpl/plexlibrary/plexlibrary.py $(basename "$file" .yml) | tee -a ${FOLDER}/pydpl/plexlibrary.log
to become:
/usr/bin/python2.7 ${FOLDER}/pydpl/plexlibrary/plexlibrary.py $(basename "$file" .yml) | tee -a ${FOLDER}/pydpl/plexlibrary.log

@adamgot
Copy link
Owner

adamgot commented Aug 25, 2020

When moving between python2 and python3 (or rather, different versions of shelve), you have to first delete the shelve file. python2 is no longer supported, you may run into other issues in the future.

@adamgot adamgot closed this as completed Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants