Ubuntu18.04 自启动任务

Ubuntu18.04 没有自带其他linux版本自带的rc.local文件的开机自启动方法,所以要自己生成rc.local文件:

生成编辑rc-local.service文件,将以下内容复制进rc-local…service文件

sudo gedit /etc/systemd/system/rc-local.service

将以下内容复制进该文件

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local  #脚本文件位置
 
[Service]
Type=forking
ExecStart=/etc/rc.local start  #配置的脚本文件rc.local为start
TimeoutSec=0    
StandardOutput=tty  #标准输出
RemainAfterExit=yes
SysVStartPriority=99  #优先级,当有多个开机启动文件时可以设置不同的值
 
[Install]
WantedBy=multi-user.target

生成编辑rc.local

sudo gedit /etc/rc.local

并将下列代码添加到rc.local文件中

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0

给rc.local加上权限,启用服务,启动服务并检查状态

sudo chmod +x /etc/rc.local
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

重启并检查test.log文件

cat /usr/local/test.log

利用rc.local文件配置开机自启动脚本
sudo gedit /etc/rc.local打开脚本文件,然后在exit 0前面加入需要执行的代码即可。这里如果不需要后台运行就直接加执行代码,如果需要后台运行需要在运行代码后面+&

# By default this script does nothing.
echo "我有循环作用" &	#这里加&
echo "之后的程序"
exit 0

如果不加&的话,程序会在一直前台运行,假如程序没有退出设置,你将做不了任何操作,重启也没用,网上倒是有其他办法,但是我没试过

然后再执行下面代码(18.04需要,其他系统可能不需要,可以不执行,重启试一下)

sudo systemctl enable rc-local
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值