I need help using Cronjob, with Python3 scripts - cron

Okay, so i have 2 python3 scripts.
First: run.py
Second: run.py
I want to run both scripts at startup automatically within Cron Job, and then they should restart automatically every 1 hour.
Before i managed both manually, with the following command:
python3 /root/script/rindes/run.py
And then manually stopped it by ALT + C.
First and Second Python scripts is located in two different folders.
I've tried, without any success...

You can use below cron it may help you with success.
0 * * * * /usr/bin/python3 /root/script/rindes/run.py
It will run every hour at 0 min also please confirm the path of python3 in your system via which python3 and put this output at the place of /usr/bin/python3

Related

Cronjob not working. Executable works, output is logged

I am running apython script in a shell script, but now fallen back to running the python script directly from the crontab. All commands I put in crontab work when run in the terminal.
Also made sure my daemon is running, that I can see my job in /var/spool/cron, that there is an empty /usr/lib/cron/cron.deny file and no cron.allow file.
The path used in crontab does contain the path to python3 (/usr/bin) and I have tried different ways of writing the fact that I want to run it every minute: *, */1.
Here's the cronjob:
*/1 * * * * /usr/bin/python3 /home/me/Desktop/path/to/script.py >> ~/loggg
I scattered print statements in my python script and only a few surface in loggg, so im guessing it is something in the script, but I made sure that my home environment and python3 has all the right pip packages called. Actually I know the script goes through the import phases as I get a print statement after those.
One thing possibly is I used pip package wget within the script calling on an API. I know the call works when not a cronjob, but maybe that is what is messing up the job ?

crontab does not execute python script

I am facing an issue of crontab does not run the python script in Raspbian GNU/Linux 9.4 (stretch) installed on a Raspberry Pi3. I have done lots of research on this topic e.g. by following the troubleshooting guide in https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working/24527#24527 , but none of them solved my issue.
The python script that I want to run is located in
/home/pi/Documents/Fork_BookManager.py
I have made sure that anyone can execute, read and change content to the above file.
I can run this file through terminal by writing
usr/bin/python3 /home/pi/Documents/Fork_BookManager.py
I know the file is running, because the Fork_BookManager.py is using selenium to open a web browser, and I see this web browser being opened. There is no error observed when running the Fork_BookManager.py file from terminal.
In terminal executed
crontab -e
Using nano as editor, I first verified that crontab acutally worked by
* * * * * env > /home/pi/Documents/env.output
After a minute there is a env.output file in Documents folder with below parameters.
LANGUAGE=da_DK.UTF-8
HOME=/home/pi
LOGNAME=pi
PATH=/usr/bin:/bin
LANG=da_DK.UTF-8
SHELL=/bin/sh
LC_ALL=da_DK.UTF-8
PWD=/home/pi
I noticed that the path from crontab is different from the path when env is called from terminal as I will have the below path if I call env from terminal
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
So I know that crontab -e was working since I otherwise would not have got the env.output file created in Documents folder. I proceed by calling crontab -e from terminal and copied the path from env into the crontab like below
PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
* * * * * /usr/bin/python3 /home/pi/Documents/Fork_BookManager.py
Saved the script and waited. Nothing happend.
To summerize I have:
Made sure that the Fork_BookManager.py is executable
Verified that crontab is working
Updated path from env into the crontab script
which are three largest cause of experiencing difficulties in making crontab to work according to this guide
https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working/24527#24527
Still it is not working. What have I missed? Is there a better practice to achieve what I want, namely to execute the python script once a minute without doing it directly from the python script itself such as a while loop with a time.sleep(60)
ADDITIONAL INFORMATION
I even tried to expand the path in crontab script by
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/bin:/home/pi/Documents
in an attempt to specify in which folder the python3 and Fork_BookManager.py is located. Still no luck.

Cron jobs on RaspBMC

