Fedora添加开机启动项

本文详细介绍了在Fedora系统中如何通过systemd服务管理器添加开机启动项,包括创建配置文件、设置权限、启用服务等步骤,并提供了自动化脚本实现便捷管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以前在ubuntu中添加开机启动项是直接在/etc/rc.local中添加一行路径加参数,到Fedora中添加启动项好像就不灵了在网上查了资料说是可以在systemd中添加,路径为/etc/systemd/system,这是一个目录,里面存放了可以启动脚本的配置文件,创建task.txt,包含如下内容

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

#!/bin/bash

curpath=$(dirname $0)
name=$1

rm -f /etc/systemd/system/$name.service is executable.
[Unit]
Description=it is my service
After=network.target
[Service]
Type=forking
ExecStart=/yourpath /yourarg
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target


其中ExecStart=/yourpath /yourarg表示你要启动的文件和参数

Description=it is my service为服务描述

然后将task.txt复制为/etc/systemd/system/hello_world.service

然后设置该文件为可执行:chmod +x /etc/systemd/system/hello_world.service

然后将其设置为有效:systemctl enable hello_world.service

这样就ok了,其中hello_world为服务的名字,可以自定义

为了方便可以将上诉操作写为脚本AddTask.sh

AddTask.sh

#!/bin/bash
  
curpath=$(dirname $0)
 
path=$1
name=$2


echo $curpath/my.txt
 
sed 's:/yourpath:'$path':g' $curpath/task.txt > $curpath/my.txt


cp $curpath/my.txt /etc/systemd/system/$2.service
chmod 777 /etc/systemd/system/$2.service
systemctl enable $2.service

传入的第一个参数为路径,第二个参数为服务名,这样就ok了

如果要删除服务可以直接调用DelTask.sh文件,文件内容如下:

#!/bin/bash

name=$1

rm -f /etc/systemd/system/$name.service

这样就完成了开机启动项的添加和删除




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值