Ubuntu平台使用systemd服务实现进程的开机启动

Ubuntu平台使用systemd服务实现进程的开机启动


前言

本文记录在Ubuntu系统使用systemd实现进程的开机启动,并管理这个进程,当系统重启或者进程意外崩溃时能重新开启这个进程。


一、使用 systemd 服务管理器

systemd 是大多数现代 Linux 发行版中默认的系统和服务管理器,可以用来管理系统启动时的服务。其他可以实现开机启动的方式有:
1,使用 ‘cron’ 定时任务;
2,在 ’.bashrc‘ 或 ’.profile‘ 中添加启动命令;
3,使用 ‘/etc/rc.local’;
systemd 是推荐的方式,特别是在现代 Linux 发行版上。它更灵活、更强大,也可以更好地控制服务的启动和停止。如果使用较老的 Linux 发行版或者需要更简单的解决方案,可以考虑 cron 或 rc.local等。

1.创建systemd文件

[Unit]
Description=AI skynet Service
After=network.target

[Service]
ExecStart=/usr/local/zrodo/skynet/mydaemon
Type=forking
PIDFile=/var/run/mydaemon.pid
Restart=always
RestartSec=10
User=root
Group=root

[Install]
WantedBy=multi-user.target

其中:
Description对服务的简短描述。
After=network.target:确保在网络服务启动后才启动该服务(如果不依赖网络,可以移除这一行)。
ExecStart 是要启动的应用程序的路径。
Restart 设定为 always,当进程意外退出时会自动重启。
User 和 Group 是运行服务的用户和组(可以省略,默认为 root)。
Restart=always: 无论退出状态是什么,总是重新启动服务。
RestartSec=10: 服务重启的时间间隔为10秒,避免过于频繁的重启。
Type=forking: 适用于程序在启动时 fork 出一个子进程,并且父进程退出的情况。在这种情况下,需要确保 mydaemon 能正确生成一个 PID 文件。
PIDFile如果进程在启动后生成了一个 PID 文件(通常用于守护进程模式),需要指定这个 PID 文件的路径。systemd 会使用这个 PID 文件来跟踪服务的主进程。
确保你的程序确实生成了这个 PID 文件,并且路径正确。如果你的程序不生成 PID 文件,你可以忽略这一行,但可能会导致 systemd 无法正确管理进程。
Type=simple: 适用于程序不 fork 的情况,即程序不会生成新的子进程,而是直接在前台运行。
关键点
自动重启:Restart=always 使服务在任何情况下(崩溃或手动停止)都会自动重启。
定期检查:systemd 通过内部机制(如 RestartSec)来管理服务的重启和检查,不需要额外编写脚本来检查进程状态。

2.生成pid文件

在进程代码中加入如下两个生成和移除pid文件的方法,路径:“/var/run/mydaemon.pid”

void create_pid_file(const std::string &pid_file_path) {
    std::ofstream pid_file(pid_file_path);
    if (pid_file.is_open()) {
        pid_file << getpid() << std::endl;
        pid_file.close();
    } else {
        std::cerr << "Unable to create PID file: " << pid_file_path << std::endl;
    }
}

void remove_pid_file(const std::string &pid_file_path) {
    std::remove(pid_file_path.c_str());
}

关键步骤解释:
create_pid_file这个函数用于创建一个指定路径的 PID 文件,并将当前进程的 PID 写入其中。这里使用了 getpid() 函数获取当前进程的 PID。
remove_pid_file当进程正常退出时,这个函数用于删除 PID 文件。这样可以确保不留下过时的 PID 文件。
另外:
进程中还需要使用 fork() 函数将进程从控制台中分离出来。如果 fork 成功,父进程退出,子进程继续运行。
PID 文件通常放在 /var/run/ 或 /run/ 目录下。你需要确保进程有权限在这个目录下创建文件。

3.启动服务

sudo systemctl daemon-reload
sudo systemctl enable mydaemon.service
sudo systemctl start mydaemon.service

4.检查服务状态和日志

sudo systemctl status mydaemon.service
journalctl -xe -u mydaemon.service
  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux或macOS系统中,让Ngrok在每次系统启动时自动运行,可以利用系统级别的服务管理器。以下是两种常见的方法: **在Ubuntu和Debian系列(Systemd)**: 1. **安装ngrok**: 使用apt-get安装: ``` sudo apt-get install ngrok ``` 2. **创建systemd服务**: 编辑`/etc/systemd/system/ngrok.service`文件,添加以下内容: ```ini [Unit] Description=Ngrok HTTP/TLS Tunnel After=network.target [Service] ExecStart=/usr/local/bin/ngrok authtoken [YOUR_NGROK_API_TOKEN] http [YOUR_PORT] Restart=always User=<your-user> WorkingDirectory=<path-to-your-project> [Install] WantedBy=multi-user.target ``` 将`[YOUR_NGROK_API_TOKEN]`和`[YOUR_PORT]`替换为你实际的Ngrok API token和需要绑定的端口号。 3. **启用并启动服务**: ```sh sudo systemctl daemon-reload sudo systemctl enable ngrok sudo systemctl start ngrok ``` **在macOS(Homebrew)**: 1. **安装ngrok**: 执行: ```sh brew install ngrok ``` 2. **创建launchd守护进程**: 创建`~/Library/LaunchAgents/homebrew.ngrok.plist`文件,加入内容: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>homebrew.ngrok</string> <key>ProgramArguments</key> <array> <string>/usr/local/bin/ngrok</string> <string>authtoken</string> <string>[YOUR_NGROK_API_TOKEN]</string> <string>http</string> <string>[YOUR_PORT]</string> </array> <!-- 其他选项如User等可选 --> </dict> </plist> ``` 3. **加载和启动守护进程**: ```sh launchctl load ~/Library/LaunchAgents/homebrew.ngrok.plist ``` 请注意替换`[YOUR_NGROK_API_TOKEN]`和`[YOUR_PORT]`为你的Ngrok API密钥和应用绑定的端口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值