Python Forum
how to stop and start a script for 30 seconds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to stop and start a script for 30 seconds
#1
Hello and thank you for having me.

At the moment i run a shell script using start.sh and then stop it with stop.sh.

Is there a way i can run the script for 30 seconds then stop the script then run it again for 30 seconds and so on.

This is what i am using at the moment:

rm -f test.out

nohup /usr/bin/python test.py > test.out

Many thanks

Paul
Reply
#2
You could use a loop.
Then the script runs the whole time.
In this example the script never stops and print each 30 seconds Hello World.

import time


while True:
    print("Hello World")
    time.sleep(30)
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Hi, thank you for the above.

So how would i incorporate your solution into my code above.

many thanks

Paul
Reply
#4
schedule work fine for this.
You could probably do all in Python,and drop the the shell script.
Here a example of making a file then delete,run_sh example on how to call shell script with subprocess.
import schedule
import time, os
import subprocess

def make_file():
    fn = 'test.out'
    open(fn, 'a').close()
    print(f'Making <{fn}>')

def del_file():
    try:
        fn ='test.out'
        os.remove(fn)
        print(f"Removed <{fn}>")
    except FileNotFoundError:
        print(f'Could not find <{fn}>')

def run_sh():
    '''Example running shell script'''
    subprocess.run(['bash', 'start.sh'])

schedule.every(8).seconds.do(make_file)
schedule.every(12).seconds.do(del_file)
while True:
    schedule.run_pending()
    time.sleep(1)
Output:
λ python remove_file.py Making <test.out> Removed <test.out> Making <test.out> Removed <test.out> Making <test.out> Making <test.out> Removed <test.out> Making <test.out>
Reply
#5
Hi

I tried the below but it isn't waiting for me. this is the contents of the .sh file i run

-----------
#!/bin/bash
# starts the test.py program

rm -f test.out
nohup /usr/bin/python test.py > test.out &
./watch.sh
-----------

So what i am looking for is the run the above for 30 seconds then pause then loop it and run the above again for 30 seconds and so on

(Jan-14-2020, 04:13 PM)snippsat Wrote: schedule work fine for this.
You could probably do all in Python,and drop the the shell script.
Here a example of making a file then delete,run_sh example on how to call shell script with subprocess.
import schedule
import time, os
import subprocess

def make_file():
    fn = 'test.out'
    open(fn, 'a').close()
    print(f'Making <{fn}>')

def del_file():
    try:
        fn ='test.out'
        os.remove(fn)
        print(f"Removed <{fn}>")
    except FileNotFoundError:
        print(f'Could not find <{fn}>')

def run_sh():
    '''Example running shell script'''
    subprocess.run(['bash', 'start.sh'])

schedule.every(8).seconds.do(make_file)
schedule.every(12).seconds.do(del_file)
while True:
    schedule.run_pending()
    time.sleep(1)
Output:
λ python remove_file.py Making <test.out> Removed <test.out> Making <test.out> Removed <test.out> Making <test.out> Making <test.out> Removed <test.out> Making <test.out>
Reply
#6
Something like this.
import schedule
import time, os
import subprocess

def run_interval():
    max_time = 30
    start_time = time.time()
    while (time.time() - start_time) < max_time:
        time.sleep(2)
        subprocess.Popen(['bash', 'start.sh'])
        #print('hello world')

schedule.every(30).seconds.do(run_interval)
while True:
    schedule.run_pending()
    time.sleep(1)
Reply
#7
On linux you just could install a cronjob (as user: crontab -e ), which executes the script for each 30 minutes.
You have to decide if you want to have a long/infinite running daemon, which is doing each 30 minutes something or you could let call by a cron service the script each 30 minutes.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Thank you for the replies, the above didn't work as i got an import error.

"
./s.sh: line 4: import: command not found
"

What im looking for is to putty into the server and start or stop the script myself so i start it then it runs for 30 seconds then waits then runs again for 30 seconds and so on until i stop the script.

Many thanks
Reply
#9
Error:
./s.sh: line 4: import: command not found
I guess you made the file executable and just run it.
Currently you try to run your code as Shell Script with the Bash.
The file extension is also misleading, because it's a Python Script and not a Shell Script.

Linux looks for the shebang, which starts with #!.
Everything after this two chars is a path to an interpreter.

We know it as:

#!/bin/bash
#!/bin/sh
#!/usr/python
#!/usr/python3
#!/usr/env python3
If the shebang is missing, he falls back to your default shell (Bash) and runs the script inside the default Shell.
Add the right shebang or call the file directly with python3.

python3 you_script.py
Do you want to start/stop the script with two different commands like start and stop?
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
Hi

thank you for the below, yes i want to run for example start.sh and it as above runs it for 30 seconds then pauases for 30 seconds and so on.

then stop.sh to stop it

Is this possible

thanks again

Paul


(Jan-16-2020, 01:18 PM)DeaD_EyE Wrote:
Error:
./s.sh: line 4: import: command not found
I guess you made the file executable and just run it.
Currently you try to run your code as Shell Script with the Bash.
The file extension is also misleading, because it's a Python Script and not a Shell Script.

Linux looks for the shebang, which starts with #!.
Everything after this two chars is a path to an interpreter.

We know it as:

#!/bin/bash
#!/bin/sh
#!/usr/python
#!/usr/python3
#!/usr/env python3
If the shebang is missing, he falls back to your default shell (Bash) and runs the script inside the default Shell.
Add the right shebang or call the file directly with python3.

python3 you_script.py
Do you want to start/stop the script with two different commands like start and stop?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to calculate time difference between each row of dataframe in seconds Mekala 1 357 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Looking for help on making a script [no idea where to start] Chietnemese 1 367 Jun-26-2020, 03:50 AM
Last Post: Larz60+
  How can I stop the script when I run the game on my own computer? StatTark 2 551 Jun-19-2020, 11:20 AM
Last Post: StatTark
  Need to add hours min and seconds tester_V 5 600 Jun-02-2020, 05:29 PM
Last Post: tester_V
  Can't substract seconds from time Raj_Kumar 1 414 Apr-15-2020, 02:47 AM
Last Post: bowlofred
  Use Python to start/stop a server service via a webform? oakleaf2001 0 416 Apr-04-2020, 06:14 AM
Last Post: oakleaf2001
  How to calculate time in seconds rajeshE 1 502 Feb-15-2020, 11:07 PM
Last Post: Larz60+
  start and stop omxlayer playback dezmob 2 944 Jan-27-2020, 09:43 AM
Last Post: dezmob
  Convert HH:MM:SS to seconds RavCOder 4 3,488 Nov-26-2019, 03:46 PM
Last Post: DeaD_EyE
  Multiple start of script dev1755 2 583 Sep-22-2019, 10:44 PM
Last Post: dev1755

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020