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.
Related
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
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 ?
I have python script that must be run on a crontab.
Here is my config
Here is the content of my fastq.sh
pipenv run python Main.py
But nothing happen,
Someone have an idea?
Thanks for help
I solved this issue by following this steps:
1-I edited Main.py and added this code
!/usr/bin/env python3
as first line.
2- Transform Main.py as an executable by using this command chmod u+x Main.py
3- Add this line in the crontab 6 17,18 * * * /usr/bin/env python3 /home/robot/bin/LUXIA/Main.py
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.
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.