You can query whether a process is running by name. You can do this with ps and pipe the output to grep. It’s not hard, but you can do this more quickly with pgrep. You can also kill that process with pkill. Which includes the ability to send a signal. So, let’s look at closing down iTunes with pkill: pkill iTunes Or we can send it with a signal (9): pkill -9 iTunes Or you could just grab the pid of a process by name: pgrep Safari It might display: 797 And that’s it. Easy Peasy.
-
-
Hide Safari’s Bookmarks Bar
Safari has a bookmarks bar. Some people want to hide it. A lot of people used to do stuff like this by modifying the default user template in OS X. Not something we’ll be doing much in the future. So to do so with a script: defaults write com.apple.Safari ShowFavoritesBar -bool false To turn it back on: defaults write com.apple.Safari ShowFavoritesBar -bool true
-
Removing Those Thousands Of Empty Directories In Bash
Earlier, we looked at creating thousands of empty directories. Today, we’re going to get rid of them. But we need to get rid of only empty directories. To do so, we’ll use the find command: find . -depth -type d -empty -exec rmdir {} \; Now, we can put both into a script: mkdir $(printf '%05d\n' {1..10000}) find . -depth -type d -empty -exec rmdir {} \;
-
Using Inputs in Bash Scripts
You can easily accept user provided input in bash by using the read command in bash for Linux and OS X. Here, we’ll echo out a choice to a user in a script, read the output into a variable called yn and then echo out the response: echo "Please enter y or n: " read yn echo "You chose wrong: $yn" Here, we used echo to simply write out what was chosen in the input. But we could also take this a little further and leverage a case statement to then run an action based on the choice selected: read -p "Should the file extension change warning be disabled (y/n)?…
-
Disable Autocorrect In OS X Automated Workflows
I mess computers up a lot. And that means I have to reload operating systems a lot. I’ve also been having terrible issues caused by autocorrect. So… Let’s disable it. By sending the NSAutomaticSpellingCorrectionEnabled key as a false boolean into NSGlobalDomain: defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false
-
Grab Your WAN IP In Scripts
Sometimes when I’m writing a script, I need something to phone home to something in the script. For example, this can tell another daemon where to ssh into when I invoke it remotely. So, let’s say I want to grab my WAN address in a script. I can use curl with a number of 3rd party sites (sites that often change. But, one that we can use here is ipecho.net. Here, we’ll look at their plain output page here: curl ipecho.net/plain This can then get output into a variable or file for processing in other parts of a script. For example, the output here is basically the same thing but…
-
Some Command Line java Debugging Options
There’s an excellent tool that can be used to grab a heap dump from a Java process. It’s called jmap. To do so, run the jmap command, followed by a format and a file path as the format and file operators. Also, provide the PID, as follows: jmap -dump:format=b,file=~/memdump.hprof 80446 Once dumped, you can view the dump file in the Memory Analyzer Tool (MAP) and find objects that use use too much memory and/or have memory leaks, as part of your troubleshooting. You can also replace the pid with a name of an executable or a core. Run the map tool along with a -h option for a help summary.…
-
Caffeinate Your Commands
The caffeinate command is pretty cool. It keeps your computer from going to sleep. It can run in a couple of different ways. There’s a timer that prevents sleep for a little while. You can also run another command from within caffeinate that keeps the system awake until the other command is finished. Here, we’ll scp a file called source file to a host called servername and keep the system from going to sleep until the process is finished: caffeinate -s scp sourcefile me:servername/targetfile Here, we’ll just use the boring command to tell the computer not to go to sleep for an hour: caffeinate -t 3600 &
-
Steve Jobs, The Movie
-
Using My Mac As An Alarm Clock
The at command can be used to schedule jobs to be run at certain times. I have a hard time getting up in the morning. Here, we’re going to echo a command that we want to be run at a certain time. In this case, we’re going to open a song to make into our alarm clock: echo 'open ~/Desktop/bangbang.m4v 2>/dev/null' | at 07:00 tomorrow The job will then output. You can see jobs waiting to be run, along with when they’ll be run using the at command with the -l option: at -l In this case, the job is 2. You can then remove a job using the atrm command: atrm job…