Python Forum
how to stop and start a script for 30 seconds - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to stop and start a script for 30 seconds (/thread-23726.html)



how to stop and start a script for 30 seconds - laspaul - Jan-14-2020

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


RE: how to stop and start a script for 30 seconds - DeaD_EyE - Jan-14-2020

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)



RE: how to stop and start a script for 30 seconds - laspaul - Jan-14-2020

Hi, thank you for the above.

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

many thanks

Paul


RE: how to stop and start a script for 30 seconds - snippsat - Jan-14-2020

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>



RE: how to stop and start a script for 30 seconds - laspaul - Jan-14-2020

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>



RE: how to stop and start a script for 30 seconds - snippsat - Jan-14-2020

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)



RE: how to stop and start a script for 30 seconds - DeaD_EyE - Jan-15-2020

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.


RE: how to stop and start a script for 30 seconds - laspaul - Jan-15-2020

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


RE: how to stop and start a script for 30 seconds - DeaD_EyE - Jan-16-2020

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?


RE: how to stop and start a script for 30 seconds - laspaul - Jan-16-2020

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?