linux新增开机启动脚本(代码片段)

xhoufei2010 xhoufei2010     2022-12-04     423

关键词:

Linux 新增开机启动脚本

1、说明

在linux 环境下,新增开机自启动脚本

2、环境说明

硬件环境: NVIDIA 开发板
软件环境: Ubuntu 18

3、操作

3.1 增加开机脚本

 sudo  vi /etc/init.d/test.sh

其中脚本的内容如下

#!/bin/sh
### BEGIN INIT INFO
# Provides:          test.sh
# Required-Start:    $remote_fs
# Required-Stop:
# Should-Start:      
# Default-Start:     2 3 4 5
# Default-Stop:
# X-Interactive:     true
# Short-Description: Just test script
### END INIT INFO




exit 0

3.2 将脚本添加到开机服务

cd  /etc/init.d/
sudo chmod 777 test.sh
sudo update-rc.d test.sh defaults  98

其中 98 为开机序号,范围0-99,数字越大,开机启动的顺序越慢。
修改之后,重启即可成功

sudo  reboot

3.3 效果查看

开机重启后,可以查看每次开机都有 ~/test_result.txt 文件产生,查看文件

sky@ubuntu:~$ cat test_result.txt 
just for a automatic script

3.4 卸载方法

cd /etc/init.d
sudo update-rc.d -f test remove

4 延伸说明

4.1 自启动脚本都有哪些

系统自带的启动脚本以及顺序,都存放于 /etc/rcN.d/
查看系统使用的是哪个rc 文件夹

sky@ubuntu:~$ runlevel 
N 5

说明采用的是 rc5.d 文件夹,查看文件夹里面的内容

ls /etc/rc5.d/ -al
total 16
drwxr-xr-x   2 root root  4096 Sep 18 17:24 .
drwxr-xr-x 133 root root 12288 Oct 13 10:09 ..
lrwxrwxrwx   1 root root    27 Sep 17 14:41 K01speech-dispatcher -> ../init.d/speech-dispatcher
lrwxrwxrwx   1 root root    15 Sep 17 14:41 S01acpid -> ../init.d/acpid
lrwxrwxrwx   1 root root    17 Sep 17 14:41 S01anacron -> ../init.d/anacron
lrwxrwxrwx   1 root root    16 Sep 17 14:41 S01apport -> ../init.d/apport
lrwxrwxrwx   1 root root    22 Sep 17 14:41 S01avahi-daemon -> ../init.d/avahi-daemon
lrwxrwxrwx   1 root root    24 Sep 17 16:57 S01binfmt-support -> ../init.d/binfmt-support
lrwxrwxrwx   1 root root    19 Sep 17 14:41 S01bluetooth -> ../init.d/bluetooth
lrwxrwxrwx   1 root root    26 Sep 17 14:41 S01console-setup.sh -> ../init.d/console-setup.sh
lrwxrwxrwx   1 root root    14 Sep 17 14:41 S01cron -> ../init.d/cron
lrwxrwxrwx   1 root root    14 Sep 17 14:41 S01cups -> ../init.d/cups
lrwxrwxrwx   1 root root    22 Sep 17 14:41 S01cups-browsed -> ../init.d/cups-browsed
lrwxrwxrwx   1 root root    14 Sep 17 14:41 S01dbus -> ../init.d/dbus
lrwxrwxrwx   1 root root    14 Sep 17 14:41 S01gdm3 -> ../init.d/gdm3
lrwxrwxrwx   1 root root    21 Sep 17 14:41 S01grub-common -> ../init.d/grub-common
lrwxrwxrwx   1 root root    20 Sep 17 14:41 S01irqbalance -> ../init.d/irqbalance
lrwxrwxrwx   1 root root    20 Sep 17 14:41 S01kerneloops -> ../init.d/kerneloops
lrwxrwxrwx   1 root root    14 Sep 18 17:24 S01nmbd -> ../init.d/nmbd
lrwxrwxrwx   1 root root    17 Sep 17 14:41 S01openvpn -> ../init.d/openvpn
lrwxrwxrwx   1 root root    18 Sep 17 14:41 S01plymouth -> ../init.d/plymouth
lrwxrwxrwx   1 root root    37 Sep 17 14:41 S01pulseaudio-enable-autospawn -> ../init.d/pulseaudio-enable-autospawn
lrwxrwxrwx   1 root root    15 Sep 17 14:41 S01rsync -> ../init.d/rsync
lrwxrwxrwx   1 root root    17 Sep 17 14:41 S01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx   1 root root    21 Sep 18 17:24 S01samba-ad-dc -> ../init.d/samba-ad-dc
lrwxrwxrwx   1 root root    15 Sep 17 14:41 S01saned -> ../init.d/saned
lrwxrwxrwx   1 root root    14 Sep 18 17:24 S01smbd -> ../init.d/smbd
lrwxrwxrwx   1 root root    23 Sep 17 14:41 S01spice-vdagent -> ../init.d/spice-vdagent
lrwxrwxrwx   1 root root    13 Sep 17 15:11 S01ssh -> ../init.d/ssh
lrwxrwxrwx   1 root root    29 Sep 17 14:41 S01unattended-upgrades -> ../init.d/unattended-upgrades
lrwxrwxrwx   1 root root    15 Sep 17 14:41 S01uuidd -> ../init.d/uuidd
lrwxrwxrwx   1 root root    18 Sep 17 14:41 S01whoopsie -> ../init.d/whoopsie

