How to check if cronjob ran in ubuntu? - python

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.

Related

crontab running python code is not saving outputs to file [duplicate]

This question already has an answer here:
crontab failed to run python script at reboot
(1 answer)
Closed 1 year ago.
I've started a compute engine instance in Google Cloud, I created a folder called "python" in the main dir, and "one.py" in that dir. Than I pasted this into one.py:
from datetime import datetime
import os
a=datetime.now()
file_to_open = os.path.join(os.getcwd(), "raw_data.txt")
with open(file_to_open, "a+") as file_object:
file_object.seek(0)
data = file_object.read(100)
if len(data)>0:
file_object.write("\n")
file_object.write(str(a))
file_object.close()
So far, so good. Dates are being saved to the file.
Than I added this to cronetab:
crontab -e
...
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * python3 ~python/one.py
The cron job is working, I'm getting this outputs after running
grep CRON /var/log/syslog
Mar 21 11:33:01 instance-2 CRON[605]: (myname) CMD (python3 ~/home/python/one.py)
Mar 21 11:33:01 instance-2 CRON[604]: (CRON) info (No MTA installed, discarding output)
Mar 21 11:34:01 instance-2 CRON[647]: (myname) CMD (python3 ~/home/python/one.py)
Mar 21 11:34:01 instance-2 CRON[646]: (CRON) info (No MTA installed, discarding output)
I thought it might be sth with the root directory being in a different place so did find . raw_data.txt
there's only one, under ~python/
Can someone help me fix this? Why the cron job isn't saving the dates to the file?
My assumption is that it is looking for raw_data.txt in the wrong folder and failing because it cannot find the file. To see the full about of the script, add a log file for the cron job to dump the output to. You could say something like: * * * * * /use/bin/python3 /home/m/one.py >> /home/m/out.log. This would dumb the full output of the execution to out.log and give you all the information you need to solve the issue.
Without knowing more, my assumption is that the issue is caused by os.getcwd(). This does not get the current working directory of one.py but the current working directory where cron is executed from. Since you are opening as a+, the file must exist first in order to append to the file. What you want instead is os.path.dirname(os.path.realpath(__file__)) which will give you the full directory path of one.py regardless of where the script is called from. Change that line to be this:
file_to_open = os.path.join(os.path.dirname(os.path.realpath(__file__)), "raw_data.txt")

Unable to write files in python

Today, I was testing my old python script, it was about fetching some details from an API and write then in a file. Until my last test it was working perfectly fine but today when I executed the script it worked, I mean no error at all but it neither write nor created any file. The API is returning complete data - I tested on it terminal, then I created another test.py file to check if file write statements are working, so the result was - they were not working.
I don't know what is causing the issue, it also ain't giving any error.
This is my sample TEST.PY file
filename = "domain.log"
with open(filename, 'a') as domain_file:
domain_file.write("HELLO\n")
domain_file.write("ANOTHER HELLO\n")
Thank you
Using 'a' on the open call to open the file in append mode (as shown in your code) should work just fine.
I don't think your issue is on the Python side. The next thing to check are your directory permissions:
$ ls -al domain.log
-rw-r--r-- 1 taylor staff 60 Apr 16 07:57 domain.log
Here's my output after running your code a few times:
$ cat domain.log
HELLO
ANOTHER HELLO
HELLO
ANOTHER HELLO
HELLO
ANOTHER HELLO
It may be related to file permission or its directory. Use ls -la to see file and folder permissions.

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)

Cron job is writing the error to error log but fails to write outputs to output log file

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.

Attachment empty when shell script runs python module to make file and email

My shell script runs a python task manager module that, at the end, makes a .png file. Then the shell script emails me the .png. If I run the tasks separately, they're fine, but combining them in the same .sh results in an email with an empty attachment.
Tail end of the python:
fig = plt.figure()
ax2 = fig.add_subplot(111)
ax2.scatter(goodRIDs.ASL75,goodRIDs.closet_count_per_slot,
color='green',alpha=0.2)
ax2.scatter(badRIDs.ASL75,badRIDs.closet_count_per_slot,
color='red',alpha=0.5)
plt.savefig('todaysASL'+str(pct)+'chart.png')
path = os.getcwd()
os.chdir(path+'/ASLgraphs')
plt.savefig(today+'ASL'+str(pct)+'vClosetperSlot.png')
And the shell script:
. venv/bin/activate
file=/home/todaysASL75chart.png
file1=/home/todaysASL95chart.png
python ~/WeeklyRIDTrendFinder.py 75 > $file
python ~/WeeklyRIDTrendFinder.py 95 > $file1
(echo "Today's ASL chart for the past week" ; uuencode $file ; uuencode $file1 )
| mail -s "Weekly ASL Chart" -r "from#email.com" me#email.com
Is this some sort of flushing issue? I tried doing plt.flush(), but apparently plot modules don't act like files.
Is it possible you need a delay between the chart creation and email creation? Maybe try a 2 second delay and see what happens. I have seen that solve issues in scripts before, might be worth trying.
Maybe savefig isn't closing the file correctly (and relying on the garbage collector). Try this:
#...
f=open(today+'ASL'+str(pct)+'vClosetperSlot.png', 'wb')
plt.savefig(f)
f.close()
#...
Turns out this is a stdout vs. file save issue -- the shell script takes whatever python prints, and saves it at file and file1. Python, however, is saving the file and not printing anything, so the shell script collects nothing and saves it.
Changing the shell script to this:
. venv/bin/activate
python ~/monica-repo/dev/bi/RIDqualifier/WeeklyRIDTrendFinder.py 75
python ~/monica-repo/dev/bi/RIDqualifier/WeeklyRIDTrendFinder.py 95
(echo "ASL chart with the past week of RIDs at 48hrs") | mailx -s "Weekly ASL Chart" -a todaysASL75chart.png -a todaysASL95chart.png me#email.com
Made it work just fine

Categories