#!/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)
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.
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.
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 “)”)
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')