🐧 Module 0.4 Parts 4–6 26 Topics

Linux Processes, Bash
& Serial Communication

Process management and permissions, I/O redirection and piping, Bash scripting, tmux, picocom, and the hardware utilities used daily in embedded development.

Part 4 — Processes & Permissions
🔁Part 5 — Redirection, Pipes & Bash
🔌Part 6 — tmux, Serial & Hardware
⚙  Part 4 — Processes & Permissions
1Processes

What is a Process?

A process is a program that is currently running. Every application you open becomes a process, and Linux manages all running processes using the kernel.

Every process gets a unique Process ID (PID):

PID COMMAND 2105 bash 3250 code 4187 python

Many Linux commands use the PID to identify and control a specific process.

CommandPurpose
psView running processes
topLive system monitor
htopInteractive process monitor
killStop a process
jobsShow background jobs
fgBring a job to foreground
bgResume a job in background
psDisplay running processes

Shows a snapshot of currently running processes.

bash
ps                    # processes in this terminal
ps aux                # all processes on the system
ps aux | grep picocom # check if picocom is running
🏭
Embedded Example
ps aux | grep openocd — verify that OpenOCD is running before attempting to debug. Helps catch the common "already running" conflict.
top / htopLive system monitor

top shows CPU, memory, and all running processes updated in real time. Press q to quit.

bash
top

htop is an improved, colour-coded version of top with a more user-friendly interface. Install it if it isn't already present:

bash
sudo apt install htop
htop
💡
Embedded Tip
Run top or htop during a large firmware build to monitor CPU and memory usage in real time — useful for spotting runaway compilation jobs or memory pressure.
killStop a process by PID

Sends a signal to a process. By default it sends SIGTERM (graceful stop). Use -9 for an immediate forced termination.

bash
kill 4187       # ask process 4187 to stop gracefully
kill -9 4187    # immediately force-terminate process 4187
⚠️
Remember
kill requests a clean stop. kill -9 is the force option — the process has no chance to save state or clean up. Use -9 only when the process is frozen.
jobs / fg / bgManage background jobs

jobs — list background jobs

bash
jobs
# Output: [1]+ Running   python script.py &

fg — bring to foreground

bash
fg       # bring most recent job to foreground
fg %1    # bring job number 1

bg — continue in background

bash
bg       # resume the stopped job in background
6Processes

Ctrl+C vs Ctrl+Z

Ctrl+C — STOP
Terminates the foreground program. Sends the SIGINT signal. The program exits completely.
Ctrl+Z — PAUSE
Suspends the foreground program. Sends the SIGTSTP signal. Resume later with fg or bg.
💡
Quick Rule
Ctrl+C = done, stop everything. Ctrl+Z = paused, I'll come back to this. Use fg to resume after Ctrl+Z.
7Processes

Common Linux Signals

SignalValuePurpose
SIGINT2Interrupt a process (Ctrl+C)
SIGTERM15Request graceful termination
SIGKILL9Force terminate — cannot be caught
SIGTSTP20Suspend a process (Ctrl+Z)
SIGCONT18Continue a stopped process
8Permissions

Linux File Permissions

Linux protects files using permissions that determine who can read, modify, or execute a file. Every file has three permission groups:

-rwxr-xr-- type Owner rwx read+write+exec Group r-x read+exec Others r-- read only Numeric Values r = 4 w = 2 x = 1 - = 0 755 = rwxr-xr-x

Every file has three groups of permissions. Each group can have read (r=4), write (w=2), and execute (x=1) — or none (-).

bash
ls -l
# -rwxr-xr-- 1 satwik satwik 1234 Jun 1 script.sh
chmodChange file permissions

Symbolic Mode

bash
chmod +x script.sh   # add execute permission
chmod -x script.sh   # remove execute permission

Numeric Mode

