I notice that alot of casual unix/linux users simply underestimate the power of the command line for getting things done. Depending on the disto of linux, or BSD you’re using, you may or may not have these already, but you’ll almost certainly be able to install them.
First some context now, to explain why I started doing this. I’m an avid shell user. I think that it has powerful tools that allow you to bring a measure of efficiency to your work that you simply can’t match with other tools. One of my colleagues at Coradiant isn’t a linux user… yet. He’s stuck working next to me though, and that’s slowly changing. He’s got a ton of knowledge on internet technologies, he just was comfortable working with GUI environments, and never had a need to go to linux until recently, when he started experimenting with virtualization.
Screen
Any hardcore IRC’er knows about this one – in conjunction with their irc client of choice. screen is very useful for being able to keep a shell “active” even after you disconnect. This allows you to start up something (like building/installing sources, which can be lengthy sometimes) and go back to it later. For those on shoddy connections, this also prevents disconnections from having to restart whatever you were doing. I’m not going to go into detail, my goal is only to point out what I’ve seen many overlook, but here’s the absolute basics:
- start a new shell by just typing screen
- You can disconnect from it without terminating by hitting CTRL+A then D
- To return to it, you can just type screen -r. Additionally, the -d switch allows you to attach to an active screen at a new location, detaching it from the location where it was running.
- You can also use screen -x so that you can attach to a screen that is active elsewhere, without disconnecting it.
Script
Script is another neat command, which is useful for capturing the output to std out of a terminal. Specifically, it’s usefulness is in being abl ee to run script together with a command… like make install for instance, so that if you absolutely need to, you can go through all of it and find relevant data if something fails. Sure, from windows, you can use Putty and save it to your PC, but then you lose the ability to parse through it looking for details the way you can with the cli (for instance grep) not to mention save files by commands. To use script:
- run script file.log command where file.log is the file that all is sent to, and command is the command you want to run. It can even be complex, although depending on what you’re looking to run, you may want to capture it in single or double quotes.
Backgrounding and Foregrounding processes
When you execute a command that will take a while, you can append & to the very end of it, and the command will run in the background. You can also suspend it instead after executing it using CTRL+Z. You can then activate it as a background process by typing bg. Typing fg will return it to foreground. Lastly, you can use the command jobs to see what jobs are in the background. If you have multiple jobs, they’ll be numbered, and you can reference the specific jobs by those numbers when using fg or bg: ‘fg 4‘ for instance would foreground the suspended or backgrounded process #4 inthe list of jobs.
nohup
This is a useful command to. Really, there isn’t much to it, but what it does is significant. First of all, this is something you prepend to a command you want to run. It also will log anything to stdout to nohup.out in the working directory. When you run something with nohup, and you put it into the background, you can then close your terminal and walk away, knowing that closing the terminal did not kill any background processes… they will continue to run on your server, effectively daemonized.
Conclusion
These are just a few to help you make your CLI use a more empowering experience. Sure, the CLI isn’t pretty to look at (ok, I find it cool and pretty in it’s own right), but it can give you control and flexibility many don’t realize. Let’s put some of these together:
What if you are running Virtualbox and want to fire up a VM without having to take up the extra headroom that the GUI uses? well, you can run nohup VBoxHeadless -s myvmname &. This launches the VM, and puts it into the background so you can close the terminal and not worry about killing it. You can also use screen instead so that you can close the terminal, but go back to it if ever you need to for any reason. Here’s an example if you wanted to run make buildworld in /usr/src on FreeBSD: screen script rebuild.log make buildworld. Perhaps you want to backup some crucial files using rsync, and you know it’ll take hours to complete, not to mention you don’t want to miss error messages that scroll through the buffer. You can achieve this by typing script mybackup.log rsync -av /my/backup destination.host:/backups/ &.
1 ping
Using Your CLI Effectively (Part II) | obsecured.net
January 14, 2011 at 11:42 (UTC -4) Link to this comment
[...] Part I, I covered some basics that help with working on the CLI and enabling you to keep things going [...]