Score:0

Kill the processes if it running for more than one hour bash script

mp flag

i have this bash script that run python program in ubuntu server every 5 monites if not alredy running , i want to make it kill program if it running more tha one hour and re run it any help

#!/bin/bash

if pgrep -f "/home/user/crawler/panel/crawler/scans.py"
then
    echo "script running"
    # Command when the script is runnung
else
    echo "script not running"
    /home/user/crawler/env/bin/python /home/user/crawler/panel/crawler/scans.py
fi
diya avatar
la flag
Use the `pgrep` command you already have to determine the Process ID and then use that PID with something like https://unix.stackexchange.com/questions/7870 to determine how long that PID has been running and `kill` that PID if it has been running too long.
urek mazino avatar
mp flag
i tried this , but no output showing ps -eo pid,comm,cmd,start,etime | grep -i "/home/user/crawler/panel/crawler/scans.py" @diya
pt flag
You could run the python script under control of the `timeout` command, so that it gets killed if it runs for more than an hour.
urek mazino avatar
mp flag
@larsks that perfect solution for me , can u re write your commant as answer so i can mark it as answered
Score:2
pt flag

Rather than using pgrep to find the process, a simpler solution may be to run the Python script under the control of the timeout command (which is part of GNU coreutils). You can then limit the maximum runtime of your Python script to 1 hour:

timeout 1h /home/user/crawler/panel/crawler/scans.py
urek mazino avatar
mp flag
thanks that the perfect solution
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.