pinging works well, use this command line:
if ping -W 5 -c 1 google.com >/dev/null; then
echo "Internet is up."
fi
This uses the following command line arguments to ping:
- -W 5: Time out waiting for a response after five seconds.
- -c 1: Only send one ping request. Default of most ping implementation is to send unlimited packages.
- Also the output of ping is redirected to /dev/null to avoid useless mails from cron.
As for your sudo question, you can configure that in the sudoers file.
Use the following line in sudo:
fromuser ALL=(root) NOPASSWD: /usr/bin/mycommand
You'll need to replace the following strings:
- fromuser: Replace with the user who runs the cronjob
- /usr/bin/mycommand: Replace with the path of the command you want to execute
With this configuration the user "fromuser" will always be able to execute /usr/bin/mycommand without entering a password. You'll want to be careful with that of course for security reasons.