I'm trying to make the following script run at 17:00 everyday on Raspbmc using crontab. Cronjob scheduler is activated and running, but the script does not get executed. The crontab I used is:
0 17 * * * python /home/pi/.kodi/userdata/test.py
and my script is:
import xbmc
xbmc.executebuiltin("PlayMedia(/home/pi/.kodi/userdata/playlists/music/test.m3u)")
xbmc.executebuiltin("PlayerControl(Random)")
I could use a service called "XBMC Alarm clock", it works, but I need the songs to be shuffled!
What am I doing wrong?
I figured it out, I had to execute the following through SSH:
1).sudo apt-get install xbmc-eventclients-xbmc-send
2).xbmc-send --action='RunScript("/home/pi/.kodi/userdata/test.py")' (to test if the script works through SSH)
or
3).0 17 * * * xbmc-send --action='RunScript("/home/pi/.kodi/userdata/test.py")' (to set the crontab)
Of course you need to make the script executable (755 permissions) and set the python env, as previously mentioned.
Thanks for the the replies!
I've never used Raspbmc but since you are using python:
have you made your test.py executable? (eg: 755 permissions on the file)
set the python env in test.py
I tend to prefer the second option when working with python files and add at the top of my file something like this:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
R.

Cron & Virtualenv : Cron does not run a python script that requires a virtualenv

EDIT 2 27/06/2013: The problem was a silly mistake unrelated to venv and cron.
Running venv from cron with the same user that created the venv works great using the activate desscribed below.
EDIT 25/06/2013: Since nohup.out is unchanged after the cron runs, I suspect the problem is in the use of virtualenv.
The cron is set with the same user than the one running the script from the command line.
I have written a script to activate venv then run a python script. It runs perfectly from the command line, when I do
nohup /home/heyheyhey/run.sh &
However, for some black magic reasons, the python part does not run from cron:
0 4 * * * /home/heyheyhey/run.sh &
Content of run.sh:
#! /bin/bash
cd /home/heyheyhey
. /home/heyheyhey/.pythonbrew/venvs/Python-2.7.2/venvname/bin/activate 2> error.log
python /home/heyheyhey/top.py 2> error.log
bzip2 -c "Exporter.csv" > "extraction.csv.bz2"
The run.sh executes since the output compressed file is created.
However the python script does not work since the Exporter.csv is not updated and I do not see activity into the log file.
Thanks for your help!
The environment of a cron job is often different from the environment you see
when you're logged into an interactive shell. In particular, you might want to
check whether the python interpreter is on the $PATH for the cron job. If your
python program reads any environment variables, you should check those too, to
ensure they're set as expected under cron.

How to execute a php script every day [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Cron job on Ubuntu for php
I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.
So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.
I tried setting up a task through plesk which is provided through my web host service but it didn't seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.
I have the directories on my server:
cron.hourly
cron.daily
cron.weekly
cron.monthly
I thought i could dump i file in there and it would execute for example every day, but i'm guessing i need to make a cron script to call a php script right?
If i were to go the way of putting a file in the cron.daily folder how would i go about it?
Also if there are any steps i need to take on the php side please let me know?
Thanks a lot for your time.
There's couple of ways to setup cron job. Assuming you got shell access you could do crontab -e from console and define job there, i.e. like this:
1 22 * * * command
which would trigger command (whatever it is) at 22:01 each day (not sure why you set minutes to 1 instead of 0 though). To launch PHP script from there you would either have to install php-cli, and then invoke it that way:
1 22 * * * <path>/php -q script.php
You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash - sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it's simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (chmod a+x script.php) and adding shell's shebang:
#!/usr/bin/php -q
<?php
...
If your script got too many dependencies and you'd prefer to call it via web, you could use wget to mimic a browser. so your command would be:
/usr/bin/wget --delete-after --quiet --spider <URL-TO-YOUR-SCRIPT>
wget manual can be accessed by man wget or wget -h, or is on this website. Aternatively you may use HEAD tool from perl-www package - but it requires perl while wget is a standalone tool. If you use HTTPS with self signed certs, add --no-check-certificate to your invocation arguments. And you may also want to setup .htaccess and limit web access to your cron script to localhost/127.0.0.1
every minute:
* * * * * /path/script.php
every 24hours (every midnight):
0 0 * * * /path/script.php
Se this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml

Resources