Schedule python script with crontab, shutil.move doesn't work - python

Hi I schedule my script with cron.d, everything work except a function for move some files from a folder to another.
the function is:
def move_imported_file():
all_file=get_file()
for files in all_file:
#print (files)
shutil.move("/mnt/test-file/"+files, "/mnt/test-file/imported/"+files)
my cron.d file is this:
10 12 * * * root cd /usr/local/sbin/import-file/ && ./myscript.py
If i try to run manually the script, the function move all_file, but if I run the cron.d task, nothing happens
There is any possibility to have a log of what the function are doing?
Thanks
get_file:
def get_file():
my_file = []
os.chdir("/mnt/test-file")
files = glob.glob('*.ics')
for file in files:
my_file.append(file)
#print (my_file)
return my_file

Cron needs the correct PATHs:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/snap/bin
0 15 * * * root cd /usr/local/sbin/import-file/ && ./myscript.py

Related

Automatically execute a python script that sits on a Digital Ocean droplet using crontab?

I have a very simple test python script as seen below. This script sits on my Digital Ocean droplet.
#!/usr/bin/env python
import datetime
import time
def main():
f = open('output.txt','a')
f.write("Hello World! # :" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " --> _the_finish_\n")
print("Hello World! # :" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " --> _the_finish_\n")
f.close
if __name__ == '__main__':
for i in range(2):
main()
time.sleep(5)
I want this script to execute automatically using crontab. My crontab execution line is the following:
48 15 * * * /usr/bin/python3 /root/test_script/systemtime.py >> /tmp/systemtime.log 2>&1
The trouble I'm having is finding where or how to locate the python3 path in my Digital Ocean droplet, 1) where can I find or locate this python path I'm supposed to use? I don't have a tmp file in my Digital Ocean droplet, 2) so where will the .log file be stored? How can I configure the path for where the output will be stored?
Thanks all.
A way to find the Python path is running whereis python3. First make sure that it is installed.
It won't be stored. You should change /tmp/systemtime.log to your desired path. For instance, /root/test_script/test_script.log.
Btw, if you are using python3 you may want to change your shebang to #!/usr/bin/env python3.

Cron job with django application

I would like to use a cron task in order to delete media files if the condition is True.
Users generate export files stored in the Media folder. In order to clean export files in the background, I have a Cron task which loops over each file and looks if the expiry delay is passed or not.
I used django-cron library
Example:
File in Media Folder : Final_Products___2019-04-01_17:50:43.487845.xlsx
My Cron task looks like this :
class MyCronExportJob(CronJobBase):
""" Cron job which removes expired files at 18:30 """
RUN_AT_TIMES = ['18:30']
schedule = Schedule(run_at_times=RUN_AT_TIMES)
code = 'app.export_cron_job'
def do(self):
now = datetime.datetime.now()
media_folder = os.listdir(os.path.join(settings.MEDIA_ROOT, 'exports'))
for files in media_folder:
file = os.path.splitext(files.split(settings.EXPORT_TITLE_SEPARATOR, 1)[1])[0]
if datetime.datetime.strptime(file, '%Y-%m-%d_%H:%M:%S.%f') + timedelta(minutes=settings.EXPORT_TOKEN_DELAY) < now:
os.remove(os.path.join(os.path.join(settings.MEDIA_ROOT, 'exports'), files))
# settings.EXPORT_TOKEN_DELAY = 60 * 24
I edited my crontab -e :
30 18 * * * source /home/user/Bureau/Projets/app/venv/bin/activate.csh && python /home/user/Bureau/Projets/app/src/manage.py runcrons --force app.cron.MyCronExportJob
Then I launched service cron restart
But nothing as changed. My file is still there. However, it should be removed because his date is greater than now + settings.EXPORT_TOKEN_DELAY
I'm using Ubuntu to local dev and FreeBSD as a production server environment.
EDIT:
I tried some things but crontab doesn't work for the moment.
1) * * * * * /bin/date >> /home/user/Bureau/Projets/app/cron_output
==> It works, so crontab works
2) I ran : python manage.py runcrons in my console
==> It works
3) I ran this script (cron.sh):
source /home/user/.bashrc
cd /home/user/Bureau/Projets/app
pyenv activate app
python src/manage.py runcrons --force
deactivate
==> It works
4) I ran this crontab line :
35 10 * * * /home/user/Bureau/Projets/app/utility/cron.sh
==> Service restarted at 10h32, I waited until 10h38 : nothing !

