Termination Techniques
Manual Termination Methods
Kill Command
## Terminate process by PID
kill -9 <PID>
## Find PID using process name
pidof script_name
Ctrl+C Interrupt
## Sends SIGINT signal to running process
Ctrl+C
Programmatic Loop Control
Break Statement
#!/bin/bash
counter=0
while true; do
((counter++))
if [ $counter -gt 10 ]; then
break ## Exit loop conditionally
fi
done
Timeout Mechanisms
graph TD
A[Termination Techniques] --> B[Manual Methods]
A --> C[Programmatic Control]
A --> D[System Timeout]
Timeout Command
## Limit script execution time
timeout 5s ./long_running_script.sh
Advanced Termination Strategies
Technique |
Description |
Use Case |
Signal Trapping |
Capture system signals |
Graceful shutdown |
Timeout Mechanism |
Limit execution duration |
Prevent resource lock |
Conditional Breaking |
Exit based on conditions |
Dynamic loop control |
Signal Handling
#!/bin/bash
trap 'echo "Script interrupted"; exit 1' SIGINT SIGTERM
while true; do
## Long-running process
sleep 1
done
LabEx Best Practices
- Implement clear exit conditions
- Use timeout mechanisms
- Handle system signals
- Log termination events
Watchdog Timer Example
#!/bin/bash
max_runtime=60 ## Maximum runtime in seconds
start_time=$(date +%s)
while true; do
current_time=$(date +%s)
runtime=$((current_time - start_time))
if [ $runtime -ge $max_runtime ]; then
echo "Maximum runtime exceeded"
break
fi
## Your script logic here
sleep 1
done