Py Script in crontab

Hi, i have a Script called Switch.py , to
Run it i have to write

sudo ./switch.py

and it Worms great.
But now i want to execute it every minute.

So i created with

crontab -e

a New cronjob

*/1 * * * * sudo python ./switch.py

But this doesnt work.

Any idea how to do. that ?

Use fully qualified names, eg /usr/bin/python /home/osmc/switch.py

Also suggest you create the job under root using sudo crontab -e

Doesnt work too :frowning:[quote=“dillthedog, post:2, topic:35738, full:true”]
Use fully qualified names, eg /usr/bin/python /home/osmc/switch.py

Also suggest you create the job under root using sudo crontab -e
[/quote]

That doesn’t help much. Edit the crontab and send stdout and stderr to a log file.

crontab -l after making the changes
Make sure you leave a new empty line after editing

*/1 * * * * /usr/bin/sudo /usr/bin/python3 /home/osmc/switch.py

Just the script Works Fine When i test it.
But not in the cronjob i guess.

Behind the command is an empty line.
I restarted cron, cron status is active. I also rebooted the pi.
But the script isnt Running.

How can i See if the cronjob is running and what is the error message if there is one?

Depending on what you are doing, you may not need the sudo part. When testing, are you running the script as osmc?

sudo journalctl -f

may help you find the error.

Don’t use sudo, put in root’s crontab instead.

So you’ve ignored:

  1. Run it under root without sudo
  2. Send stdout and stderr to a log file

How do you know it isn’t running? Perhaps it’s running and failing.

Add this to your (osmc) crontab

*/1 * * * * logger Cron is running

then run sudo journalctl -f to montor the system log. Ctrl-C to stop.

Edit: Missed this first time through:

/usr/bin/python3

python3? OSMC is still on v2.7.

hi, first of all thanks for all your help.

  1. How do it do that ? Switch the user to root, than crontab -e and /usr/bin/python3 /home/osmc/switch.py as cronjob? without sudo ?
  2. I have no idea how to do that.
  3. The script sends a command to 433Mhz modul, and i can see if my lamp is on or off :wink: Also if i just type /usr/bin/sudo /usr/bin/python3 /home/osmc/switch.py in SSH ,it works great
  4. I’m not at home at the moment, but i will add your lines to the cron tab.

Thanks for all your support :slight_smile:

so what i did now:

sudo su
crontab -e

content of the crontab

*/1 * * * * /usr/bin/python3 /home/osmc/switch.py
*/1 * * * * logger Cron is running

and this is the output of journalctl

Mar 31 19:38:01 osmc CRON[31378]: (root) CMD (logger 
Cron is running)
Mar 31 19:38:01 osmc CRON[31377]: 
pam_unix(cron:session): session opened for user root by 
(uid=0)
Mar 31 19:38:01 osmc CRON[31379]: (root) CMD 
(/usr/bin/python3 /home/osmc/switch.py)
Mar 31 19:38:01 osmc logger[31380]: Cron is running
Mar 31 19:38:01 osmc CRON[31376]: 
pam_unix(cron:session): session closed for user root
Mar 31 19:38:02 osmc CRON[31377]: (CRON) info (No MTA 
installed, discarding output)
Mar 31 19:38:02 osmc CRON[31377]: 
pam_unix(cron:session): session closed for user root

but the script doesnt execute.

any ideas ?

Hi. I’ll just run through the questions listed in post #10 first.

I believe that was covered waaaay back in post #2, though the method you used in post #11 is also ok.

2 Send stdout and stderr to a log file. I’m pretty sure a quick google search would have given you the answer. There are a few ways but the one most often used is to tag this to the end of your command >>/tmp/pylog 2>&1 so the full line will now read

/usr/bin/python3 /home/osmc/switch.py >>/tmp/pylog 2>&1

Two things: the file /tmp/pylog is just an arbitrarily chosen name, and could be anything you choose, and the use of double chevrons >> means that log messages will be appended to the file each time the job runs. (A single chevron overwrites the file each time.)

3 The fact that it works under SSH but not in cron suggest some environmental issue, which I hope will be clearer from the log file.

You’ve proven that cron is working, so I suggest you remove the logger message (if you haven’t done already).

thanks for your answer :slight_smile: now i see that the error is from the script, cause the log says:

sh: 1: ./raspberry-remote/send: not found

my script is this:

#!/usr/bin/python3
import os
import sys
import urllib.request
 
datei = "switch.txt"
urllib.request.urlretrieve("http://www.server.de/pi/switch.txt", datei)
with open(datei) as txtfile:
    for line in txtfile:
        vars = line.strip().split("#")
        if vars[0] == "a":       
               if vars[1] == "0":
                  os.system("./raspberry-remote/send 10001 4 0")
               elif vars[1] == "1":
                  os.system("./raspberry-remote/send 10001 4 1")
               else:
                  os.system("./raspberry-remote/send 10001 4 0")
os.remove(datei)

do i just have to write the absolute path ?

/home/osmc/raspberry-remote/send

:slight_smile:

Sure do. It’s running as root, so its $HOME is not /home/osmc.

I assume you’re sure it needs to run as root…