#!/bin/bash # 备份源文件路径 source_dir="/path/to/source" # 备份目标路径 backup_dir="/path/to/backup" # 备份文件名 backup_file="backup_$(date +%Y%m%d%H%M%S).tar.gz" # 创建备份目录 mkdir -p $backup_dir # 压缩备份文件 tar -czvf $backup_dir/$backup_file $source_dir # 打印备份完成信息 echo "备份已完成,备份文件位于:$backup_dir/$backup_file"
如何使用C语言编写Linux下的守护进程
#include#include #include #include #include #include void daemonize() { pid_t pid; // 创建子进程,结束父进程 pid = fork(); if (pid< 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } // 创建新会话并成为会话组组长 if (setsid()< 0) { exit(EXIT_FAILURE); } // 忽略SIGHUP信号 signal(SIGHUP, SIG_IGN); // 创建孙子进程,结束子进程 pid = fork(); if (pid< 0) { exit(EXIT_FAILURE); } if (pid > 0) { exit(EXIT_SUCCESS); } // 修改当前路径为根目录 if (chdir("/")< 0) { exit(EXIT_FAILURE); } // 关闭标准输入、输出和错误输出 close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } int main() { // 执行守护进程化 daemonize(); // 守护进程的主要逻辑代码 return EXIT_SUCCESS; }
如何使用Python编写Linux下的系统监控脚本
import psutil import os def get_cpu_usage(): return psutil.cpu_percent(interval=1) def get_memory_usage(): return psutil.virtual_memory().percent def get_disk_usage(): partitions = psutil.disk_partitions() usage = 0.0 for partition in partitions: mountpoint = partition.mountpoint usage += psutil.disk_usage(mountpoint).percent return usage / len(partitions) def get_process_count(): return len(psutil.pids()) def send_alert(message): # 发送警报通知的逻辑 def main(): cpu_threshold = 80.0 memory_threshold = 70.0 disk_threshold = 60.0 while True: cpu_usage = get_cpu_usage() memory_usage = get_memory_usage() disk_usage = get_disk_usage() if cpu_usage > cpu_threshold or memory_usage > memory_threshold or disk_usage > disk_threshold: message = f"CPU使用率: {cpu_usage}%, 内存使用率: {memory_usage}%, 磁盘使用率: {disk_usage}%" send_alert(message) process_count = get_process_count() if process_count > 100: message = f"当前运行进程数: {process_count}" send_alert(message) sleep(60) if __name__ == "__main__": main()