Crontab executes but nothing happens (python file)

I set up a crontab file to execute a python file every minute however it states it executed but no file is generated.
crontest1.py file contains:
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
if __name__ == '__main__':
f = open('/APPS/CronRun/crontest/dummy1.txt','w')
f.write('hello world it is a file')
f.close()
Crontab file:
# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: release/10.0.0/etc/crontab 194170 2009-06-14 06:37:19Z brian $
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
#
#minute hour mday month wday who command
#
* * * * * root /APPS/CronRun/crontest/crontest1.py
11 11 * * * root /usr/bin/find /APPS/* | grep python | grep core | xargs rm
14 14 17 * * root /APPS/CronRun/Report/report.py
The crontab executes but no file appears.
I tried a simple date >> datelog.txt
which works via crontab but the python file does not seem to execute.
The python file works if executed manually from the shell but not via crontab
I have tried explicitly stating: python /APPS/CronRun/crontest/crontest1.py in the crontab file, but this does not work
I have previously added cron jobs that work fine and still execute daily without any issues
e.g
31,01 * * * * root /APPS/CronRun/makelist/list.py
Try the code without :
if __name__ == '__main__':
Like this:
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
f = open('/APPS/CronRun/crontest/dummy1.txt','w')
f.write('hello world it is a file')
f.close()
Maybe " crontest1.py " is executed by crontab as a module - not "main".
If that doesn't help try to update the Python.
Python 3.1.x and lower
may be the reason.

python does not create json files from crontab

I have a python file that creates json files from database, when I run it from the terminal (python pythonfile.py) it works without problems, but when I create a crontab that executes that python every 2 minutes, it does not create the files, the crontab runs the python without problems, I know because before creating the files json makes a change in the database and I can see that change in the database, what will be the problem?
-rwxrwxrwx 1 pi pi 5721 nov 15 02:36 searchProgramData.py
#reboot python /home/pi/aufen/searchProgramData.py >> /tmp/log.txt
*/2 * * * * python /home/pi/aufen/searchProgramData.py >> /tmp/log.txt
query = "SELECT * FROM det_programacion where programacion_id = %s "%programacion_grupo[0][2]
detalles_programacion = run_query(query)
json_str = json.dumps(detalles_programacion,default=datetime_handler)
#escribir json de array
with open('datos.json', 'w') as file:
json.dump(json_str, file)

Python script not work in crontab

Here is my script(randombg.py):
#!/usr/bin/env python
# -*- coding: utf8 -*-
import random
import subprocess
import os
BACKGROUND = '/home/david/wallpaper/dell2312'
IGNORE_FILES = ['/home/david/wallpaper/dell2312/.directory']
def enumerate():
global BACKGROUND
file_collections = []
for root, dirs, files in os.walk(BACKGROUND):
for file in files:
file_collections.append(os.path.join(root, file))
return file_collections
def randombg():
select_files = list(set(enumerate())-set(IGNORE_FILES))
subprocess.call(['feh', '--bg-scale', random.choice(select_files)])
def main():
while 1:
randombg()
if __name__ == '__main__':
main()
I have run chmod a+x randombg.py and it worked with python randombg.py .Let's say its path is /path/to/randombg.py. Also, run /path/to/randombg.py worked.
However, when I added it to crontab as below:
1 * * * * /path/to/randombg.py
or
01 * * * * python /path/to/randombg.py
or
01 * * * * /usr/bin/python /path/to/randombg.py
All failed.
I can't figure out. Could anyone explain?
PS: ArchLinux
More infomation
When I run ps aux|grep python, I can't find the randombg.py while sometimes it appears.
Addtional logs from crontab redirect stderr:
import: unable to open X server `' # error/import.c/ImportImageCommand/361.
import: unable to open X server `' # error/import.c/ImportImageCommand/361.
import: unable to open X server `' # error/import.c/ImportImageCommand/361.
/home/david/dotfiles/randombg.py: line 9: BACKGROUND: command not found
/home/david/dotfiles/randombg.py: line 10: IGNORE_FILES: command not found
/home/david/dotfiles/randombg.py: line 13: syntax error near unexpected token `('
/home/david/dotfiles/randombg.py: line 13: ` def enumerate():'
Try to change your subprocess.call to
subprocess.call("export DISPLAY=:0; feh --bg-scale " + random.choice(select_files), shell=True)
This should export the DISPLAY variable, as scripts run from crontab do not have access to environmental variables by default.

Categories