0

For a student environment, I have a simple bash script that runs upon boot up on Ubuntu 18.04 (@reboot in crontab -e).

It runs ok (java file starts, netcat starts, pings, and all PIDs are viewable via "ps -aux"), but I would like to make this more "bullet proof", if possible.

Is there anything I can put in this Bash script to monitor the PIDs that are created and then dynamically generate them again if they stop running or are killed?

#!/bin/bash
echo myPassword | sudo -S service simulatord start &&
nc -k -l 2214 & 
nc -k -l 3612 & 
ping -i 290 192.168.0.101 & 
ping -i 290 10.10.3.254 &

This scripts starts a simulation program (simulatord) and then runs the following commands in parallel in the background.

Ideally I can put some logic in this Bash script that would allow me to "kill -9" the ping or nc PID and then have it respawn, but I'm unsure if this is doable in Bash or if I need a different tool (Python script?)

1 Answer 1

0

I can imagine using a function to run each program in a loop until it sees a stop condition. Suppose you use the existence of /var/run/stopcmd as the stop condition. Then it might look like this:

function run {
    while ! -e /var/run/stopcmd; do
        "$@"
    done
}

run nc -k -l 2214 & 
run nc -k -l 3612 & 
run ping -i 290 192.168.0.101 & 
run ping -i 290 10.10.3.254 &

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .