How to use “watch” on Mac OSX

In case you are looking for a “watch” command, similar to Linux, for your Mac OSX platform. You can create a shell script which can be used to emulate the similar requirement of what a watch command does on Linux.

Create a shell script named “watch” on your path “~username/bin/”, with the following content:

#!/bin/sh

if [ -z "$2" ]; then
echo "Usage: $0 seconds command" >&2
exit 1
fi

seconds=$1
shift 1

while sleep $seconds; do
clear
echo "Press ctrl+c to quit\n";
$*
done

Once this file is created, provide execute grants.
shell> chmod +x ~username/bin/watch

Try running a sample command as given below.
shell>watch 3 "ls -lF $HOME"

See the results reload every 3 seconds in this example. This is a work around for the linux watch command. Notice the delay before running the command too.
In case you are unable to run the watch command as explained above, kindly refer the below steps to add the “watch” command to your self executable bin path.

Once you added your custom created binaries/shell scripts to your “~username/bin” path, you need to add them to the $PATH to make them run as an executable command. Check whether “~username/bin/” is already in the $PATH
shell> echo $PATH

If not in the $PATH, add the ~username/bin path to $PATH using the below command.
shell> export PATH="$PATH:/Users/username/bin/"

Once added, verify it again.
shell> echo $PATH

If you want this command added everytime you login. Add the command on the .bashrc or .bash_profile file under the user home directory.
Add this command in the .bash_profile or .bashrc file
export PATH="$PATH:/Users/username/bin/"
Now your shell script can run as a built-in command on shell.

How to clear DNS cache in Mac OSX

Every outgoing DNS request is cached for optimized performance. Its good for faster lookups of the same host but sometimes we will want to clear the cache if the host name no longer points to the earlier IPAddress. To clear the DNS cache in Mac OSX we need to use the “Terminal” application. Type “terminal” in spotlight and open the application “Terminal”.

When you are on the terminal, it shows a user shell. Enter this command.
shell> dscacheutil -flushcache

And after the command is executed. Try visiting the website through any browser to see if the website now loads from the valid IP Address.

How to ensure mac osx time machine is creating restorable backups

While using a Mac OSX, make sure you have a spare external hard drive which is used to take Time Machine backups. Well, if you landed on this page, you definitely know how to take Time Machine backups, however – the concern which strikes most of the people is whether you can make a restorable backup using Time Machine.

By default, Time Machine makes full system backups, unless changes in the system settings might keep it from taking backups of system files/folders. Here I show you how to confirm if the system files/folders are being backed up while you run a Time Machine backup process.

Time Machine is Apple’s built in application to backup the state of Operating system and user files/documents. This application of all OSX versions ensures that you have hourly for current day, daily backups for past few days and weekly backups over a period of time. Beyond which it retains monthly backup. Also if the external disk runs out of space for backup, Time Machine shall delete the oldest backup retained on the Backup drive.

By default, all the files on the system are backed up. However, you can confirm if system files are being backed up or not.

sudo defaults read /Library/Preferences/com.apple.TimeMachine SkipSystemFiles

Result of this command should be “0” or “1”.

If it shows “0”, its an assurance that system files are being backed up.
If the result is “1”, it means your system files are avoided from the time machine backups.

Running the following command shall allow you to make this system files backup started
sudo defaults write /Library/Preferences/com.apple.TimeMachine SkipSystemFiles false

After this setting is modified, consider that Time Machine’s next execution will now include your system files and may take longer than regular backup process.