Cronjob is not running Python script on Raspberry - python

On my Raspberry I try to run a Python script as a cronjob. The script does run and execute perfect via the console when I ask him to. However I cannot manage to run the task in cron. Why is this cronjob not working?
What I’ve did so far
Logged in as root user
Granted all rights at the .py file in the Finder to anybody and chmodded the file to 777
Edited both sudo crontab -e and crontab -e files and I did edit the /etc/crontab file (they are three different files(?!)).
Tested crons with each of the below cron codes (I did reboot after every edit).
Checked the error log /var/log/syslog/ and saw no errors regarding these cron tasks.
Tested with an output file: >> /home/root/Desktop/pi.log 2>&1. That didn’t provide me output.
cronjob commands
/2 * * * * Python /home/root/Desktop/pi.py
/2 * * * * /usr/bin/python /home/root/Desktop/pi.py
/2 * * * * cd /home/root/Desktop/ Python pi.py
The Python script is a bit long. The main function is to retreive data from a usb device and store this in an sql database. See full code : https://github.com/gejanssen/slimmemeter-rpi/blob/master/p1db/p1uitlezerdb-ESMR50.py

Related

crontab seems to run in syslog but no output is received (ubuntu)

I am using a virtualbox with ubuntu 20.04. Within the VM I have a condo environment and installed python-crontab using:
conda install -c conda-forge python-crontab
I want to continuously run a certain python file which will write to a database. I can make this happen when using job.run() but not when the job is actually on a time schedule.
I have tried the following things:
crontab -e
then adding in
* * * * * python /home/cron/... to my python file
I have also tried:
sudo crontab -e
then adding in
* * * * * python /home/cron/... to my python file
as well as just making a call to python and running the following:
from crontab import CronTab
cron = CronTab(user=True)
job = cron.new(command='python /home/cron/... to my python file')
job.minute.every(1)
cron.write()
user=True I have also tried replacing by user=cron
Now as mentioned I can run job.run() and have new entries in my database. But when it is on a schedule nothing gets written to my database anymore.
My syslog file says the following:
(cron) CMD (python /home/cron/... to my python file)
(CRON) info (No MTA installed, discarding output)
(root) CDM (python /home/cron/... to my python file)
These messages do get written every minute, so cron does seem to run, why do I not see any output in my database then?
ps with some digging on the internet, I was told that the info message shouldn't interfere with the cron job itself

Python crontab - How to check if cron is running?

I currently have a python script at
/home/ubuntu/test/test.py
When this script runs, it writes to a file
/home/ubuntu/test/test.txt
I am completely new to cron, and not very familiar with linux in general. I am trying to set up a cronjob that basically runs this script every minute.
I saw some people suggest #!/usr/bin/env python so I added it, but I noticed I don't even have a env folder in /usr/bin
I then ran chmod -x test.py. Then added an entry to cron * * * * * /home/ubuntu/test/test.py. Noted this wasn't working and saw someone suggest trying * * * * * /home/ubuntu/test/test.py 2>&1 /tmp/testlog.log. But when I check /tmp i only see a folder crontab.8Rxowt/crontab/cron and i don't see any log file created.
I am kind of confused now, I can't figure out why nothing is being updated at all. I'm not sure if the script being run needs to be placed somewhere specific, or if I screwed something up with my cron installation, or something else altogether.
I noticed trying to run ./test.py gives permission denied, and sudo ./test.py gives command not found. Is my shebang not working? I verified im using unix line endings.
To make it run every minute you have to add the path to python from your system:
* * * * * /usr/bin/python cd /path_to/test.py
I suggest you to test it with a simple command such as "touch"
* * * * * /usr/bin/touch cd /path_to/test.txt
https://crontab.guru/every-1-minute

Crontab Python Script Not Working

I am running a python script on a Linux EC2 instance (the standard AMI) and I am having trouble executing a python script through the Crontab. I have another cron job already running and followed the same format. I think I'm missing something simple, but have had trouble identifying the cause. Here is what pops up when I run crontab -e
*/5 * * * * ~/scripts/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --disk-space-util --disk-path=/ --from-cron
*/1 * * * * ~/scripts/python cpu-util.py
The error I get in the logs is /bin/sh: /root/scripts/python: No such file or directory
I'm a little confused about this error message because the path from when I log in is ~/scripts, which has my Python script.
I also tried */1 * * * * ~/scripts python cpu-util.py (which I thought made more sense), but rearranged my code based on this other post to no avail.
Also, does it matter if I run these tasks from root or ec2-user? I just put the same scripts in both to be safe (sorry if this is two questions in one, but just curious about this...)
Any input would be great. Thanks!
in your line you are looking for
the python application inside your scripts folder
I guess this is not what you intended. Try this out:
*/1 * * * * /usr/bin/python ~/scripts/cpu-util.py
I think this should work.
Also you can call it directly using ./ just putting inside your python script as the first line.
#!/usr/bin/env python
Then you can run it like this
*/1 * * * * /usr/bin/sh ~/scripts/cpu-util.py

cron python file doesn't work on centos 7

i'm trying to schedule my pythohn script into Centos 7 with cron.
On my script at start i have added this:
#!/usr/local/bin/python
and this is my cron file that i have create into folder that contain python file
*/5 * * * * /usr/local/bin/python /home/Documents/SCRAPE_PYTHON/SCRAPE.py &>> /home/Desktop/log.txt
i have try to run the script into terminal by chmod +x and works fine.
But whit this configuration when in terminal i set crontab .cron the job doesn't work. The log file set into cron file are not write and script not run. The script could be write some data into db and the db is always empty.
Any help???
Thanks
Type crontab -e in the shell, this will open the cron editor. Here copy paste the entire command.
*/5 * * * * /usr/local/bin/python /home/Documents/SCRAPE_PYTHON/SCRAPE.py &>> /home/Desktop/log.txt
Now press Esc key then type colon :wq! and press Enter key. This will install your cron. Your cron should run every 5 minutes.
Your standard output and error will be appended to /home/Desktop/log.txt.

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

Categories

Resources