executing bash loop while command is running

前端 未结 4 1097
情歌与酒
情歌与酒 2020-12-12 20:05

I want to build a bash script that executes a command and in the meanwhile performs other stuff, with the possibility of killing the command if the script is killed. Say, ex

4条回答
  •  遥遥无期
    2020-12-12 20:12

    here is the simplest way to do that using ps -p :

    [command_1_to_execute] &
    pid=$!
    
    while ps -p $pid &>/dev/null; do
        [command_2_to_be_executed meanwhile command_1 is running]
        sleep 10 
    done
    

    This will run every 10 seconds the command_2 if the command_1 is still running in background .

    hope this will help you :)

提交回复
热议问题