Shell Script command timeout

264 views Asked by At

I am trying to run a command but if the command doesnt succeed in an amount of time (Lets say 30s for example), Cancel out the command and fail out. The script is fairly simple and I am just pausing ntpd and then forcing a time synchronization and starting. However if there is an invalid ntp server this hangs.

systemctl stop ntpd
ntpd -gq
systemctl start ntpd

My thoughts would be, I try to run this in the background, check every 10 seconds to see if the command is still attempting to be run. And say after 3 checks if i see "ntpd -gq" I kill the process and return a failure. Is this the correct way to do this or is there any built in time handling?

1

There are 1 answers

0
dbush On

The shell doesn't have this kind of time handling.

Running the process in the background and checking it periodically as you suggested is the best way to handle this.