bash
chmod 644 file       # rw-r--r-- (common for text files)
chmod 755 script.sh  # rwxr-xr-x (common for scripts)
chmod 777 file       # rwxrwxrwx (avoid — grants full access to everyone)
PermissionModeResultUse Case
chmod 644rw-r--r--Owner: read+write. Others: readConfig files, source code
chmod 755rwxr-xr-xOwner: all. Others: read+execShell scripts, binaries
chmod 777rwxrwxrwxEveryone has full accessAvoid unless absolutely needed
🏭
Embedded Example
chmod +x flash.sh && ./flash.sh — make the flash script executable, then run it immediately.
chownChange file ownership

Changes the owner (and optionally the group) of a file or directory.

bash
sudo chown satwik project.txt          # change owner
sudo chown satwik:developers file.txt  # change owner and group
💡
Remember
ls -l shows current permissions. chmod changes permissions. chown changes ownership. 644 is common for files, 755 for scripts and directories.
🔁  Part 5 — Redirection, Pipes & Bash Scripting
11I/O Streams

stdin / stdout / stderr

Every Linux program communicates using three standard streams. Understanding these is the foundation of redirection and pipes.

Keyboard stdin (0) stdin Program any Linux command stdout (1) stderr (2) Terminal Terminal

By default all three streams connect to the terminal. Redirection changes where each stream goes.

StreamNumberDefault source/destPurpose
stdin0KeyboardInput data for the program
stdout1TerminalNormal program output
stderr2TerminalError messages only
12Redirection

Output Redirection

> — Overwrite
ls > files.txt Redirects stdout to a file. ⚠ Overwrites existing content.
>> — Append
date >> log.txt Adds to end of file. ✓ Preserves existing content.

Input Redirection

bash
sort < numbers.txt   # sort reads from file instead of keyboard
13Redirection

Pipes ( | )

A pipe sends the stdout of one command directly into the stdin of another — without creating any temporary files.

ps aux | grep python filtered result

Pipes chain commands together. The output of each command flows directly into the next.

bash
ps aux | grep python        # find running Python processes
find . -name "*.c" | wc -l  # count all C source files
dmesg | tail                 # see the last kernel messages
💡
Remember
Pipes avoid creating temporary files by connecting commands directly. You can chain multiple pipes: cmd1 | cmd2 | cmd3.
14Redirection

Error Redirection

bash
cat missing.txt 2> errors.txt   # redirect only errors to file
make > build.log 2>&1           # redirect both stdout AND stderr to same file

The 2>&1 syntax means: "redirect file descriptor 2 (stderr) to wherever file descriptor 1 (stdout) is going." This is used constantly to capture complete build logs.

🏭
Embedded Example
west build > build.log 2>&1 — capture the entire build output (including errors) into a single log file. Then grep "error" build.log to find the failures.
15Bash Scripting

Bash Shell

Bash (Bourne Again SHell) is the default command-line shell on most Linux systems. It reads commands from the user, executes them, and displays the results. It also runs shell scripts — files containing lists of commands to automate tasks.

16Bash Scripting

Shell Scripts

A shell script is a text file containing Linux commands. Instead of typing the same commands repeatedly, save them once and execute whenever needed.

bash — build.sh
#!/bin/bash

echo "Building..."
make
echo "Done"

Shebang — the first line

The #!/bin/bash line tells Linux which interpreter should run the script. It must be the very first line.

Make it executable and run it

bash
chmod +x build.sh   # make executable (only needed once)
./build.sh          # run the script

Comments

Lines beginning with # are comments. They are ignored during execution but improve readability.

bash
# Build firmware for ESP32
make
17Bash Scripting

Variables & echo

bash
PROJECT=ESP32          # assign variable (no spaces around =)
echo $PROJECT          # use with $ prefix → outputs: ESP32

PORT=/dev/ttyUSB0
echo "Connecting to $PORT"

read NAME              # read user input
echo "Hello $NAME"
💡
Variable Rule
No spaces around = when assigning. Always use $ prefix when reading a variable. Variables can store paths, names, numbers, or command output.
18Bash Scripting

