Bottom Page

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
Quote
#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!
Quote
#3
Hi, thank you for the above.

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

many thanks

Paul
Quote
#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>
Quote
#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>
Quote
#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)
Quote
#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!
Quote
#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
Quote
#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!
Quote
#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?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Convert HH:MM:SS to seconds RavCOder 4 293 Nov-26-2019, 03:46 PM
Last Post: DeaD_EyE
  Multiple start of script dev1755 2 210 Sep-22-2019, 10:44 PM
Last Post: dev1755
  pyjama - frame every x seconds? MuntyScruntfundle 2 360 Jan-18-2019, 05:18 PM
Last Post: MuntyScruntfundle
  What's the difference b/w assigning start=None and start=" " Madara 1 511 Aug-06-2018, 08:23 AM
Last Post: buran
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 667 Jul-28-2018, 10:19 AM
Last Post: Lyperion
  win10 Service(script) start and stop hwa_rang098tkd 0 660 Jun-21-2018, 07:42 PM
Last Post: hwa_rang098tkd
  Conversion of Time Duration in seconds in python jdevansh99 0 667 Jun-05-2018, 05:12 PM
Last Post: jdevansh99
  Using Subprocess.Popen to start another python script running in background on Window johnb546 0 7,875 Jun-01-2018, 01:57 PM
Last Post: johnb546
  check process status and start if it stop linux inspirer 4 3,884 May-17-2018, 02:52 AM
Last Post: inspirer
  starting a process every 10 seconds Skaperen 7 8,973 Dec-31-2017, 03:47 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)