I am trying to do a shell script to curl and change my IPAddress at FreeDNS. The Curl is currently working but my internet is not so fast sometimes and the curl doesn’t finish and my script doesn’t continue. Is there a way that I can check to see if my curl script was finished and put in a repeat loop instead of using a delay.
Thanks for any help
Model: Mac Pro
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)
yes, that’s possible:
“ create an process that’s executing curl command
“ put that process in the background
“ continue when that process is finished, otherwise return a timeout and kill the process (after a minute or so)
Creating a process:
Using curl in an do shell script already invokes a process. So you don’t have to do anything special here.
Putting a process in the background:
Adding an ampersand at the end of the line in bash will put the process in the background and your script won’t wait till the command is finished. So we need to keep track of that process by using it’s process id. You can get the process id of the previous background process by echoing ‘$!’.
Repeat until the process is finished:
You can use a repeat loop with an interval of 1 or 2 seconds that checks if the process is still running by looking up running process ids. When the process isn’t in the list of running process ids, you will exit the repeat loop. The repeat loop itself have also a maximum number of repeats, when the interval is 1 seconds you can say it will only repeat 30 times and after that it will return an timeout error.
In theory it would be something like this (out of my head, not tested the code):
set pid to do shell script "mycommand >/dev/null &
repeat with x from 1 to 31
if x = 31 then
--return a timeout error here and kill the process (it's hanging)
do shell script "kill -9 " & pid
error "Network Timeout" number 2 --choose a number to your likings
if not (do shell script "ps -A -o pid | grep -o '[^0-9]" & pid & "$' > /dev/null && echo yes || echo no") as boolean then exit repeat
edit: Forgot to add the timeout handling
DJ Bazzie Wazzie
The shell script curl that i am using was in a snippet that i got on the URL. I am not that familiar with shell scripting and i am not sure how to include this script with the suggestion of yours. This is the shell script that I am using.
do shell script "/usr/bin/curl http://freedns.afraid.org/dynamic/update.php?***36 digit secret code goes here***"
Could you please show me where my code would be located. Thanks for your help.
your command (/usr/bin/curl http://freedns.afraid.org/dynamic/update.php?***36 digit secret code goes here***) should replace the word “mycommand” on the first line. The rest should all be the same, the “> /dev/null” is important so the “echo $!” will return the right information. FYI, the “> /dev/null” redirects the output to some sort of waste basket.
DJ Bazzie Wazzie
I think that will fix my problem. So far it is working like it should.
Thanks for all your Help.