A process is a program that is running.
When you start a program, the operating system first reads the program file from the hard disk, then loads the program contents into memory, and finally the CPU executes the program. This is a process.
Basic concepts of Process
Checking processes
htop
ps (process status)
ps
shows only the relevant processes running in this terminal.
ps aux
shows all processes.
PID (process identifier)
The PID is a number that uniquely identifies a process.
The PID can be used as an index when hanging, continuing or terminating a process.
Process priority
NI (nice)
The range of values is -20 to 19. Usually the NI value of the currently running process is 0. The higher the NI, the lower the priority.
Specify the priority:
1 | $ nice -n 10 vim # Run vim with a nice value of 10 |
To lower the nice value you need sudo
.
PRI (priority)
Normally PRI = nice + 20. If the PRI value is RT, or if it is negative, the corresponding process has higher real-time requirements (e.g. kernel processes, audio-related processes, etc.).
Process status
Roughly divided into:
- Running
- Ready
- Blocked
Detailed classification:
State | Abbreviation | Description |
---|---|---|
Running | R | Running/ready to run immediately |
Sleeping | S | Interruptible sleep |
Disk Sleep | D | Non-interruptible sleep |
Traced / Stopped | T | Traced / Suspended processes |
Zombie | Z | Zombie processes |
Process Control
Signal
Signal is a mechanism for processes to communicate with each other on Unix family systems.
The Linux command that sends signals is called kill
.
Use man 7 signal
to see the list of signals.
Switching from front to back
By default, commands run in the shell are run in the foreground. If you need to run in the background, you need to add &
to the end of the command.
If you need to switch the foreground program to the background, you need to press Ctrl + Z to send SIGTSTP to hang the process and return control to the shell.
1 | $ bg %2 # Put process 2 into the background |
Terminating a process
The standard signal to terminate a process is SIGTERM, which implies the natural death of a process.
Sending the signal in htop
Press K, select the desired signal in the left prompt bar and press enter to send it.
Kill
1 | $ kill -l # Show all signals |
If you don’t add any arguments, only PID, kill
will automatically use 15 (SIGTERM) as signal argument.
Kill-like commands
1 | $ apropos kill # Show various other kill-like commands |
Get out of the terminal
nohup
Once a terminal is closed, it sends a SIGHUP (Signal Hangup) to each process in it, and the default action for SIGHUP is to quit the program running. By adding nohup
before the program that needs to block SIGHUP, the runtime output will be redirected to nohup.out
(which can also be customized).
tmux
“tmux” is a split-screen, command-line emulated terminal that runs on the command line. It can integrate multiple interactive processes in a single window and does not disappear when disconnected or temporarily logged out, but is stored in the background and can be restored immediately when logging in next time.
“tmux” is divided into three parts:
- Session: to distinguish different jobs;
- Window: different pages of the session in terms of displays;
- Pane: different areas of the window divided by white lines.
To install and open tmux:
1 | $ sudo apt install tmux |
Shortcut keys (Ctrl + B first) | Function |
---|---|
% | Left/right split screen |
“ | Up and down split screen |
↑ ↓ ← → | The focus switches to the top, bottom, left, and right pane, and the pane being interacted with is selected in green. |
d (detach) | Detach from tmux and return to the command line interface. |
z (zoom) | Temporarily fullscreen the pane, and press again to restore it to its original state |
c | Create a new window |
, | Name the window |
s | List all sessions |
If you are disconnected, use tmux attach [-t window name]
to reconnect the window when you log in again. Without -t
, the last opened window will be connected by default.
Customizing tmux
Service
Daemon
The only way to avoid disappearing due to terminal shutdown is to disconnect from the session at startup. Such processes, which work silently in the background, are called daemons.
Service management
The init scheme for most Linux distributions is systemd, whose command for managing system services is systemctl
.
Use the systemctl status
command to see what services are running on the system. Use systemctl list-units
to see the status of all systemd managed services.
Use tldr
to view the commands for starting, terminating, and reloading configuration of services.
Customizing services
Write a .service configuration file and run it.
Routine tasks
Refers to the implementation of periodic scheduled tasks. The main ones in Linux are at
and crontab
.
at
at
is responsible for single scheduled tasks.
Installation:
1 | $ sudo apt install at |
Use tldr
to see how to use it.
crontab
crontab
is responsible for periodic tasks.
Unlike at
, crontab
is mostly implemented through configuration files.
To see the usage of crontab:
1 | $ crontab --help |
Configuration format: The first half of each line is the time, and the second half is the shell execution command.
# minutes hour day month week | command
# Example
* * * * * echo "hello" >> ~/count
# Output hello every minute to count file in home directory
0,15,30,45 0-6 * jan sun command
# Do something randomly every Sunday in January every year for 15 minutes from 0:00 midnight to 6:00 am
The crontab.guru website can translate the configuration file into an easy-to-understand representation.
Appendix
Reference Tutorial
Linux 101 - chap 04: 进程、前后台、服务与例行性任务
Recommended Reading
Comments