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)
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.
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?
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
  Button to stop while loop from another script Absolutewind 5 819 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  ''.join and start:stop:step notation for lists ringgeest11 2 2,381 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Problem with module time and leap seconds Pedroski55 3 1,190 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  non-stop ping script kucingkembar 1 1,321 Aug-23-2022, 06:29 AM
Last Post: menator01
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,476 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  Store variable data and display sum after 60 seconds the_dude 11 3,368 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,533 Sep-27-2021, 05:40 PM
Last Post: rondon442
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,516 Jul-16-2020, 12:57 PM
Last Post: Larz60+
  Looking for help on making a script [no idea where to start] Chietnemese 1 1,708 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 2,193 Jun-19-2020, 11:20 AM
Last Post: StatTark

Forum Jump:

User Panel Messages

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