按照字母和数字顺序执行

2021-10-14(代码片段)

Linux新增开机启动脚本1、说明2、环境说明3、操作3.1增加开机脚本3.2将脚本添加到开机服务3.3效果查看3.4卸载方法4延伸说明4.1自启动脚本都有哪些1、说明在linux环境下,新增开机自启动脚本2、环境说明硬件环境:NVIDIA开... 查看详情

linux开机自启应用&开机执行脚本&监听端口应用挂掉了执行启动脚本(代码片段)

Linux开机自启应用Linux开机执行脚本Linux监听端口应用挂掉了执行启动脚本linux开机自启背景目前要部署一个springboot框架的jar包,实现开机启动项目或者应用挂掉了执行启动脚本在root目录下有一个启动项目的脚本:app_start.shapp_star... 查看详情

linux-简易shell脚本编写,以开机日志为例,编写一个用于查看linux进程启动的脚本(代码片段)

...或日志,今天这篇简短的文章就是介绍编译一个Linux开机启动脚本,记录各个进程模块的启动时间以及顺序。准备任务,在Linux服务器下编写一个shell脚本,脚本的可以存在任何你可以访问的目录,笔者存在根... 查看详情

linux开机自启动命令及脚本(代码片段)

目的提示:有些时候,担心某些服务,启动命令不生效,或不想每次都输入复杂的命令例如:docker镜像封装的命令:启动Xvfb有的时候不生效!1)在此目录创建脚本Xvfb.sh2)添加内容:vimXvfb.sh3ÿ... 查看详情

linux之开机自启动服务(两种方式)(代码片段)

linux开机启动自启动脚本两种方式先要了解一下系统启动运行级别,请看这篇文章链接:Linux之运行级别rc.local方式1首先创建一个要自启动的脚本vi/etc/scripts/createFile.sh#!/bin/bash#开机创建一个文件夹mkdir/opt/ccc2.给予执行权限chmod77... 查看详情

centos7开机启动脚本与命令后台运行(代码片段)

一、&在Linux命令后加上 & 可以在后台运行 二、nohup对SIGHUP信号免疫,对SIGINT信号不免疫,可用shopt|grephup查看。当关闭终端时,shell默认会发送SIGHUP信号给与该终端关联的进程,从而导致其他进程跟随终端退出。n... 查看详情

centos7.5linux版本设置jar包开机自启动(代码片段)