Exit Status

Every command returns an exit status (also called a return code) when it finishes. This is the most important way scripts detect success or failure.

bash
make
echo $?       # 0 = success, non-zero = error
0 = Success
Command completed correctly. Build succeeded. File was found.
Non-zero = Error
Command failed. Build error occurred. File not found.
19Bash Scripting

set -euo pipefail

Professional shell scripts begin with this line to make them safe and reliable:

bash
set -euo pipefail
FlagEffect
-eStop the script immediately if any command fails
-uTreat any undefined variable as an error
pipefailIf any command in a pipe fails, the entire pipeline fails

Complete Embedded Script Example

bash — build-flash-monitor.sh
#!/bin/bash

set -euo pipefail

west build
west flash
picocom -b 115200 /dev/ttyUSB0

This script builds firmware, flashes it to the board, and opens the serial monitor. If any step fails, the script stops immediately rather than continuing with a broken state.

📊 Redirection & Bash Quick Reference
  • > overwrites a file with command output.
  • >> appends output to a file (keeps existing content).
  • | connects commands — output of one becomes input of next.
  • 2>&1 captures both stdout and stderr to the same destination.
  • #!/bin/bash must be the first line of every shell script.
  • set -euo pipefail is best practice for reliable scripts.
🔌  Part 6 — tmux, Serial Communication & Hardware Utilities
tmuxTerminal Multiplexer — persistent, split terminal sessions

tmux lets you run multiple terminal sessions inside one window. Unlike a normal terminal, a tmux session keeps running even if you disconnect from SSH or close the terminal.

Without tmux
Close terminal → build stops. SSH disconnect → picocom dies. One task per terminal window.
With tmux
Long builds continue. Serial monitors stay active. Multiple panes in one window.

Session Commands

bash
tmux new -s embedded       # create session named "embedded"
tmux ls                    # list all running sessions
tmux attach -t embedded    # re-attach to "embedded" session

To detach (leave session running in background): press Ctrl+B then D.

Keyboard Shortcuts (prefix: Ctrl+B)

ShortcutAction
Ctrl+B CCreate a new window
Ctrl+B %Split pane vertically
Ctrl+B "Split pane horizontally
Ctrl+B ArrowMove between panes
Ctrl+B DDetach (session stays running)
🏭
Embedded Workflow with tmux
Open three panes: Pane 1 → west build. Pane 2 → west flash. Pane 3 → picocom -b 115200 /dev/ttyUSB0. All three stay running simultaneously — even over SSH.
21Serial Communication

UART & Serial Device Files

UART (Universal Asynchronous Receiver/Transmitter) is the hardware interface used for serial communication — transferring data one bit at a time between two devices.

Computer Ubuntu Linux USB USB-UART Converter CP2102 / CH340 / FT232 UART Microcontroller ESP32 / STM32 / nRF52 Linux creates /dev/ttyUSB0

When the board is connected, Linux automatically detects it and creates a device file — the address you use to open a serial connection.

Serial Device Files

DeviceCreated byExample boards
/dev/ttyUSB0USB-to-UART chips (CP2102, CH340, FT232)ESP32, ESP8266
/dev/ttyACM0Native USB CDC devicesSTM32 USB CDC, Arduino Leonardo

Baud Rate

The baud rate is the communication speed. Both sides must use the same value. Common values: 9600, 115200, 230400, 460800. Most embedded projects use 115200.

picocomLightweight serial terminal
bash
picocom -b 115200 /dev/ttyUSB0             # open serial monitor
picocom -b 115200 --logfile serial.log /dev/ttyUSB0  # + save to log

To exit: press Ctrl+A then Ctrl+X.

dialout Group — Required for Serial Access

Serial devices are only accessible to members of the dialout group. If picocom gives a "Permission denied" error, add yourself to the group:

