In Linux, there are several ways to execute commands that continue running even after you close your terminal session:
- Background execution with &: The ampersand symbol allows a command to run in the background. However, if you close your terminal (like Xshell), the process will terminate.
- nohup command: This allows a command to keep running after you log out. It closes standard input, redirects output and errors to a file called nohup.out in the current directory, and makes the process immune to terminal disconnection.
- Combining nohup with &: This runs a command in the background with nohup, allowing both background execution and immunity to terminal disconnection.
Examples:
# Run a Python script in the background
python application.py &
# Execute a Java application that persists after logout
nohup java -server myapp.jar
# Run a Node.js service in background with persistence
nohup node server.js &
Monitoring Output from Persistent Processes
When using nohup, all output is redirected to the nohup.out file. You can monitor this file using the tail command:
# Continuously display the last 50 lines of nohup.out
tail -fn 50 nohup.out
# Display the last 100 lines without following
tail -n 100 nohup.out
Process Management with ps
The ps command displays information about running processes. Here are common options:
| Parameter | Description |
|---|---|
| -A | List all processes |
| -e | Select all processes (equivalent to -A) |
| -f | Display process in full format |
| -l | Display long format |
| -u | Display processes by user |
| -x | Show processes not attached to a terminal |
| -aux | Display detailed information about all processes |
Finding Process IDs:
# Find processes related to a specific application
ps -ef | grep nginx
# Display all processes with detailed information
ps aux
Disk Space Management
Managing disk space is essential for system administration:
# Check directory sizes with depth limit
du -h --max-depth=1 /var/www/
# Display disk usage summary
df -h
Background Job Control
The jobs command displays tasks running in the background of the current shell:
| Command | Description |
|---|---|
| jobs | List all background jobs |
| jobs -l | List jobs with process IDs |
| jobs -r | Only show running jobs |
| jobs -s | Only show stopped jobs |
Bringing Background Jobs to Foreground
You can move a background job to the foreground using the fg command:
| Command | Description |
|---|---|
| fg | Bring the most recent background job to foreground |
| fg %2 | Bring job number 2 to foreground |
| fg %+/- | Bring most recent/previous job to foreground |
Resuming Stopped Background Jobs
The bg command resumes a stopped background job:
# Resume a specific background job
bg %3
Terminating Processes
You can terminate processes using the kill command with various signals:
| Command | Description |
|---|---|
| kill -l | List available signals |
| kill -9 PID | Forcefully terminate a process |
| kill -15 PID | Gracefully terminate a process |
| kill -18 PID | Resume a stopped process |
| kill -19 PID | Pause a running process |
System Shutdown and Restart
There are multiple ways to shut down or restart a Linux system:
Using shutdown (recommended):
# Immediate shutdown
shutdown -h now
# Shutdown in 10 minutes
shutdown -h +10
# System restart
shutdown -r now
# Cancel a scheduled shutdown
shutdown -c
Alternative commands:
# Immediate shutdown
halt
poweroff
# System restart
reboot
# Change runlevel (0=halt, 6=reboot)
init 0
init 6
Static IP Configuration (Ubuntu)
To configure a static IP address on Ubuntu:
# Edit network interfaces configuration
sudo vim /etc/network/interfaces
# Add the following configuration:
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
# Restart networking service
sudo /etc/init.d/networking restart
Firewall Management (Firewalld)
Firewalld is the default firewall management tool on modern Linux distributions:
# Check firewall status
systemctl status firewalld
firewall-cmd --state
# Start, restart, or stop the firewall
sudo systemctl start firewalld
sudo systemctl restart firewalld
sudo systemctl stop firewalld
# List firewall rules
sudo firewall-cmd --list-all
# Manage ports
sudo firewall-cmd --query-port=8080/tcp
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --reload
# Enable/disable firewall at boot
sudo systemctl enable firewalld
sudo systemctl disable firewalld
Log Analysis with tail and grep
These commands are essential for system monitoring and troubleshooting:
# Follow log file in real-time
tail -f /var/log/syslog
# Display last 100 lines and follow new entries
tail -100f /var/log/application.log
# Search for specific patterns in recent log entries
tail -100f /var/log/app.log | grep "ERROR"
# Find log entries within a time range
tail -1000f /var/log/app.log | grep '2023-05-15 14:[0-5][0-9]'
# Show matching lines plus 5 lines after each match
tail -1000f /var/log/app.log | grep -A 5 "Exception"
SSH Remote Connections
Secure Shell (SSH) allows secure remote server access:
# Basic SSH connection
ssh user@192.168.1.100
# Connect with specific port
ssh -p 2222 user@example.com
# Execute command on remote server
ssh user@server "df -h"
Secure File Transfer with SCP
SCP (Secure Copy) allows secure file transfers between systems:
# Copy directory to remote server
scp -r /local/directory user@remote:/path/to/destination
# Copy file from remote to local
scp user@remote:/remote/file.txt /local/destination/
# Copy with custom port
scp -P 2222 user@remote:/file.txt /local/
# Preserve file attributes
scp -p /local/file user@remote:/destination/
Checking Login History
System administrators can monitor user login activity:
Successful login attempts:
# View recent login history
last -n 10
# View with IP addresses resolved to hostnames
last -a -n 10
Failed login atempts:
# View failed login attempts
lastb -n 10
# Clear failed login logs (requires root)
sudo > /var/log/btmp
Clearing login history:
# Clear successful login logs
sudo > /var/log/wtmp
# Clear failed login logs
sudo > /var/log/btmp