首页 > 系统相关 >centos 实现开机运行脚本

centos 实现开机运行脚本

时间:2023-11-29 18:16:05浏览次数:52  
标签:脚本 centos service sudo rc 开机 path your

Crafted by [Genie](https://marketplace.visualstudio.com/items?itemName=genieai.chatgpt-vscode)

You

centos 实现开机运行脚本

Genie

在CentOS系统中,如果您希望在开机时自动运行一个脚本,可以采用以下几种方法之一来实现:

方法1:使用cron@reboot

  1. 打开当前用户的crontab编辑界面:

    crontab -e
    
  2. 添加以下内容:

    @reboot /path/to/your/script.sh
    

    这里/path/to/your/script.sh是您的脚本文件的路径。

  3. 保存并关闭编辑器。这样,脚本会在每次系统启动后自动执行。

方法2:使用systemd服务

  1. 创建一个新的systemd服务单元文件。

    sudo vim /etc/systemd/system/your-service.service
    
  2. 在该文件中添加以下内容:

    [Unit]
    Description=Description of your service
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/path/to/your/script.sh
    
    [Install]
    WantedBy=multi-user.target
    

    请把/path/to/your/script.sh替换成您的脚本实际路径,并为您的服务提供适当的描述(Description字段)。

  3. 重新载入systemd以使新的服务生效:

    sudo systemctl daemon-reload
    
  4. 启用服务使其在开机时自动运行:

    sudo systemctl enable your-service.service
    
  5. (可选)立即启动服务进行测试:

    sudo systemctl start your-service.service
    

方法3:使用rc.local文件(对于较旧的系统)

  1. 确认/etc/rc.d/rc.local文件存在,并且具有执行权限:

    sudo chmod +x /etc/rc.d/rc.local
    
  2. 编辑该文件:

    sudo vim /etc/rc.d/rc.local
    
  3. 在文件的末尾添加您的脚本路径:

    /path/to/your/script.sh
    
  4. 保存并关闭文件。

按照上述任意一种方法操作后,您的脚本应该就能在CentOS系统开机时自动运行了。记得替换示例中的脚本路径与名称,使其指向您自己的脚本。另外,请确保您的脚本具有执行权限:

sudo chmod +x /path/to/your/script.sh

标签:脚本,centos,service,sudo,rc,开机,path,your
From: https://www.cnblogs.com/rotk2022/p/17865505.html

相关文章

  • shell脚本5---信号处理
    信号的类别信号值描述1SIGHUP挂起进程2SIGINT终止进程3SIGQUIT停止进程9SIGKILL无条件终止进程15SIGTERM优雅的终止进程17SIGSTOP无条件停止进程,但不是终止进程18SIGTSTP停止或暂停进程,但不是终止进程19SIGCONT继续运行停止的进......
  • 油猴脚本屏蔽元素
    //==UserScript==//@nameBlockZhihuTag//@namespaceyour-namespace//@version1.0//@descriptionBlockaspecifictagonZhihuwebsite//@matchhttps://www.zhihu.com/*//@grantnone//==/UserScript==(function(){......
  • centos 历史命令模糊匹配
    参考文章https://blog.csdn.net/evane1890/article/details/112863698  操作步骤  vi~/.bashrcif[[$-==*i*]]thenbind'"\e[A":history-search-backward'bind'"\e[B":history-search-forward'fi//配置......
  • centos一网打尽
    wget-i-chttp://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm#https://dev.mysql.com/downloads/yum-yinstallmysql80-community-release-el7-11.noarch.rpmyum-yinstallmysql-community-serversystemctlstartmysqld.servicesystemctlstat......
  • 【HarmonyOS】模拟器一直停留在开机页面,无法进入桌面
    ​【关键字】 模拟器,qemu-error.log,Nosounddriver 【问题背景】模拟器一直停留在开机页面,无法进入桌面​​ 【解决方案】qemu-error.log中有以下报错​检查立体声混音是否打开,或者重新安装以下音卡驱动​​......
  • Mysq8l在Centos上安装后忘记root密码如何重新设置
    场景Mysql8在Windows上离线安装时忘记root密码:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/133809385如果是在Windows上忘记密码可以参考上面。如果在Centos中安装mysql可以参考下面。CentOS7中安装Mysql8并配置远程连接和修改密码等:https://blog.csdn.net/......
  • 【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-Az
    问题描述AzureWebJob执行Powershell脚本报错 Select-AzContext:Theterm'Select-AzContext'isnotrecognizedasthenameofacmdlet,function,scriptfile,oroperableprogram.Checkthespellingofthename,orifapathwasincluded,verifythatthepa......
  • secureCRT脚本登录迈普交换机报错
    参考:http://www.lingchenliang.com/post/1799.htmlhttps://blog.csdn.net/qq_25294171/article/details/8515845832位的windowd7中在CRT6.2里手动输入IP能正常ssh登迈普交换机路由器,当使用以前的配置备份脚本如下登录就报错(注:params(3)密码(2)用户名(1)IP) cmd="/SSH2/L"&params(......
  • 20231128 - 重启Centos后无法远程连接,重启网络服务报错:Error:Failed to start LSB: Br
    1.https://blog.csdn.net/m0_74953387/article/details/1329143062.https://blog.csdn.net/weixin_45894220/article/details/130487066......
  • 关于 xshell 连不上远程 CentOS7 云服务器的问题
    前情提要:今天收到腾讯云发来短信提示我的云服务器在17:23分时异地登录,我震惊,因为今天貌似根本没登过云服务器于是我赶紧上腾讯云控制台查看,先修改密码,然后重启,最后关机在接近19点时我重新开机,再次修改了密码,然后按照网上的教程关闭定时任务,查找有没有守护进程或者挖矿病毒,无果......