一 开机自启动1.1jar包1.2编写启动shell脚本编写脚本ziqidong.sh,内容如下:#!/bin/bashexportJAVA_HOME=/usr/local/java/jdk1.8.0_171exportJRE_HOME=/usr/local/java/jdk1.8.0_171/jreexportCLASSPATH=.:$JAVA_HOME/lib/dt.j 查看详情

fedora设置开机自启动脚本rc-local.service服务(代码片段)

前言自从很多Linux发行版采用systemd来管理服务和开机启动程序后,开机速度大大加快。同时老的开机启动脚本/etc/rc.local默认不再被支持。然而很多时候rc.local自启动脚本带来的方便和快捷胜过了速度需求,此时我们希望... 查看详情

markdown蝙蝠脚本后台开机启动(代码片段)

查看详情

开机自启动程序脚本(部署)(代码片段)

基于shell脚本的Linux脚本开机自启的几种方法系统启动时需要加载的配置文件/etc/profile、/root/.bash_profile/etc/bashrc、/root/.bashrc/etc/profile.d/*.sh、/etc/profile.d/lang.sh/etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)一、修改开机启动... 查看详情

linux开机自动执行命令或自动启动程序(rc.local)(代码片段)

linux开机的最后会执行/etc/rc.local,因此可以在此脚本里面添加shell命令自动执行或者自动启动某个进程。比如自动输出信息:#!/bin/sh-e##rc.local##Thisscriptisexecutedattheendofeachmultiuserrunlevel.#Makesurethatthescriptwill"exit0" 查看详情

ubuntu开机自启动(代码片段)

开机自启动脚本如果要添加为开机启动执行的脚本文件,可先将脚本复制或者软连接到/etc/init.d/目录下,然后用:update-rc.dxxxdefaultsNN命令(NN为启动顺序),将脚本添加到初始化执行的队列中去。注意如果脚本需要用到网络,则NN需... 查看详情

自定义开机启动脚本(代码片段)

如果你有一些自定义的脚本或服务需要开机启动,那么就可以写入到/etc/rc.local文件中,然后chmod+x/etc/rc.local赋予运行权限。/etc/rc.local文件的内容:但是这个文件在Centos7中已经不推荐被使用了,它的存在只是为了兼容Centos5和Centos... 查看详情

centos配置开机启动脚本启动docker容器(代码片段)

原文:Centos配置开机启动脚本启动docker容器Centos配置开机启动脚本启动docker容器Intro我们的Centos服务器上部署了好多个docker容器,因故重启的时候就会导致还得手动去手动重启这些docker容器,为什么不写个脚本自动重启呢,于是就... 查看详情

zk开机自动启动脚本(代码片段)

[Unit]Description=ZookeeperserviceAfter=network.target[Service]User=wwwGroup=wwwSyslogIdentifier=zookeeperExecStart=/usr/local/zookeeper/bin/zkServer.shstartExecStop=/usr/local/zookeeper/bin/zkServer. 查看详情

linux(centos)下设置nginx开机自动启动(2个办法)(代码片段)

首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: 1vim/etc/init.d/nginx在脚本中添加如下命令:#!/bin/sh##nginx-thisscriptstartsandstopsthenginxdaemon##chkconfig:-8515#description:NGINXisanHTTP(S)server,HTTP(S)rev 查看详情

centos7如何设置脚本开机自启动?(代码片段)

1.在需要设置为开机启动的脚本中加入必须的chkconfig启动脚本规范,如下:#!/bin/bash#chkconfig:23458012#description:autostartshellscript2.使用chkconfig将脚本加入自动启动列表#mvyour.sh/etc/rc.d/init.d#chkconfig/etc/rc.d/init.d/your.sh 查看详情

树莓派进阶之路(033)-开机启动自定义脚本

因为需求需要,树莓派开机需要自动运行一些代码和脚本,并且需要对网络是否正常进行监测,所以需要做带网络监测的自启动服务。参考了一下文档:Linux开机启动程序详解Linux中设置服务自启动的三种方式,linux服务的开机启... 查看详情