Ubuntu下自启动:程序Systemd启动配置文件

本文详细介绍了如何创建一个systemd服务配置文件,使得在Ubuntu系统网络启动后,能自动执行/home/ubuntu/Desktop/test目录下的程序。首先,确认系统使用systemd,然后创建名为restart.service的配置文件,设置工作目录和执行命令。接着,重新加载systemd配置,启动并检查服务状态,最后将其设置为开机启动项。
摘要由CSDN通过智能技术生成

本文介绍如何通过systemd配置文件,在网络启动后自动启动/home/ubuntu/Desktop/test文件夹下的程序,并提供详细指导步骤

编写配置文件

1.查看第一个进程是否为systemd,命令为:

ps -e

2.打开终端,使用以下命令创建一个名为restart.service(名字可以按自己想法来命名)的服务配置文件:

sudo vim /etc/systemd/system/restart.service

3.将以下内容写入到配置文件中:

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

[Service]
WorkingDirectory=/home/ubuntu/Desktop/test
ExecStart=/home/ubuntu/Desktop/test/demo &!
Restart=always

[Install]
WantedBy=multi-user.target

4.保存并退出配置文件。

配置文件生效

1.重新加载systemd配置:

sudo systemctl daemon-reload

2.启动restart服务:

sudo systemctl start restart

3.验证服务是否启动:

sudo systemctl status restart

如果服务已经成功启动,可以看到以下内容的输出:

● restart.service - Demo Service
     Loaded: loaded (/etc/systemd/system/restart.service; disabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-07-23 10:06:05 CST; 10s ago
   Main PID: 15651 (demo)
      Tasks: 9 (limit: 2262)
     Memory: 4.0M
        CPU: 15ms
     CGroup: /system.slice/restart.service
             └─15651 /home/ubuntu/Desktop/test/demo "&!"

4.将restart服务添加到系统启动项中:

sudo systemctl enable restart

然后每次系统启动后,restart服务都会自动启动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Supreme_King.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值