在linux中将程序设置成服务

Linux 11个月前 管理员
3,279 2 0

1、编写一个脚本文件用于启动服务,脚本文件名称 frps-start

#!/bin/bash
# 该脚本用于启动frps程序

# 检查frps.ini文件是否存在
if [ ! -f "/home/frp/frps.ini" ]; then
echo "frps.ini文件不存在,请确保文件路径正确"
exit 1
fi

# 执行frps命令,使用./frps.ini作为配置文件,&标识后台启动
/home/frp/frps -c /home/frp/frps.ini &

# 将frps进程添加到systemd服务中,以便通过service frps restart命令重启
echo "$(date) - frps已启动" >> /var/log/frps.log

2、给这个文件添加可执行权限

chmod +x frps

3、创建一个名为frps.service的文件,然后把这个文件放到/etc/systemd/system目录下

[Unit]
Description=frps Service
After=network.target

[Service]
Type=simple
# 根据实际情况修改运行脚本的用户
User=root
# 修改为实际的脚本路径
ExecStart=/home/frp/script/frps-start
# 如果失败,尝试重启服务
Restart=on-failure
# 根据需求选择不同的进程结束模式,此处为正常退出
KillMode=process
# 是否允许在private tmp目录下创建临时文件,根据需求修改
PrivateTmp=true
# 如果服务超时(默认为10秒),则停止服务,此处设置为30秒
TimeoutStopSec=30
# 如果主进程超时或崩溃,是否向其发送SIGKILL信号,此处设置为yes
SendSIGKILL=yes

[Install]
# 定义服务的启动目标,此处为多用户模式启动
WantedBy=multi-user.target

4、执行命令来启用该服务,同时启动这个服务

# 启用服务,以便在系统启动时自动运行
sudo systemctl enable frps.service
# 启动服务
sudo systemctl start frps.service

5、最后可通过常用的命令控制

# 启动服务
service frps start
# 停止服务
service frps stop
# 重启服务
service frps restart

 

 

 

版权声明:管理员 发表于 2024-05-10 23:13。
转载请注明:在linux中将程序设置成服务 | kk编程驿站

相关文章

2 条评论

  • Desmond Eichmann

    Your exemplary work demonstrates remarkable potential. With strategic enhancements, this foundation can be transformed into a pioneering contribution that advances the field and solidifies your position as an industry authority.

    未知
    回复
  • Temp mail
    Temp mail 游客

    Your blog is a true gem in the world of online content. I'm continually impressed by the depth of your research and the clarity of your writing. Thank you for sharing your wisdom with us.

    未知
    回复