EC2 Ubuntu 开机启动项

方法一:编辑rc.loacl脚本

rc.local是一个常用的开机启动脚本,它会在是在系统初始化级别脚本运行后运行,因此可以比较安全地在里面添加那些在系统启动之后执行的脚本。

rc.local的路径是/etc/rc.local

#!/bin/bash
# 
# 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.

echo Hello World   

mkdir /home/ubuntu/rew

echo asdasda >> /home/ubuntu/try.log

cd /home/ubuntu
./new.sh

exit 0

把你想添加的启动项放在exit 0之前

** 注意 **

  1. 在Ubuntu 16以后系统没有/etc/rc.local了,需要新建一个rc.local文件
    touch /etc/rc.local
    sudo vi /etc/rc.local
    
  2. 把rc.local添加在启动项里面
    sudo chown root:root /etc/rc.local
    sudo chmod 755 /etc/rc.local
    sudo systemctl enable rc-local.service
    

常见问题

  1. The unit files have no installation config (WantedBy, RequiredBy, Also, Alias settings in the [Install] section, and DefaultInstance for template units). This means they are not meant to be enabled using systemctl.

    编辑/etc/systemd/system/py_service.service文件

	[Install]
	WantedBy=multi-user.target
	
	[Unit]
	After=network.target
	
	[Service]
	Type=simple
	ExecStart=(...)service.py
	ExecReload=(...)service.py
	Restart=always

开启service

	sudo systemctl daemon-reload
	systemctl enable py_service.service

方法二: 添加 /etc/init.d

将你的启动脚本复制到 /etc/init.d目录

	mv test /etc/init.d/test
	sudo chmod 755 /etc/init.d/test

将该脚本放倒启动列表中去

	cd .etc/init.d
	sudo update-rc.d test defaults 95

其中数字95是脚本启动的顺序号。当有多个脚本的时候,可以设定启动的顺序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值