Six-module walkthrough covering navigation, files, reading/searching, processes/editors, scripting, and advanced tools (ssh, regex, tar, etc.). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
180 lines
7 KiB
Markdown
180 lines
7 KiB
Markdown
# CLI Part IV: Processes and Editors
|
|
|
|
**CHEG 667-013 — Chemical Engineering with Computers**
|
|
Department of Chemical and Biomolecular Engineering, University of Delaware
|
|
|
|
---
|
|
|
|
## Key idea
|
|
|
|
Monitor and control running processes, edit files in the terminal, and use control characters and job control.
|
|
|
|
## Key goals
|
|
|
|
- Check system information with `who`, `ps`, `top`, and `uptime`
|
|
- Kill runaway processes
|
|
- Edit files with `vi`, `nano`, or `emacs`
|
|
- Use control characters (`^C`, `^Z`, `^D`) and job control (`fg`, `jobs`, `&`)
|
|
|
|
---
|
|
|
|
We continue on our quest to learn about the command line. There is a lot to learn and the commands are somewhat cryptic. The best way to get comfortable with the command line is to use it. Look around the system. You can't really break anything. You generally don't have the permissions to edit, move, or delete system files.
|
|
|
|
That said, remember: there is no undo when it comes to your own files. A good practice is to make backup copies of important files!
|
|
|
|
|
|
## 1. System information and managing processes
|
|
|
|
There are several useful programs for seeing who or what is running on a machine.
|
|
|
|
- `who` — display who is on the system
|
|
- `w` — display who is on the system and what they are doing
|
|
- `whoami` — display the current user ID
|
|
- `ps` — process status — information about processes on the system
|
|
- `top` — display sorted information about processes (like activity monitor)
|
|
- `uptime` — show how long system has been running
|
|
|
|
`ps` returns important information like the *process id*, a unique number assigned to every running process. The command has a few options, and a somewhat strange syntax. Most systems will interpret `ps aux` as a command that lists all running processes on a system.
|
|
|
|
> **Exercise 1:** Try out each of the commands above!
|
|
|
|
### Killing a process
|
|
|
|
If you have a process that seems to be hanging, like a program with a buggy infinite loop (maybe `top` shows a lot of CPU use and the fan is spinning loudly), you can terminate it with the `kill` command. You first have to find the process id using `ps`. Then type:
|
|
|
|
```
|
|
$ kill pid
|
|
```
|
|
|
|
|
|
## 2. Editing files in the terminal
|
|
|
|
Most of the time, we can use an editor or development environment to write code. However, sometimes we need to write or edit a file in the terminal, including configuration files. There are a few options (some people have [strong feelings about editors](https://en.wikipedia.org/wiki/Editor_war)):
|
|
|
|
- `vi` — "a programmers text editor"
|
|
- `nano` — a "small and friendly editor"
|
|
- `emacs` — Emacs is more than an editor.
|
|
- `ed` — line editor for Yoda-level Jedi editing.
|
|
|
|
> **Exercise 2:** Using one of the editors above, type in the following short C program:
|
|
>
|
|
> ```c
|
|
> #include <stdio.h>
|
|
> int main() {
|
|
> printf("hello, world");
|
|
> }
|
|
> ```
|
|
>
|
|
> Save this file as `hello.c`.
|
|
|
|
There is a bit of a learning curve for each of these editors, but they can be quite useful. Most users will choose `vi` or `nano`. The "standard editor" `ed` is found on almost every machine and can be a good fallback if you need to repair a system or make a quick edit.
|
|
|
|
|
|
### C programming side quest
|
|
|
|
Use the C compiler to create an executable program from our `hello.c` code:
|
|
|
|
```
|
|
$ cc hello.c
|
|
$ ls
|
|
a.out* hello.c
|
|
```
|
|
|
|
Now we have an executable file `a.out`. Run it by typing `./a.out`. A right of passage!
|
|
|
|
|
|
## 3. Controlling the terminal output
|
|
|
|
Several control characters are used to control the terminal and its output:
|
|
|
|
- `^C` — stop execution (halt a program or clear a terminal line)
|
|
- `^S` — pause output
|
|
- `^Q` — continue output
|
|
- `^D` — end of transmission / end of file
|
|
|
|
Here, the caret character `^` stands for `control`. You might use control-C the most.
|
|
|
|
|
|
## 4. Suspending and backgrounding processes
|
|
|
|
Sometimes you need to pause what you're doing (like editing a file) to perform another task. You can have multiple terminal windows (or tabs) open, but if you're working on a remote machine, it may be inconvenient to open multiple sessions. Suspending (pausing) execution is one option. In other cases, a program may take a while to run, and its output is written to files, not the terminal. In that case, running it in the background is a good option.
|
|
|
|
- `^Z` — suspend an active process
|
|
- `fg` — foreground a suspended or backgrounded process
|
|
- `jobs` — display status of jobs in the current session (not all shells)
|
|
- `&` — used after a command, this runs a process in the background
|
|
|
|
Type `cat` and `return`. Then type `^Z`.
|
|
|
|
```
|
|
$ cat
|
|
^Z
|
|
[1]+ Stopped cat
|
|
```
|
|
|
|
The `cat` command was reading from the standard input (the keyboard). When we typed control-Z, it suspended the process. Type `ps` and you should still see it listed as an active process. We can reactivate it using the command `fg`:
|
|
|
|
```
|
|
$ fg
|
|
cat
|
|
```
|
|
|
|
It tells us that the `cat` command is active again. (Nothing much will happen. Try typing a few lines. What do you see and why?)
|
|
|
|
Control-Z is useful when you are editing a file and need to return to the command line (although some editors have the ability to open a new shell). Use `vi` or `nano` to open your `hello.c` file. Then hit `control-Z`:
|
|
|
|
```
|
|
$ vi hello.c
|
|
|
|
[1]+ Stopped vi hello.c
|
|
$
|
|
```
|
|
|
|
Now do some other work. Compile the program:
|
|
|
|
```
|
|
$ cc hello.c
|
|
```
|
|
|
|
You should see a new file called `a.out`:
|
|
|
|
```
|
|
$ ls -l
|
|
total 20
|
|
-rwxrwxr-x 1 furst furst 15960 Mar 31 21:41 a.out*
|
|
-rw-rw-r-- 1 furst furst 65 Mar 31 21:41 hello.c
|
|
```
|
|
|
|
This is the executable or binary file compiled from our short C program. Run it by typing `./a.out`.
|
|
|
|
Now return to the editor. Type:
|
|
|
|
```
|
|
$ fg
|
|
vi hello.c
|
|
```
|
|
|
|
(This will put you back in the editor. You probably won't see these lines until you finally quit the editor, unless you're using `ed`.)
|
|
|
|
**Warning!** Any process running in the background will be terminated if you close the terminal session. There are a few tools to keep processes running after you disconnect:
|
|
|
|
- `nohup` — run a command immune to hangups. Prefix any command with `nohup` and it will keep running after the terminal closes. Output goes to `nohup.out` by default:
|
|
|
|
```
|
|
$ nohup ./long_simulation &
|
|
```
|
|
|
|
- `screen` — a terminal multiplexer that lets you create sessions you can detach from and reattach to later. Start a session with `screen`, run your work, then detach with `Ctrl-A d`. Reattach later with `screen -r`.
|
|
|
|
- `tmux` — a more modern terminal multiplexer with similar goals to `screen` but better defaults, window splitting, and scripting. Start a session with `tmux`, detach with `Ctrl-B d`, and reattach with `tmux attach`. `tmux` also makes it easy to split your terminal into panes:
|
|
|
|
```
|
|
$ tmux # start a new session
|
|
Ctrl-B % # split vertically
|
|
Ctrl-B " # split horizontally
|
|
Ctrl-B arrow-key # move between panes
|
|
Ctrl-B d # detach (session keeps running)
|
|
$ tmux attach # reattach later
|
|
```
|
|
|
|
Both `screen` and `tmux` are especially useful when working on a remote machine over SSH — if your connection drops, the session survives and you can reconnect to it.
|