Command Line Control#
Terminate Processes#
SIGINT:Ctrl-CSIGQUIT:Ctrl-\SIGTERM:kill -TERM <PID>
#!/usr/bin/env python
import signal, time
def handler(signum, time):
print("\nI got a SIGINT, but I am not stopping")
signal.signal(signal.SIGINT, handler)
i = 0
while True:
time.sleep(.1)
print("\r{}".format(i), end="")
i += 1
Pause and Run Processes in the Background#
SIGSTOP:Ctrl-Zfg: Resume running in the foregroundbg: Resume running in the backgroundjobs: List all unfinished tasks in the current terminal sessionpidreferences the task,pgrepfinds the pid%+task numberselects the task,jobsprints the task number$!the most recent task
&: Command suffix to run directly in the backgroundnohup: Wrapper that ignoresSIGHUP, closes the terminal, and continues running the program in the backgrounddisown: For already running programs, closing the terminal allows them to continue running
Terminal Multiplexing#
tmux#
Split the terminal window into multiple panels and tabs, allowing interaction with multiple shell sessions simultaneously, and detach and reattach the current terminal session in the future.
Session-Workspace, containing one or more windows#
tmuxstart a new sessiontmux new -s NAMEstart a new session with a specified nametmux lslist all current sessions<C-b> ddetach the current sessiontmux areattach the last session-tspecify a specific session
Window-Tab, visually divide the session into multiple parts#
<C-b> ccreate a new window,<C-d>close<C-b> Njump to the Nth window<C-b> pswitch to the previous window<C-b> nswitch to the next window<C-b> ,rename the current window<C-b> wlist all current windows
Panel-Like splitting in vim, panels allow us to display multiple shells in one screen#
<C-b> "split horizontally<C-b> %split vertically<C-b> <direction>switch to the specified direction of the panel<C-b> ztoggle zoom for the current panel<C-b> [start scrolling the screen backwards. Press space to start selecting and enter to copy the selected part<C-b> <space>switch between different panel layouts
Further Reading#
Here is a quick start guide to tmux, and this article is more detailed and includes the screen command. You may also want to learn the screen command, as it is installed by default on most UNIX systems.
Homework Exercises#
Task Control#
-
We can use commands like
ps aux | grepto get the pid of a task, and then you can end these processes based on the pid. But we actually have a better way to do this. Execute the commandsleep 10000in the terminal. Then useCtrl-Zto switch it to the background and usebgto continue running it. Now, usepgrepto find the pid and usepkillto end the process without manually entering the pid. (Hint: use the-afflag).sleep 10000 & pgrep sleep pkill -f sleep -
If you want to start another process after a certain process ends, how would you do it? In this exercise, we use
sleep 60 &as the first command. One way is to use thewaitcommand. Try starting this sleep command and then wait for it to finish before executing thelscommand.sleep 60 & pgrep sleep | wait; lsHowever, if we operate in different bash sessions, the above method will not work. Because
waitonly works for child processes. One feature we haven't mentioned before is that when thekillcommand exits successfully, its exit status is 0, and other statuses are non-zero.kill -0does not send a signal, but returns a non-zero status code when the process does not exist. Please write a bash functionpidwaitthat takes a pid as an input parameter and waits until the process ends. You need to usesleepto avoid wasting CPU performance.pidwait() { while kill -0 $1 # Loop until the process ends do sleep 1 done ls }
Terminal Multiplexing#
- Please complete this tmux tutorial and refer to these steps to learn how to customize tmux.
tmux source-file ~/.tmux.confto make the configuration file take effect
Aliases#
- Create an alias
dcthat allows us to executecdcorrectly when we mistakenly inputdc. - Execute
history | awk '{$1="";print substr($0,2)}' | sort | uniq -c | sort -n | tail -n 10to get your top ten most used commands, and try creating aliases for them.