[SOLVED] Cron help!

Hello smart people,

I’m having a problem whereby I can get a script to do what I want manually, but not using Cron.

The background to the problem is I have built a shut-down switch and the now need to implement the necessary script.

My cronjob looks like this:

@reboot sh /home/osmc/Scripts/s3.sh

To trigger this:

#!/bin/sh
cd /
cd /home/osmc/Scripts/
sudo python s3.py
cd /

Which in turn triggers this:

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Import the modules to send commands to the system and access GPIO pins
import RPi.GPIO as gpio
import os

# Set pin numbering to board numbering
gpio.setmode(gpio.BOARD)

# Set up pin 7 as an input
gpio.setup(7, gpio.IN)

# Set up an interrupt to look for pressed button
gpio.wait_for_edge(7, gpio.RISING)

# Shutdown
os.system('shutdown now -h')

If i run the script manually everything works as it should, but Cron just will not work. Cron status gives me this:

osmc@osmc:~$ sudo service cron status
* cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled)
   Active: active (running) since Sun 2017-04-23 14:27:36 SAST; 1min 38s ago
     Docs: man:cron(8)
 Main PID: 238 (cron)
   CGroup: /system.slice/cron.service
           |-238 /usr/sbin/cron -f
           |-261 /usr/sbin/CRON -f
           |-266 /bin/sh -c sh /home/osmc/Scripts/s3.sh
           |-269 sh /home/osmc/Scripts/s3.sh
           |-272 sudo python s3.py
           `-273 python s3.py

Apr 23 14:27:36 osmc cron[238]: (CRON) INFO (pidfile fd = 3)
Apr 23 14:27:36 osmc cron[238]: (CRON) INFO (Running @reboot jobs)
Apr 23 14:27:36 osmc CRON[261]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 23 14:27:36 osmc CRON[262]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 23 14:27:36 osmc CRON[266]: (root) CMD (sh /home/osmc/Scripts/s3.sh)
Apr 23 14:27:36 osmc CRON[267]: (root) CMD (sh /home/osmc/Scripts/test.sh)
Apr 23 14:27:36 osmc sudo[272]: root : TTY=unknown ; PWD=/home/osmc/Scripts ; USER=root ; COMMAND=/usr/bin/python s3.py
Apr 23 14:27:36 osmc sudo[272]: pam_unix(sudo:session): session opened for user root by (uid=0)
Apr 23 14:27:37 osmc sudo[287]: root : TTY=unknown ; PWD=/home/osmc/Scripts ; USER=root ; COMMAND=/usr/bin/python shutdown.py
Apr 23 14:27:37 osmc sudo[287]: pam_unix(sudo:session): session opened for user root by (uid=0)

Any suggestions?

When you run things in cron, the environment can differ. So, make sure you have fully qualified names for all files inside your scripts.

If the job really needs to run as root, better to set it to run as a root job in the first place using sudo crontab -e

Thanks for the reply!

The cron job has been created using sudo crontab -e

Ah yes, I see now. But… your first script contains the line sudo python s3.py

Generally, it is a bad idea to have scripts in /home/osmc but run them as root. It’s better to place them in, say, /usr/local/bin and have them owned by root.

Try using full path for python

1 Like

Ok I’ve moved S3.py to /usr/bin and renamed s4.py.

Amended Cron to:

@reboot sudo /usr/bin/python /usr/bin/s4.py

This was to leave out the sh script, but that may be necessary?

Cron service gives:

osmc@osmc:~$ sudo service cron status -l

  • cron.service - Regular background program processing daemon
    Loaded: loaded (/lib/systemd/system/cron.service; enabled)
    Active: active (running) since Sun 2017-04-23 15:31:46 SAST; 4min 49s ago
    Docs: man:cron(8)
    Main PID: 215 (cron)
    CGroup: /system.slice/cron.service
    `-215 /usr/sbin/cron -f

