Using cron to start a python script at every boot - python

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

Related

Raspberry crontab python script at boot

I've been trying to launch a python script at the boot of the Rpi, but everything I've tried until now did not work.
The script is some version of this : https://www.raspberrypi.org/learning/temperature-log/worksheet/ :
#!/usr/bin/python
import os, sys
from subprocess import check_output
from re import findall
from time import sleep, strftime, time
def get_temp():
temp = check_output(["vcgencmd","measure_temp"]).decode("UTF-8")
temp = float(findall("\d+\.\d+",temp)[0])
return(temp)
while True:
log=open("cpu_temp.txt","a")
temp = get_temp()
log.write("{0} {1}".format(strftime("%Y-%m-%d %H:%M:%S"),str(temp))+" degreeC\r\n")
sleep(60)
log.close()
It works fine on its own. I tried editing crontab, with and without the absolute path to Python, as well as editing /etc/rc.local
I know it doesn't work, because it should create a text file and edit it each minute, and it's not created at boot. I have other commands in crontab and rc.local that are working.
Need some help please !
If your script is located at /home/pi/tempcheck.py the you should edit crontab with
sudo crontab -e
and append the line
#reboot python /home/pi/tempcheck.py &
then save and exit.
Further details can be found at http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/
You can check it is running with
ps aux | grep tempcheck.py
Note that if you edit root's crontab, the python process will be run as root. So you should use absolute filenames in the python script:
log=open("/home/pi/cpu_temp.txt","a")
sudo crontab -e
#reboot /usr/bin/python /path/to/file/script.py
/path/to/file/script.py would probably be something like /home/username/script.py
If it still doesn't work you can try giving it execute permission with this:
chmod a+x script.py
You can call your script in the ~/.bashrc file. It will be called at boot or terminal opening.
Just write :
python /path/to/your/script.py
At the end of the .bashrc file.

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

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