Python script does not work under crontab - python

I have a very basic python script to run through crontab for every minutes.
The Script
filed = open('test.txt','a')
Crontab
* * * * * /to path the file/job.py
It should work, but i have not been able to see the results. So, what could be the problem ?

You need to open text.txt using an absolute path; the crondaemon may well be using different path from what you expect:
filed = open('/home/john/Desktop/test.txt','a')

Related

Cannot run python script in a crontab

I am trying to run a python script and I am using this code to run the crontab:
SHELL = /home/boogieman/codeCopy.py
PATH = /sbin:/bin:/usr/sbin:/usr/bin
MAILTO = ""
* * * * * /usr/bin/python/ /home/boogieman/codeCopy.py
I tried SHELL = /bin/bash too but still cannot see any difference.
When I run the codeCopy.py in my terminal, it works like a charm. But still cannot figure out what am I doing wrong with the crontab.
Here is a piece of my code which saves a dictionary into a csv file:
from time import strftime
t = strftime('%a, %d %b %Y %H:%M:%S')
body.update({'Time':t})
with open('/home/boogieman/r3edata.csv','a+', newline='') as fhandle:
writer = csv.writer(fhandle)
items = body.items()
# writer.writerow([key for key, value in items])
writer.writerow([value for key, value in items])
I am trying to save a copy of the dictionary every time when it runs.
The solutions I found on several pages didn't work for me. I changed Shell path, updated exact file paths and tried the script accessible for every user.
I explicitly used /usr/bin/python/ in my crontab command and tried #!/usr/bin/python/ in my python script too. Both of them didn't work.
Can you please help me fix this? thank you in advance
Try the following line in /etc/crontab:
* * * * * python3 /home/boogieman/codeCopy.py
you can also consider this line:
* * * * * cd /home/boogieman && python3 codeCopy.py
so you'll be running your script in the correct folder.

Cron job write to a new file every time it runs

I'm trying to run a cron job on Python3 that runs once every hour, and writes to a new log file each time. My code is currently:
0 * * * * /home/user/Projects/example.py > /home/user/Projects/cron_logs/'`date +\%d\%m\%y_\%H\%M\%S`'.log 2>&1
There were other questions asked here that I used to put together that line, but it isn't working. It creates a file titled date +\%d\%m\%y..., and I can't even open the file. What am I doing wrong?
your date format is incorrect, check out date --help and experiment.
this also goes for actually testing the command in its entirety before installing it as a scheduled command; i.e. run ./script.py > "/target/folder/$(date).log" command in your terminal to make sure it actually works, then you can put it into the crontab.
this should fix your existing entry;
0 * * * * /home/user/Projects/example.py > "/home/user/Projects/cron_logs/$(date +\%d\%m\%y_\%H\%M\%S).log" 2>&1
this would create log files with filenames looking like this : 090718_234854.log
(I would also suggest looking at ISO-8106 (e.g. date --iso-8106=s)

Save Python Crontab without Keywords

I am working with python crontab, and have the ability to save crontabs for specific users.
However the python crontab package is saving crontabs with keywords (i.e #hourly rather than 0 * * * *). I want to save without keywords as we have a couple scripts that run on the current format of our crons.
I searched through the the package, but I couldn't figure out how to do this. Below is the code snippet of what I have so far. Any help is appreciated!
#create cron obj
cron_file_path = os.getcwd() + '/' + cron
cron_obj = CronTab(tabfile=cron_file_path, user=cron_user)
for job in cron_obj:
print str(job)
if not job.is_valid():
print "Job is not valid. Please fix and retry.\n ", str(job)
#set cron
cron_obj.write_to_user(user=cron_user)
After having imported crontab try :
crontab.SYSTEMV = True

Task perform by crontab to redirect output to folder

I have a script which I run every day at particular time so and I am using cron for this task and from within cron I am also trying to redirect the output which is ".txt" file to a folder.
0 14 * * * python /home/lab/Desktop/meet.py > /home/lab/Meet1 2>&1
without using "/home/lab/Meet1 2>&1 " path I am able to run script at a given time but I am not able to redirect my output to the folder provided with the path.
Is there any way to direct my txt file output to the given folder(Meet1) or my given command is wrong.
so I need help with crontab command to redirect my txt file output to given(Meet1 in this example) folder.
Thanks for your help
You should not redirect a stream into a folder but into a file. Given that /home/lab/Meet1 really is an existing folder, try redirecting to e.g. /home/lab/Meet1/output.txt or so.
Correct answer is already provided to redirect it to a file not directory, just to add:
# Below command will overwrite contents of temp file.
0 14 * * * python /home/lab/Desktop/meet.py> temp 2>&1
# Below command will append contents to temp file.
0 14 * * * python /home/lab/Desktop/meet.py >> temp 2>&1

Not created file, in ubuntu server 12.04 Cron Job

someone help me please:
I have made a cron job in ubuntu server 12.04, the frist job recolete data from web, and set in data base, it's working well, but in secon job the python scrpit in reading database and should make a file from database data , but it not working.
rub#myserver:/etc$ crontab -e
i've edit this:
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * /usr/bin/python /home/rub/app/writeindb.py
*/1 * * * * /usr/bin/python /home/rub/app/writedatafromdb.py
the last part of my code in python writedatafromdb.py
cursor.close() #close curso in data base
conn.close() #close connecion in data base
json.dump(geojson, open('datafromdb.json', 'w'))
You are using a relative path in your code, so the file datafromdb.json gets written to the current working directory. But a cronjob doesn't run your Python script in the same working directory as you would in the shell. See What is the 'working directory' when cron executes a job over on the Unix & Linux Stack Exchange website.
Use an absolute filepath instead; one that start with a / and spells out the full path to the file.

Categories