UNIX CODA

First, let’s review what ps from yesterday does:

ps will show you the running processes on your hard drive. If you run it “naked,” (no flags), it will show you only your own user processes. To show all the system process also, run ps x

ps aux |grep unix

Here you’ll want to actually run

ps aux |grep spring

You’ll want to use ps with |grep spring with Rails 6, but if you boot with Puma (previous versions of Rails), you can |grep rails.

This reveals something important: the unix process id number. The number to the leftmost of your ps output is your process id number.

the unix process id is displayed

When you need to forcibly kill a hanging process, you this is how you find its Unix process id number.

With that brief review, let’s move on to today’s 5 Unix tips & tricks.

6) kill and kill -9

This is how you “kill” a process in Unix. You pass the process id number that you found through ps to the kill command. Note that kill is different than terminate— and that’s an important distinction— because kills just “tells” the process to shut down. In other words, it just asks kindly.

Terminate, on the other hand, actually tells the OS to terminate the process. That’s important when debugging because very often your process might be hanging, and when it is hanging, you can’t kill it (you must terminate it). To terminate, you use kill -9, like so

kill -9 73609

7) Ctl-Z — Background a unix process

Ctl-Z (or Control-Z on your keyboard) is “background” which means “send the process to the background.” It frees up your shell prompt for more commands but keeps the process running. When you hit Ctl-Z, Rails says this slightly confusing message:

When Rails says 'stopped' it really means 'backgrounded'
What happens when you CTL-Z a running Rails app

What’s slightly confusing is that the app isn’t actually stopped at all, it is now just running in the background. (See #9)

8) Ctl-C — Stop a unix process

Although the Background keystroke (#7) tells you “Stopped,” it is, in fact, Ctl-C that is the correct way to exit (also known as “stop” or “terminate”) out of a Rails app. When Rails boots, it tells you this in fact:

Booting puma Rails loading default behavior showing puma booting
Rails default boot

Ctl-C will send the terminate signal to the app and it will shut down with:

Rails Exiting
Rails shutdown with Ctl-C

#9) fgforeground

If you hit the Ctl-Z key (see #7), your process went into “the background.” What does that mean, exactly? It means your process is still running, with a process ID number that can be examined with ps (see #5) and seen running in the background, on one of many threads, operating on your computer system.

To get it back, that is, to go back to the state where your shell prompt had the Rails app running, use fg, or foreground.

Essentially, your shell will keep a stack (first-in-last-out) of processes which have been backgrounded. You use fg to move them back to the foreground of your shell prompt (for example, if you want to exit them without using kill.)

#10) Tab — Auto-complete

On your Bash prompt while you navigate the file system, hit tab once. It will “type for you” — automatically matching what it thinks you want—  until it finds two files with the same name. Then it will go as far as it can completing only up until the letters provide a match that it can distinguish. If such a unique matching file name exists in the file system, it will automatically complete the matching filename for you and put a space.

If, however, it finds matching two or more matching file names like what you’ve typed, it will complete up to the point where it can distinguish between the filename themselves. That is, where they differ reading left-to-right. Then it will beep at you alerting you that it could not autocomplete.

If it can complete your file typing simply by what you entered, it will so so. In case it cannot, you then press tab again (a second time), which brings us to…

#11) Tab-tab — Auto-suggest BONUS!

Bash comes with a special bonus: Tab-tab. That’s when you hit tab twice, your shell prompt will show you a list of suggested completions and then return you to the same state ready to continue taking your input.
Remember, this might work in conjunction with #10 above, because your first tab may have completed the input to the point where your bash could make the completion. If there’s more than one option, bash will beep at you to let you know it couldn’t fully make your completion. Hit tab again, and you will see a list of matching filenames that are criteria for your completion.