Reputation: 77
I want to run a Python script every night at 12:00 am to clean up a csv file which is automatically filled in during the day by another script.
My csv file code_list.csv content looks like this at the end of the day:
87698
63753
19742
89876
62765
79832
# etc.
I use the truncate
method to clean the csv file with this script csvclean.py :
import csv
filename = "/root/folder/code_list.csv"
f = open(filename, "w")
f.truncate()
f.close()
and here is my crontab:
SHELL=/bin/bash
0 0 * * * cd /root/folder && /usr/bin/python3 ./csvclean.py
Both files are located in the same folder /root/folder
.
I can see in /var/log/syslog
that the cron is running at the scheduled time but nothing is happening inside my code_list.csv file. While if I run the script directly using python3 csvclean.py
it cleans up the csv file perfectly.
What have I forgotten?
- EDIT -
I used a method found here to retrieve logs. I edited my crontab like this:
*/1 * * * * cd /root/folder && /usr/bin/python3 ./csvclean.py > /root/folder/csvclean.log 2>&1
and I added this line at the beginning of the csvclean.py script:
print 'starting'
I opened the csvclean.log file after the cron and I saw:
/usr/bin/python3: can't open file './csvclean.py': [Errno 2] No such file or directory
Which made me realize that my Python script file name is cleancsv.py and not csvclean.py.
It was under my eyes...
Upvotes: 1
Views: 168
Reputation: 9311
Run this command: sudo /etc/init.d/cron restart
after setting crontab demon has to restart.
Also check sys/log
for tracing what is issue while running script.
Upvotes: 1