hebertovich
Posts: 20
Joined: Sun Mar 04, 2018 12:09 pm

Ending a python script in Cron-Answered

Thu Mar 14, 2019 2:36 pm

I have a new dilemma

I now have this cron working fine to start a python script

35 15 * * 2-5 cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./opengreen.py

Now I'm trying to figure out how to stop it in the cron at a certain time I've modified the cron to this
# Tasks to be run by cron.
# to run open sign
25 11 * * 2-5 cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./opengreen.py
#to stop the script
27 11 * * 2-5 /killall -9 opengreen.py >/home/pi/log_opengreen2.txt 2>&1
#next script
#


I've also tried Pkill -9 ./opengreen.py or -x and exit(opengreen.py) but to no avail

Anyone know what would work to stop the process in cron
Last edited by hebertovich on Thu Mar 14, 2019 6:52 pm, edited 1 time in total.

W. H. Heydt
Posts: 15841
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Ending a python script in Cron

Thu Mar 14, 2019 3:28 pm

My "alarm clock" uses mplayer, so in the evening I have a cron job that does "killall mplayer". That depend on that being the only thing I'm doing with mplayer, though. The more specific method would be "kill <PID>" or "kill -9 <PID>", but finding the process ID may be tricky. A "ps -C <programname>" can do it, for instance.

User avatar
thagrol
Posts: 7197
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Ending a python script in Cron

Thu Mar 14, 2019 3:36 pm

hebertovich wrote:
Thu Mar 14, 2019 2:36 pm
35 15 * * 2-5 cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./opengreen.py

Now I'm trying to figure out how to stop it in the cron at a certain time I've modified the cron to this
# Tasks to be run by cron.
# to run open sign
25 11 * * 2-5 cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./opengreen.py
#to stop the script
27 11 * * 2-5 /killall -9 opengreen.py >/home/pi/log_opengreen2.txt 2>&1
#next script
#
Take the "/" in front of "killall" out. With it, cron will be looking for something called killall in the root directory ("/") rather than searching the current $PATH for killall.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

hebertovich
Posts: 20
Joined: Sun Mar 04, 2018 12:09 pm

Re: Ending a python script in Cron

Thu Mar 14, 2019 6:24 pm

Thanks for the suggestion thagrol

I actually have it working perfect like this ----Thanks everyone for your help

# Tasks to be run by cron.
#
#to run the closed script at 7:30 every mon
30 7 * * mon cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./closedor.py
#
#to stop the script @ 6:29pm mon
29 18 * * mon cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; killall python
#
# to run open sign mon @ 6:30pm mon
30 18 * * mon cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./opengreen.py
#
#to stop the script @ 8:30pm mon
30 20 * * mon cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; killall python
#
#to run the closed script at 8:33pm mon
30 20 * * mon ( sleep 03 ; cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./closedor.py)
#
#
#to run the closed script at 7:30 tue-sun
30 7 * * 2-0 cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./closedor.py
#
#to stop the script @ 10:29am tue-sun
29 10 * * 2-5 cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; killall python
#
# to run open sign tue-fri @ 10:30
30 10 * * tue-fri cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./opengreen.py
#
#to stop the script @ 8:30pm t-f
30 20 * * 2-5 cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; killall python
#
#to run the closed script at 8:33pm t-f
30 20 * * 2-5 ( sleep 03 ; cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./closedor.py)
#
# to run open sign sat-sun @ 10:30
30 10 * * sat-sun cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./opengreen.py
#
#to stop the script @ 2:00pm sat-sun
0 14 * * sat-sun cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; killall python
#
#to run the closed script at 2:03 pm Sat-sun
0 14 * * sat-sun ( sleep 03 ; cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; ./closedor.py)
#
#to stop it all at 5pm sat-sun
* 17 * * sat-sun cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; killall python
#
#to stop it all at 10pm mon-fri
* 22 * * mon-fri cd /home/pi/rpi-rgb-led-matrix/bindings/python/samples; killall python


Does what it's supposed to do

hebertovich
Posts: 20
Joined: Sun Mar 04, 2018 12:09 pm

Re: Ending a python script in Cron-Answered

Thu Mar 14, 2019 6:57 pm

One last question if my R pi reboots I'm guessing I have to do the /etc/init.d/cron start. and enter my password again to re-initialize the cronjob??

Is there a way to set it so that if I have a power outage or my R pi reboots, the cronjob will automatically start?

could I use @reboot ? or will that hold it till I provide my password?

Return to “Beginners”