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?

echo myPassword | sudo -S service simulatord start &&
nc -k -l 2214 & 
nc -k -l 3612 & 
ping -i 290 & 
ping -i 290 &

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


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

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

You must log in to answer this question.

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