Ubuntu – Cancel and restart a certain Routine (python-script)

bashcommand linecronpython

I have a question regarding a programm I run. Every once in a while, I need to restart a certain program.

For that case, I usually run:

screen ./run.sh arg1 arg2 "arg3"
(screen, since I don't know if there is another way of putting something in the background – but that's another thing.)

So, I think I could add a cronjob which (via crontab -e) which runs this program (do I need a bash-script for that though?) – But I don't know how to stop the process. Right now I'm cancelling it via CTRL + C after I re-attached the detached session.

I was thinking of killing it, but I don't know the process-id when I start program. Can someone help me with that? I'm using Ubuntu 12.04.


start the process

wait 6 mins

stop the process,

(re)start the process (…)


Best Answer

If you are not opposing bash solutions, here's a script that does what you outlined. It can be added to /etc/rc.local to run on every boot. Just call it like bash /path/to/script & from within /etc/rc.local


while true
    screen ./run.sh arg1 arg2 "arg3" & # start in background
    CMDPID=$! # get pid of that command
    TIME=$( date +%s  ) # get timestamp

    # next while loop just keeps checking time
    # We don't want to block up CPU with 
    # continuous sleep command
    while [ $(date +%s) -lt $(($TIME+360)) ];
      sleep 0.25
    kill -TERM $CMDPID # kill that process

    # return to the top and repeat the procedure