Crontab Python cannot write log file and execute - python

Yesterday, I was able to write log file and execute Crontab in Mac.
However, today I cannot write log file and execute Crontab at all.
What I did yesterday was :
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
To try to handle Chrome.
After it, I can execute Python file in VS code but cannot do anything in Crontab.
Experts, could you help me?
My code for crontab is :
* * * * * /Users/XXXXXX/anaconda3/bin/python && /Users/XXXXXX/Desktop/python/.vscode/python/1234.py >> Users/XXXXXX/Desktop/python/test_log.log 2>&1
Thanks in advance.

To check crontab logs for errors that might show what is going on try
grep -i cron /var/log/syslog
If crontab was executed but there was an error in the script, that might be sent to system email if it is setup, else you might check
/var/spool/mail
for cron error messages.

Related

Can't execute a cron job

I am creating a cron job to execute a python script
hello.py
a = 'a cron job was executed here'
text_file = open('output_hello.txt', 'w')
text_file.write(a)
text_file.close()
Works fine if I execute via terminal, I am on ubuntu 15.10.
My cron job file is:
* * * * * /usr/bin/python /home/rohit/hello.py
(excluding the #)
I am a root user and creating the job in /var/spool/cron
The issue is that it is not executing the script. I don't know why.
One does not simply modify the crontab, you run the command:
crontab -e
and edit from there. Execute the above command using sudo if you want it to run as root.
Assuming your paths are correct, your script may not have the right environment or it may not be executable. Ensure your script starts with:
#!/usr/bin/python
And also that you then give execute permission to that script:
chmod a+x hello.py
Ensure you use crontab -e and if you have any doubts about your syntax, you can find more info here:
https://help.ubuntu.com/community/CronHowto

Using cron to start a python script at every boot

I tried using cron to start python script , at boot (as mentioned in the link).Running a python script At Boot using cron
I made a python script "hello.py":
#!usr/bin/env python
import time
print "Hello World!"
time.sleep(10)
Then chmod +x hello.py.
I checked,if it running or not,it is giving me output.
I used crontab -e command and added the line #reboot python /home/pi/hello.py &.
Reboot using sudo reboot , but nothing happened! I am newbie .Although I read many discussions but I am not able to fix that!
Generally when I want to verify whether a cronjob ran or not, I redirect all output to a log file like so:
12 0 * * * python /Path/To/File.py > /Path/ToLog/log 2>&1
Then you can check this log timestamp and for its contents

Running crontab with python

Python crontab script doesnt seem to work. When i run it manually,
python /home/ec2-user/code1.py
it works fine but when put into cron.txt file for the crontab, doesnt.
My crontab file is:
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
i also tried
0 * * * * python /home/ec2-user/code1.py >/dev/null 2>&1
But neither have much luck.
sudo crontab -l
#hourly python /home/ec2-user/code1.py >/dev/null 2>&1
Shows everything functional.
I tried Crontab not running my python script and couple others with not much luck either.
EDIT:
With
PATH=/opt/python2.7/bin
MAILTO=my#email
*/5 * * * * /home/ec2-user/code1.py
Email i get is:
/bin/sh: /home/ec2-user/code1.py : No such file or directory
Yet I can open and edit the file no problem. I tried many different thing but it comes down to this: cron doesnt see the file.
Feels like I went through entire https://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
and still no luck
Verify cron is running: ps aux | grep [c]ron should show a running cron process
Remove the redirects from the command so that cron emails you the output
Add a MAILTO=<email address> to your crontab, so that you get the email
Put the full path to python (/opt/python2.7/bin/python) instead of just python in the command
Add another command to crontab such as echo FOOBAR and verify that you get the email.
ls -l /homeec2-user/code1.py ? Should that be /home/ec2-user/code1.py
Only ever edit a user's crontab with crontab -e never from another platform, or by editing the file directly.
Run crontab -l | cat -A so that we can verify all the control characters are correct.
did you check the following points?
is your script executable? chmod 700 code1.py
the first line in your code should be, in most cases the python is installed at this place
#!/usr/bin/python
after that the crontab as follow should execute
0 * * * * /home/ec2-user/code1.py >/dev/null 2>&1
If the error message is correctly copy/pasted, it seems to reveal that there is a problem with the crontab file. If you created it on a foreign platform, it might be best to start over with an empty file, this time creating it in a native editor.
As others have already pointed out, redirecting output and errors to /dev/null basically makes debugging impossible, so don't do that. If your program creates copiously verbose uninformative output, run it in a wrapper which filters out the trivial diagnostics, or, if it is your own program, rewrite it to run silently in normal operation.
Did you try "/usr/bin/python" instead of "python"?
ps ax | grep python
will give you the path you could use.
try this command that should hopefully where your python is :
which python
very likely you will have something like
/usr/bin/python /home/ec2-user/code1.py

Using Crontab with Python

I'm trying to schedule a python script to run every minute or every hour.
What I did so far :
crontab -e
In the crontab file I added
* * * * * /usr/bin/python /path/to/script/script.py
After I save the file I get the message
crontab: installing new crontab
However crontab is not running the script.
Any ideas?
Thanks,
Diez
I found the problem, I will put here the solution maybe it helps someone.
I tried putting in crontab -e * * * * * /usr/bin/python /path/to/script/script.py >>/tmp/script.out 2>&1 as Rafal suggested however the output was blank.
I read a lot about crontab last night and I found out that if you use a script with crontab and you write your output in a certain file you will need to modify your script and input the exact path for the file. If you don't put the full path the script will work when you run it manually. The script will still work if you run it with crontab but it won't know where to place its output.
So modifying
with open('output.txt', 'a') as f
with
with open('/path/to/file/output.txt', 'a') as f
did the job for me.
Thanks,
Diez
Depending on the script (e.g. scripts which prints a message to the console) you won't be able to distinguish whether the script is running or not.
You can assure that crontab is running correctly by typing in the console:
tail -f /var/log/syslog

Add CRON via shell to run Python

My cron doesn't seem to execute every 5 mins. Can anyone show me where I have gone wrong?
I made it executable using this command:
chmod +x /etc/utilities/poll.py
I can run it manually with this command:
cd /etc/utilities
python poll.py
When I run it like this I get an error:
root#li453-78:~# /etc/utilities/poll.py
-bash: /etc/utilities/poll.py: Permission denied
This is the command I use to add it to shell (via my automatic deployment script):
crontab -l | { cat; echo "*/5 * * * * /etc/utilities/poll.py"; } | crontab -
The start of my python file is like this:
#!/usr/bin/env python
So, could someone please enlighten me about how I should be adding the cron to my debian server via shell so that it executes?
Using the help from here, for whatever reason, even though I had the code correct to make the script executable, this line didn't seem to fire in my deployment script, meaning that all I had to do was run it afterwards to make it executable and then everything worked.
Lesson learnt: If you need to do this, the code above works

Categories

Resources