Apr 23 15:31:46 osmc systemd[1]: Started Regular background program process…n.
Apr 23 15:31:46 osmc cron[215]: (CRON) INFO (pidfile fd = 3)
Apr 23 15:31:47 osmc cron[215]: (CRON) INFO (Running @reboot jobs)
Apr 23 15:31:47 osmc CRON[258]: pam_unix(cron:session): session opened for …0)
Apr 23 15:31:47 osmc CRON[260]: (root) CMD (sudo /usr/bin/python /usr/bin/s4.py)
Apr 23 15:31:47 osmc sudo[261]: root : TTY=unknown ; PWD=/root ; USER=root …py
Apr 23 15:31:47 osmc sudo[261]: pam_unix(sudo:session): session opened for …0)
Apr 23 15:32:26 osmc sudo[261]: pam_unix(sudo:session): session closed for …ot
Apr 23 15:32:26 osmc CRON[258]: (CRON) info (No MTA installed, discarding o…t)

Why use sudo if you added it to root crontab?

Paranoia. removed it from cron. now reads:

@reboot /usr/bin/python /usr/bin/s4.py

cron status is now:
osmc@osmc:~$ sudo service cron status -l

  • cron.service - Regular background program processing daemon
    Loaded: loaded (/lib/systemd/system/cron.service; enabled)
    Active: active (running) since Sun 2017-04-23 15:47:14 SAST; 48s ago
    Docs: man:cron(8)
    Main PID: 241 (cron)
    CGroup: /system.slice/cron.service
    |-241 /usr/sbin/cron -f
    |-251 /usr/sbin/CRON -f
    |-268 /bin/sh -c /usr/bin/python /usr/bin/s4.py
    `-269 /usr/bin/python /usr/bin/s4.py

Apr 23 15:47:14 osmc systemd[1]: Started Regular background program process…n.
Apr 23 15:47:14 osmc cron[241]: (CRON) INFO (pidfile fd = 3)
Apr 23 15:47:15 osmc cron[241]: (CRON) INFO (Running @reboot jobs)
Apr 23 15:47:15 osmc CRON[251]: pam_unix(cron:session): session opened for …0)
Apr 23 15:47:15 osmc CRON[268]: (root) CMD (/usr/bin/python /usr/bin/s4.py)
Hint: Some lines were ellipsized, use -l to show in full.

Still not working :frowning:

Better is /usr/local/bin. /usr/bin is for the system stuff.

You also need to add >/tmp/pyjob.log 2>&1 to this line so you can see what errors it produced.

Okie dokie, file moved to /usr/local/bin

Cron changed to:

@reboot /usr/bin/python /usr/local/bin/s4.py >/tmp/pyjob.log 2>&1

pyjob.log is created, but has no contents.

Since the s4.py file contains the #!/usr/bin/python line, ensure that it is executable and try running it with sudo /usr/local/bin/p4.py and see what happens.

Yup,

osmc@osmc:~$ sudo /usr/bin/python /usr/local/bin/s4.py

Works great, but no joy from a cron

I should say, I’m not set on using a cron job to implement this if you have an easier suggestion to get the python script started at boot?

Leave out the /usr/bin/python part. /usr/local/bin/s4.py needs to be executable.

Mayday mayday…

osmc@osmc:~$ sudo /usr/local/bin/s4.py
/usr/local/bin/s4.py: 7: /usr/local/bin/s4.py: import: not found
/usr/local/bin/s4.py: 8: /usr/local/bin/s4.py: import: not found
/usr/local/bin/s4.py: 12: /usr/local/bin/s4.py: Syntax error: word unexpected (expecting “)”)

Please list the script. Format it with the </> symbol so that it is readable.

like so? This is copy past from the python script. (pls forgive my noobish ways)

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Import the modules to send commands to the system and access GPIO pins

import RPi.GPIO as gpio
import os

# Set pin numbering to board numbering

gpio.setmode(gpio.BOARD)

# Set up pin 7 as an input

gpio.setup(7, gpio.IN)

# Set up an interrupt to look for pressed button

gpio.wait_for_edge(7, gpio.RISING)

# Shutdown

os.system('shutdown now -h')

No. Edit the post, then highlight the text and click on the </> button at the top of the edit box.

When you paste code into the forum, please highlight it all with the r-click and select the </> icon in the post editor here.

It will 

make your code 

look appropriate

Thanks, and so I learn… Done.