Layla
Layla

Reputation: 37

Killing infinite python script from shell

Hi i have 5 infinite python script.I want run first script and after 1 hours kill first script and after this run second infinite script and after 1 hours kill second script. Its my codes what is a problem ? how i can ? Please dont recomend me giveng time to python script . I can do id , but its not useful for my project . Only need killing infinite scripts from shell . Thank you very much

ITS MY SHELL SCRIPT

#!/bin/bash

START=`date +%s`
while true
do
    if [ $(( $(date +%s) - 10 )) -lt $START ]; then
        python infinite.py
    else
        pkill python
        break
    fi
done

START=`date +%s`
while true
do
    if [ $(( $(date +%s) - 10 )) -lt $START ]; then
        python infinite.py
    else
        sudo pkill python
        break
    fi
done

START=`date +%s`
while true
do
    if [ $(( $(date +%s) - 10 )) -lt $START ]; then
        python infinite.py
    else
        pkill python
        break
    fi
done

ITS MY PYTHON SCRIPT

x = 0
while True:
    print("Hello, World ! " + str(x))
    x +=1

Upvotes: 1

Views: 130

Answers (2)

LMC
LMC

Reputation: 12662

This code will run your script in the background during an hour and then kill the process

START=$(date '+%s')
python infinite.py &
while true
do
    if [ $(( $(date '+%s') - $START )) -lt 3600 ]; then
        echo "Still running"
    else
        pkill -f 'infinite.py'
        break
    fi
done

Upvotes: 0

that other guy
that other guy

Reputation: 123460

GNU coreutils comes with a timeout tool.

To run five scripts, infinite1.py to infinite5.py, for one hour each in sequence:

for script in infinite_{1..5}.py
do
   timeout 1h python "$script"
done

Upvotes: 1

Related Questions