在Linux的启动时间中启动脚本的最佳方法是什么 [英] what is the best way to start a script in boot time on linux

查看:109
本文介绍了在Linux的启动时间中启动脚本的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启动系统启动并寻找最佳方法时使用的脚本,我的方法是:

I want to start a script I have on when the system start and looking for the best way, my way is:

  • vi/etc/systemd/system/myscript.service

  • vi /etc/systemd/system/myscript.service

[Service]
Type=simple
ExecStart=/usr/bin/myscript
CPUSchedulingPolicy=rr
CPUSchedulingPrioty=27
[Install]
WantedBy=multi-user.target graphical.target

  • systemctl daemon-reload; systemctl enable myscript; systemctl start rmyscript

    它工作正常,但只是想知道是否还有另一种更好的方法.

    it's working good but just wondered if there another and better way.

    推荐答案

    有两种方法可以实现此目的,但是以下任何情况都需要root特权.要获得root用户权限,请打开一个终端并运行以下命令:

    There are a couple of ways to achieve this, but you will need root privileges for any the following. To get root, open a terminal and run the command:

    sudo su
    

    ,命令提示符将变为'#',表示终端会话具有root特权.

    and the command prompt will change to '#' indicating that the terminal session has root privileges.

    替代#1.添加初始化脚本

    /etc/init.d/myscript中创建一个新脚本:

    vi /etc/init.d/myscript
    

    (显然不必将其称为"myscript".)在此脚本中,执行您想做的所有事情.也许只需运行您提到的脚本即可:

    (Obviously it doesn't have to be called "myscript".) In this script, do whatever you want to do. Perhaps just run the script you mentioned:

    #!/bin/sh
    /path/to/my/script.sh
    

    使其可执行:

    chmod ugo+x /etc/init.d/myscript
    

    配置初始化系统以在启动时运行此脚本:

    Configure the init system to run this script at startup:

    update-rc.d myscript defaults
    

    替代#2.将命令添加到/etc/rc.local

    vi /etc/rc.local
    

    内容如下:

    # This script is executed at the end of each multiuser runlevel
    /path/to/my/script.sh || exit 1   # Added by me
    exit 0
    

    替代#3.添加新贵职位

    创建/etc/init/myjob.conf:

    vi /etc/init/myjob.conf
    

    具有以下内容:

    description "my job"
    start on startup
    task
    exec /path/to/my/script.sh
    

    顺便说一句:

    如果您可以编辑crontab (crontab -e)并创建如下条目,则无需成为root用户:

    You don't need to be root if you can edit your crontab (crontab -e) and create an entry like this:

    @reboot /path/to/script.sh
    

    这样,您可以以普通用户身份运行它. @reboot只是表示它在计算机启动时运行(不一定只是在重新启动时运行).

    This way, you can run it as a regular user. @reboot just means it's run when the computer starts up (not necessarily just when it's rebooted).

    这篇关于在Linux的启动时间中启动脚本的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