gintonik
Posts: 35
Joined: Fri Apr 22, 2016 4:09 pm

python and crontab

Mon Jun 03, 2019 4:43 pm

Hi,

i wrote a script what have to activate a relay. It´s a controller for a pool pump.
At 6-8am the Relay have to activate for filtering the water, after 8am the relay have to deactivate.
And from 8am up to 6pm the relay have to activated 20secs and keep 4 minutes deactivated, so the solarpanel can heating the water.
it wrote three python script, who are working.

But now i have trouble to work with a crontab!

Code: Select all

*/1 8-20 * * * pi /usr/bin/python /home/pi/Desktop/heating.py
0 6-8 * * * pi /usr/bin/python /home/pi/Desktop/cleaning_start.py
01 8 * * * pi /usr/bin/python /home/pi/Desktop/cleaning_stop.py
Thats what i paste when i use crontab -e and saved it but i dosnt work. Can someone help me to fix it?

User avatar
DougieLawson
Posts: 35840
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: python and crontab

Mon Jun 03, 2019 5:04 pm

If it's your own crontab you don't need the userid.

Code: Select all

*/1 8-20 * * *    /usr/bin/python /home/pi/Desktop/heating.py
0 6-8 * * *    /usr/bin/python /home/pi/Desktop/cleaning_start.py
01 8 * * *    /usr/bin/python /home/pi/Desktop/cleaning_stop.py
That will run heating.py every minute from 08:00 until 20:00. cleaning_start.py on the hour at 06:00, 07:00 and 08:00 and cleaning_stop.py at 08:01.

BTW, why are you using python2 for new code. All new code should be python3.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

pcmanbob
Posts: 6710
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: python and crontab

Mon Jun 03, 2019 5:20 pm

So cron will only call a program you can't use it to keep a program active for a set time.

So for your cleaning cycle you need to write the " cleaning_start.py " so that it activates the relay and do not use gpio.cleanup () at the end of it
so that the relay will stay active, then you just need to call it the once at 6am like this

Code: Select all

* 6 * * * python /home/pi/Desktop/cleaning_start.py
then at 8am you call the " cleaning_stop.py " program to deactivate the relay, then you just need to call it the once at 8am like this

Code: Select all

* 8 * * * python /home/pi/Desktop/cleaning_stop.py

Now for you heating cycle you need to write the " heating.py " program so it just activates the relay for 20 seconds then deactivates it, then you call every 4 mins it like this

Code: Select all

*/4 8-18 * * * python /home/pi/Desktop/heating.py

now if you want to see if there are any errors being generated by your programs you can add a log capture to the end of each, like this

Code: Select all

* 6 * * * python /home/pi/Desktop/cleaning_start.py >> /home/pi/log-c-start.txt  2>&1
* 8 * * * python /home/pi/Desktop/cleaning_stop.py >> /home/pi/log-c-stop.txt  2>&1
*/4 8-18 * * * python /home/pi/Desktop/heating.py >> /home/pi/log-heat.txt  2>&1
you will then have 3 text files with any program errors being log in them, if you have print statements in your programs these will also be logged in the log files.

hope this helps if you need help with the actual programs post them here and we will do our best to help.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

gintonik
Posts: 35
Joined: Fri Apr 22, 2016 4:09 pm

Re: python and crontab

Mon Jun 03, 2019 5:51 pm

Hi,

thanks for your quick answer. I had changed the job like your code:

Code: Select all

* 6 * * * python /home/pi/Desktop/cleaning_start.py >> /home/pi/log-c-start.txt  2>&1
* 8 * * * python /home/pi/Desktop/cleaning_stop.py >> /home/pi/log-c-stop.txt  2>&1
*/4 8-18 * * * python /home/pi/Desktop/heating.py >> /home/pi/log-heat.txt  2>&1
But fot the last i don´t understood, where do the code is inserted correctly:

My Collegue Loise (normally a linux-queen) told me, that i have to insert a simple file into /etc/crone.d/pump ,chmod 700 and then insert your example. But it doesn´t start after a reboot. i´m not sure thats it the correct place for the code.

gintonik
Posts: 35
Joined: Fri Apr 22, 2016 4:09 pm

Re: python and crontab

Mon Jun 03, 2019 5:53 pm

Everything back, now it works !!!!

ich done this:

sudo nano /etc/crontab -e
Insert your Code
Save File
Reboot
Works....


THANKs YOU VERY MUCH !!!!!!!

gintonik
Posts: 35
Joined: Fri Apr 22, 2016 4:09 pm

Re: python and crontab

Mon Jun 03, 2019 5:56 pm

I´m not sure but could it be possible when crontab was changed by ssh, it just works for the ssh session?
No i had insert i at terminal on the pi and it works...

pcmanbob
Posts: 6710
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: python and crontab

Mon Jun 03, 2019 6:59 pm

sudo nano /etc/crontab -e

Right that is not how you use crontab

To enter a corn line or lines for the pi use which is all you need to run a few gpio command's

At a command line or in an SSH session you just need to enter the command

Code: Select all

 crontab -e
 
The first time you use the command you are asked to select an editor which is by default nano

Then you enter the lines and save and exit if all is well you should get a message say new Cron installed.

I would have expected your Linux expert to know how to use crontab :shock:
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Other projects”