编写一个Linux进程守护脚本的大致步骤如下:
#!/bin/bash
process_name="your_process_name"
process_path="your_process_path"
check_process() {
local pid=$(pgrep -f $process_name)
if [[ -n "$pid" ]]; then
return 0
else
return 1
fi
}
start_process() {
$process_path &
}
stop_process() {
local pid=$(pgrep -f $process_name)
if [[ -n "$pid" ]]; then
kill -9 $pid
fi
}
while true; do
if ! check_process; then
start_process
fi
sleep 5
done
stop_process
完整的脚本示例:
#!/bin/bash
process_name="your_process_name"
process_path="your_process_path"
check_process() {
local pid=$(pgrep -f $process_name)
if [[ -n "$pid" ]]; then
return 0
else
return 1
fi
}
start_process() {
$process_path &
}
stop_process() {
local pid=$(pgrep -f $process_name)
if [[ -n "$pid" ]]; then
kill -9 $pid
fi
}
while true; do
if ! check_process; then
start_process
fi
sleep 5
done
stop_process
请根据实际情况修改process_name和process_path变量,然后保存脚本并设置为可执行文件(chmod +x daemon.sh)。