Uncategorized

Useful MacOS commands

Today I discovered some useful Mac commands so I want to document them down here.

Turning off Mac display without it going to sleep

Turn off the Mac display without getting it to sleep. There might be keys but I found this little command pretty handy.

pmset displaysleepnow

This will turn off the display immediately but not get Mac to sleep so it can still run processes if needed.

This command is normally handy with the following command which keeps the computer running forever. Basically do not sleep.

Keeping Mac running sleeplessly

caffeinate -disu

The above command will keep Mac awake without going into sleep so you can run long running processes.

Repeating a command for X number of times

Sometimes I want to run a script and retry X number of times. This can be done with a little function. Add this to the .bashrc

repeat() {
  number=$1
  shift
  for i in `seq $number`; do
    $@
  done
}

Save the file. In the terminal,

source ~/.bashrc

Then I can do

repeat 100 echo 'Foobar'

This will print ‘Foobar’ 100 times in the terminal. I can replace the command with any commands.

Watch command

watch is a command-line tool, part of the Linux procps and procps-ng packages, that runs the specified command repeatedly and displays the results on standard output so you can watch it change over time. By default, the command is run every two seconds, although this is adjustable with the -n secs argument.

wikipedia

In Mac, this can be installed from brew,

brew install watch

Then we can do,

watch "mysql -h 127.0.0.1 -uroot -ppassword -Ddbname -e 'select count(1) from a_db_table'"

This will query the db every 2 secs and updates the total count.

Done.

Leave a comment