Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for Network Interface Before Executing Command

I have a couple ideas on how I would achieve this. Not sure how I would script it.

Method 1: (probably the better choice)

Create a loop that pings a server until reply is received then execute command if no reply is received in X amount of time/runs continue script.

Method 2:

Check if network interface has valid IP then continue script

How would one go about adding this functionality in a script. Would awk or grep be of use in a situation like this? Thank you in advance for ANY input.

like image 753
Geofferey Avatar asked Mar 09 '14 03:03

Geofferey


People also ask

How do I make terminal wait for input?

Sometimes we want to get some inputs from the user through the console. We can use input() function to achieve this. In this case, the program will wait indefinitely for the user input. Once the user provides the input data and presses the enter key, the program will start executing the next statements.

What is wait $!?

$! is an internal Bash variable that stores the PID of the last job run in the background. In this example, that is the PID of the sleep command. We're storing the PID in a variable ( process_id ). Prints the PID number. The PID is passed to the wait command that waits until the sleep command completes.

Is there a wait command?

wait is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. When multiple processes are running in the shell then only the process id of the last command will be known by the current shell.

Is there a wait command in bash?

The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.


2 Answers

You can do the following

until ifconfig -l | grep ppp0 >/dev/null 2>&1; do :; done

Works on MacOS

like image 81
abd3lraouf Avatar answered Oct 05 '22 01:10

abd3lraouf


This command should wait until it can contact google or it has tried 50 times:

for i in {1..50}; do ping -c1 www.google.com &> /dev/null && break; done

The for i in {1..50} loops 50 times or until a break is executed. The ping -c1 www.google.com sends 1 ping packet to google, and &> /dev/null redirects all the output to null, so nothing is outputed. && break executes break only if the previous command finished successfully, so the loop will end when ping is successful.

like image 27
PlasmaPower Avatar answered Oct 05 '22 01:10

PlasmaPower