pertm84
Posts: 29
Joined: Sat Oct 20, 2018 10:59 am
Location: Norway

Crontab Issue - AKA not working?

Sat Nov 24, 2018 3:24 pm

Hi

So I am new to this world of working with cmdline interfaces, and I do not understand it well enough to know what to do (improvise), but I am good at googling the subject and copy/paste the code where it should be.

However, I seem to have confused myself with the crontab usage. I am running Raspbian Stretch on a Rpi2.

I have made two scripts which I want to run every 25 and 15 minutes.

First script

Code: Select all

sudo nano motionrestart.sh
containing:

Code: Select all

#!/bin/bash
sudo service motion restart
then I made it runnable:

Code: Select all

sudo chmod +x motionrestart.sh
I tested it with

Code: Select all

./motionrestart.sh
Then I made a python script to run the bash.script with python:

Code: Select all

sudo nano motionrestart.py
containing:

Code: Select all

#!/usr/bin/env python

import shutil
import os
os.system("bash -c /home/pi/bin/motionrestart.sh")
2nd script:

Code: Select all

#!/bin/bash

filename= ^`^|/tmp/image.jpg ^`^|
hostname="webcam.wunderground.com"
username="xxx"
password="xxx"
lftp ftp://$username:[email protected]$hostname -e "put /tmp/image.jpg ; quit"
made it runnable and tested it, then I made the python script:

Code: Select all

#!/usr/bin/env python

import shutil
import os
os.system("bash -c /home/pi/bin/wunderftp.sh")
So this is where the confusion starts, I added this to root user crontab -e:

Code: Select all

*/15 * * * * /home/pi/bin/wunderftp.py
*/25 * * * * /home/pi/bin/motionrestart.py
Because I didn't remember I added it to root, I added the cronjob to the user pi as well.
Will this cause confusion? Shall it only be root? I am accessing the system through SSH, and I have disabled root access through ssh.

What if I remove the crontab from root, and only manage cronjobs through the user pi?

PhatFil
Posts: 1197
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Crontab Issue - AKA not working?

Sat Nov 24, 2018 5:20 pm

try prefixing your path/script.py with "python " in your crontab entry
*/15 * * * * python /home/pi/bin/wunderftp.py
*/25 * * * * python /home/pi/bin/motionrestart.py
or python3 if using Python3...
https://www.dexterindustries.com/howto/ ... pberry-pi/

pertm84
Posts: 29
Joined: Sat Oct 20, 2018 10:59 am
Location: Norway

Re: Crontab Issue - AKA not working?

Sun Nov 25, 2018 10:50 am

Added python as you suggested, and thank you, that seemed to do the trick! :)

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Crontab Issue - AKA not working?

Sun Nov 25, 2018 11:53 am

pertm84 wrote:
Sat Nov 24, 2018 3:24 pm
Hi

So I am new to this world of working with cmdline interfaces, and I do not understand it well enough to know what to do (improvise), but I am good at googling the subject and copy/paste the code where it should be.

However, I seem to have confused myself with the crontab usage. I am running Raspbian Stretch on a Rpi2.

I have made two scripts which I want to run every 25 and 15 minutes.

First script

Code: Select all

sudo nano motionrestart.sh
containing:

Code: Select all

#!/bin/bash
sudo service motion restart
...

Because I didn't remember I added it to root, I added the cronjob to the user pi as well.
Will this cause confusion? Shall it only be root? I am accessing the system through SSH, and I have disabled root access through ssh.

What if I remove the crontab from root, and only manage cronjobs through the user pi?
Three of your four scripts seem to be adding complexity for no real gain.

You could replace the first two scripts (the bash and python) with a single root cron entry

Code: Select all

*/25 * * * *  service motion restart
I would put that one in the root crontab as it needs to run with root perms, you could put it in the pi crontab if you added sudo to the command. Ideally you should probably use systemctl rather than service but that's a seperate thing.

Your 2nd part I'd run from the pi crontab (as it looks like it should run as the pi user), the python script also seems a bit redundant, you could run the bash script direct from cron.

Code: Select all

*/15 * * * * /home/pi/bin/wunderftp.sh

Return to “Beginners”