command representation in crontab - python

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.

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!

How to run git pull from subprocess

I am trying to run a subprocess command to do a git pull.
The cwd of the Git repository is /home/ubuntu/Ingest.
The id_rsa that I'm using with Github is located at /home/ubuntu/.ssh/id_rsa.
How would I run a subprocess call to do the following?
import shlex, subprocess
subprocess.call(shlex.split('git pull origin master'), cwd='/home/ubuntu/Ingest')
The log looks like:
movies_ec2.py:43#__init__ [INFO] Version not up to date...Doing a git pull and exiting...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
The script is running from cron and is not picking up on the id_rsa. (Note: I am not looking to use GitPython). What do I need to change in my cron job or script so that this will work? My cron job is currently:
# sudo crontab -e
*/1 * * * * STAGE=production /home/ubuntu/Ingest/ingest/movies_ec2.py > /home/ubuntu/test.log 2>&1
The following answer addresses this question quite well: How to specify in crontab by what user to run script?. However, in short, this can be accomplished by modifying the user in the crontab to be "ubuntu", which is the current user that is used to create all the files, etc.
$ sudo vim /etc/crontab
*/1 * * * * ubuntu /home/ubuntu/Ingest/ingest/movies_ec2.py > /home/ubuntu/test.log 2>&1

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

Python script inside docker creates empty file with CRON

I have a CRON job set like this :
0 0 * * * cd /home/path/to/script && sudo -u myuser ./thescript.sh
This script builds a docker image running a simple python app dumping on file a test validation report. When I run this script in a terminal everything works fine (the generated file goes in /home/myuser). Unfortunately, when I run the CRON job, the file is created, but empty. It must have something to do with the Root owning the CRON job, but I can't figure out how to get it done
Any clue ?
A sudo requires a TTY and cron doesn't run commands with a TTY. You need to run the cron for root user
This can be done using
sudo crontab -e
Then in cron don't use sudo
0 0 * * * cd /home/path/to/script && ./thescript.sh

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.

Categories

Resources