bash
groups                              # check your current groups
sudo usermod -aG dialout $USER      # add yourself to dialout
⚠️
Important
Log out and log back in after running usermod. The group membership only takes effect in new login sessions — the terminal you ran the command in won't be updated until you re-login.
23Hardware Utilities

dmesg & lsusb

dmesgDisplay Linux kernel messages

Shows messages from the Linux kernel. Most useful immediately after plugging in a board — it shows whether Linux detected the device and what device file was created.

bash
dmesg | tail     # show last kernel messages (use after plugging in board)
[ 1234.567] usb 1-1: new full-speed USB device number 4 [ 1234.892] ch341-uart converter now attached to ttyUSB0
lsusbList connected USB devices

Lists all USB devices currently connected to the system. Use it to verify Linux has detected your board.

bash
lsusb
Bus 001 Device 004: ID 10c4:ea60 Silicon Laboratories CP210x UART Bridge
24Hardware Utilities

ss & tree

ssDisplay network sockets
bash
ss -tln    # show listening TCP ports
ss -tlnp   # show ports with process names

Useful for debugging networking services — verify which ports your embedded server, MQTT broker, or debug interface is listening on.

treeDisplay directory as a visual tree
bash
tree         # full tree from current directory
tree -L 2    # limit depth to 2 levels
ESP32_Project/ ├── src/ │ ├── main.c │ └── uart.c ├── include/ └── platformio.ini

Essential for understanding unfamiliar project structures.

25Reference

Typical Embedded Workflow

This is the standard sequence every embedded developer follows when connecting a board for the first time:

1. Plug in the board 2. dmesg | tail ↳ confirm Linux detected it, note device file 3. lsusb ↳ confirm USB device is listed 4. ls /dev/ttyUSB* /dev/ttyACM* ↳ identify the exact device path 5. picocom -b 115200 /dev/ttyUSB0 ↳ open serial monitor (Ctrl+A Ctrl+X to exit) 6. west build ↳ compile firmware 7. west flash ↳ flash firmware to board 8. tail -f firmware.log ↳ monitor log output in real time
🔌
Permission Error Fix
If picocom gives "Permission denied": run sudo usermod -aG dialout $USER then log out and back in. You only need to do this once per machine.
26Reference

Complete Command Reference

CommandPurposePart
pwdPrint current directoryPart 1
ls / ls -laList files (including hidden)Part 2
cdChange directoryPart 2
mkdir / rmdirCreate / remove directoryPart 2
touchCreate empty filePart 2
cp / mv / rmCopy / Move / DeletePart 2
cat / lessView file / page-by-pagePart 3
head / tailFirst / last lines; tail -f for livePart 3
findFind files by name or typePart 3
grep / rgSearch text inside filesPart 3
du / dfFolder size / disk free spacePart 3
ps / top / htopView / monitor processesPart 4
killStop a process by PIDPart 4
chmod / chownChange permissions / ownerPart 4
> / >> / |Redirect output / pipePart 5
2>&1Merge stderr into stdoutPart 5
tmuxPersistent terminal sessionsPart 6
picocomSerial terminal for boardsPart 6
dmesgKernel messages (device detection)Part 6
lsusbList connected USB devicesPart 6
ssNetwork sockets / open portsPart 6
treeDirectory tree visualisationPart 6
🚀 Final Notes
  • These commands are used daily when working with Zephyr RTOS, ESP-IDF, STM32Cube, PlatformIO, Git, CMake, OpenOCD, West, SSH, and Raspberry Pi.
  • Reading alone is not enough — practice each command in a real terminal on your own projects.
  • The man command (manual) is available for every command: man grep, man find, etc.
  • Combine commands with pipes — real-world usage chains multiple tools together.
  • Use tmux from day one when working on embedded Linux projects over SSH.
  • Always run dmesg | tail first when a board isn't detected — it tells you exactly what happened.

Module 0.4 (Parts 4–6) — Processes, Permissions, Bash & Serial · Embedded Systems Foundations