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

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

Related

problem with cronjob to run python script on ubuntu server

I have installed python3 following the Digital Ocean guide and to run my python scripts just by command line, this is the bash I use and it works:
~$ source /home/username/python_projects/project_name/bin/activate
~$ python3 /home/username/python_projects/project_name.py
~$ deactivate
if I put that commands in crontab in the same order, nothing happens.
0 7 * * * source /home/username/python_projects/project_name/bin/activate
0 7 * * * python3 /home/username/python_projects/project_name.py
0 7 * * * deactivate
What am I doing wrong?
Cronjob is active and running.
~$ systemctl status cron
The python file has this permissions:
-rw-rw-r-- 1 user_name user_name 17075 Feb 7 02:30 python_projects/project_name.py
The activate job runs, then exits, and then the next cron job knows nothing about what it did. You want to run them all in the same process (though running deactivate at the end is pointless, as everything that activate did will be wiped when the job ends anyway).
In practice, you can run the Python interpreter from within the virtual environment directly.
For what it's worth, the error message about "no MTA installed" means that the output from cron was discarded because the system could not figure out how to send it by email. You'll probably want to change the rule to write the output to a file.
0 7 * * * cd python_projects && ./project_name/bin/python3 project_name.py >>project_name.log 2>&1
Perhaps notice that cron always starts in your home directory, so you don't have to spell out /home/username (provided of course that username is the user whose crontab this runs from).
There are a few things that could be causing your cronjob not to run:
Environmental variables: The source command in your cronjob sets up the virtual environment, but this environment is not passed on to the cron process. You should specify the full path to the Python executable in your virtual environment, e.g. /home/username/python_projects/project_name/bin/python3.
Permission issues: Ensure that the cron user has permission to execute the script and access the virtual environment.
Output: By default, cron does not send any output to the terminal. If your script produces output, you may want to redirect it to a file for debugging purposes. You can do this by adding > /tmp/output.log 2>&1 to the end of your crontab entry.
You can check the system logs for any error messages related to your cron job. The logs are usually located in /var/log/syslog or /var/log/messages.
I hope this helps!

Cronjob is not running Python script on Raspberry

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

Crontab of python script using notify-send

This is my python script for notify send:
#! /usr/bin/env python
import os
mstr='The scoreis 102/3'
title="SCORE"
os.environ.setdefault('DISPLAY', ':0.0')
os.system('notify-send -i "notification-message-IM" "'+title+'" "'+mstr+'"')
It works normally when I run the script,but while trying to run it from cron it is not working
I have tried reference from this links:
Cron scheduler of python script using notify-send
Cron with notify-send
Even in crontab i have tried to run a notify-send command like this:
* * * * * export DISPLAY=:0.0 && /usr/bin/notify-send "How are you"
But of no help.
Is there anything I am doing wrong please suggest.
I'm using a little bit different code, but the end result is what you were trying to achieve: get notify-send to work from a python script via crontab.
Here's my Python 3.x script:
import subprocess
subprocess.Popen(['notify-send', 'Running.'])
And here's how I setup crontab:
DISPLAY=:0.0
XAUTHORITY=/home/matrix/.Xauthority
*/1 * * * * /usr/bin/python3 /home/user/path/to/my/script.py
This should work with Ubuntu 16.04 and python3, I cannot speak for other OS's, but you can give it a try. Use which to check the path of your python or python3 installation.
By the time I finished this answer the "Running." notification popped up at least 3 times.

command representation in crontab

Normally I have a crontab like:
1 * * * * /home/praneeth/wru-pam/venv/bin/python3.4 /home/praneeth/wru-pam/pam_site/manage.py notify_about_changes
I have been raising serious issues with crontab execution at some times it raises an error that main.cf is missing and when I have have created a file main.cf
Next fatal error it raised what etc/mailname is missing.
To some extend I believe commands which are executing in terminal are not absolutely written in crontab. I have the commands below to execute my task
In the Linux Terminal I normally execute in this procedure:
$ source venv/bin/activate
$ cd pam_site
$ export DJANGO_SETTINGS_MODULE=project.settings.development
$ python manage.py notify_about_changes
Task: To represent the above commands in crontab which would enable me to avoid any postfix/sendmail error and execute notify_about_changes.py(which sends email notifications in a timely manner)
finally I got the problem resolved as below:-
1.I re-edited the crontab as
$sudo crontab -e
*/1 * * * * /home/praneeth/wru-pam/venv/bin/python3.4 /home/praneeth/wru-pam/pam_site/manage.py notify_about_changes --settings=project.settings.development
2.restarted the crontab as
$sudo service cron restart
For every one minute I have been getting email notifications.

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

Categories

Resources