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)
Related
I am absolutely new to ubuntu and cron.
I want to run some scripts:
I edited and saved the crontab file:
37 13 30 6 * /media/xxx/xxx/bin/python /home/xxx/PycharmProjects/testcron.py
testcron.py code:
print('Hellow World')
input('Test Success')
I assumed that this would show me if the cronjob ran.
But no window popped up on the time I set.
Can someone point me to how to check if it ran? Did I configure this wrong?
If you haven't done anything on your system, try looking in
/var/log/syslog
Use grep to filter/search:
grep CRON /var/log/syslog
You can also pipe the output of your cron job to a specific location as well
37 13 30 6 * /media/xxx/xxx/bin/python /home/xxx/PycharmProjects/testcron.p >> /var/log/job.log 2>&1
I always include date >> ~/cronjobname.txt at the end of my cron job scripts. It will keep appending the date and time to that text file every time it runs. Another advantage to this is, If someone runs it manually for any reason (testing, debugging), you will have that timestamp too.
In Python, you can create a testcron.py file that looks like this:
f = open("cool_test", "w")
f.write("")
f.close()
If you run python3 testcron.py, you'll see a cool_test file generated in the same directory as testcron.py.
Now, you can just call testcron.py from a cronjob, and you'll be able to verify the process worked by making sure that cool_file was generated.
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
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
I have 2 python codes which i schedule using cron. My codes actually runs from 10:30am to 4:20pm everyday. from 10:30am to 11:00am my codes outputs 2-3 lines every minute and after that they start outputting 30-40 lines every minute. I have scheduled my codes like this.
30 10 * * 1-5 cd /home/alpha/IBpy && python LongData.py >> /home/alpha/logs/Longdata.op 2>> /home/alpha/logs/Longdata.er
31 10 * * 1-5 cd /home/alpha/IBpy && python ShortData.py >> /home/alpha/logs/Shortdata.op 2>> /home/alpha/logs/Shortdata.er
now the problem is, My programs are working all fine they are doing everything they were supposed to do. If any error occurs they immediately write the error to error log file but they are not even writing single line to output file. I checked almost every possible posts that can help me in here and in stackoverflow but unfortunately none of them helped me. however if i start the same programs at 11:00am(when programm starts outputing 30-40 lines) instead of 10:30 everything works fine. I really don't know what wrong thing am i doing. i am not supposed to start my program at 11. Any help would be appreciated.
Seems i got this problem since the data to stdout was buffered. According to this post i just changed my cron job to
31 10 * * 1-5 cd /home/alpha/IBpy && stdbuf -i0 -o0 -e0 python ShortData.py >> /home/alpha/logs/Shortdata.op 2>> /home/alpha/logs/Shortdata.er
and every thing is working fine now.
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')