Search code examples
pythonterminalscheduled-tasksnassynology

How to get python scripts requiring 3rd party libraries running in Task Scheduler of Synology NAS?


Now i need to run a py file that uses 3rd party libraries in Task Schedulers of Synology NAS. I got the privilege in the NAS terminal by sudo su , and installed pip by command curl -k https://bootstrap.pypa.io/get-pip.py | python3.

enter image description here

The script wheel is installed in '/var/packages/py3k/target/usr/local/bin'. I then try installing pandas with sudo python3 -m pip install pandas , the installation is successful. Then I go on installing requests, DateTime as well. They are all successful.

When i go to /volume1/@appstore/py3k/usr/local/lib/python3.5/site-packages , i can see them all there.

I have 2 py files scheduled actually, one is a simple py file, another is a py file that uses 3rd party libraries. The simple py file runs successfully in the NAS. It repeats itself every minute as set. But the one using 3rd party still can't run.

The below is the simple py file which runs perfectly,

def main():
    f = open("/volume1/homes/admin/python/result.txt", 'w+') 
    for i in range(10): 
        f.write("This is line {0:d}\n".format(i+1)) 
    f.close()
if __name__=="__main__":
    main()

The below is the less simple one which can't produces log.txt successfully,

from datetime import datetime
import os

def write_file(filename,data):
    if os.path.isfile(filename):
        with open(filename, 'a') as f:  # in append mode
            f.write('\n' + data)   
    else:
        with open(filename, 'w') as f: # in write mode
            f.write(data)
 
def print_time():   
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    data = "This is to log each minute Cron runs, the time is: " + current_time

    return data

if __name__=="__main__":
    write_file('log.txt' , print_time())

Does anyone know what is my problem? Why does the less simple one can't run? The OS library is default in Python 3.0 or above, so I think I have all the required installed already.


Solution

  • Try specify the file name and path at the same time. That is write_file('/volume1/homes/admin/python/log.txt' , print_time()) in your case. I assume you are putting the log.txt file in the same location as your result.txt file.

    I remember synology nas likes more evident paths to look for files.