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
Related
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.
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)
I have already seen a lot of questions around this.. and a lot of proposed solutions. But none have worked for me so far. So here we go...
A simple python script called test.py:
#!/usr/bin/env python3
from datetime import datetime
print('Im alive')
fn = 'msgs.txt'
with open('/home/username/Documents/code/production/msgs.txt', 'aw') as f:
f.write('%s\n' % datetime.now())
And here the line in the sudo crontab -e file
*/1 * * * * /home/username/Documents/code/production/test.py >> /home/username/outputlog.txt
The log shows that the program runs and executes properly, I have used full paths in specifying the file that Im writing too, stated the job in sudo crontab in case anything goes wrong with the user... and at this point I am lost. I don't know what to change anymore and all others questions that Ihave seen wont help me further.
Anyone else has another idea here?
Short Answer: Change 'aw' file open mode to 'a'
With your current code, you'll get the error
ValueError: must have exactly one of create/read/write/append mode
because you are trying to combine append ('a') and write ('w') modes. Simply using append ('a') mode fixes the problem.
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.
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')