From 1d63bddba169c5d33f184379450bb2c56b418f54 Mon Sep 17 00:00:00 2001 From: darksun Date: Sat, 9 Dec 2017 21:45:18 +0800 Subject: [PATCH] =?UTF-8?q?update=20at=202017=E5=B9=B4=2012=E6=9C=88=2009?= =?UTF-8?q?=E6=97=A5=20=E6=98=9F=E6=9C=9F=E5=85=AD=2021:45:18=20CST?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...nd Scripts at Reboot & Startup in Linux.md | 64 ---------------- ...nd Scripts at Reboot & Startup in Linux.md | 75 +++++++++++++++++++ 2 files changed, 75 insertions(+), 64 deletions(-) delete mode 100644 sources/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md create mode 100644 translated/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md diff --git a/sources/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md b/sources/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md deleted file mode 100644 index 04c922aa41..0000000000 --- a/sources/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md +++ /dev/null @@ -1,64 +0,0 @@ -translating by lujun9972 -Executing Commands and Scripts at Reboot & Startup in Linux -====== -There might arise a need to execute a command or scripts at reboot or every time when we start our system. So how can we do that, in this tutorial we are going to discuss just that. We will discuss how we can make our CentOS/RHEL and Ubuntu systems to execute a command or scripts at reboot or at system startup using two different methods. Both the methods are tested and works just fine, - -### Method 1 – Using rc.local - -In this method, we will use ‘rc.local’ file located in ‘/etc/’ to execute our scripts and commands at startup. We will make an entry to execute the script in the file & every time when our system starts, the script will be executed. - -But we will first provide the permissions to make the file /etc/rc.local executable, - -$ sudo chmod +x /etc/rc.local - -Next we will add the script to be executed in the file, - -$ sudo vi /etc/rc.local - -& at the bottom of file, add the entry - -sh /root/script.sh & - -Now save the file & exit. Similarly we can execute a command using rc.local file but we need to make sure that we mention the full path of the command. To locate the full command path, run - -$ which command - -For example, - -$ which shutter - -/usr/bin/shutter - -For CentOS, we use file ‘/etc/rc.d/rc.local’ instead of ‘/etc/rc.local’. We also need to make this file executable before adding any script or command to the file. - -Note:- When executing a script at startup, make sure that the script ends with ‘exit 0’. - -( Recommended Read : ) - -### Method 2 – Crontab method - -This method is the easiest method of the two methods. We will create a cron job that will wait for 90 seconds after system startup & then will execute the command or script on the system. - -To create a cron job, open terminal & run - -$ crontab -e - -& enter the following line , - -@reboot ( sleep 90 ; sh \location\script.sh ) - -where \location\script.sh is the location of script to be executed. - -So this was our tutorial on how to execute a script or a command when system starts up. Please leave your queries, if any , using the comment box below - --------------------------------------------------------------------------------- - -via: http://linuxtechlab.com/executing-commands-scripts-at-reboot/ - -作者:[Shusain][a] -译者:[lujun9972](https://github.com/lujun9972) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:http://linuxtechlab.com/author/shsuain/ diff --git a/translated/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md b/translated/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md new file mode 100644 index 0000000000..107caa6bd9 --- /dev/null +++ b/translated/tech/20170918 Executing Commands and Scripts at Reboot & Startup in Linux.md @@ -0,0 +1,75 @@ +在 Linux 启动或重启时执行命令与脚本 +====== +有时可能会需要在重启时或者每次系统启动时运行某些命令或者脚本。我们要怎样做呢?本文中我们就对此进行讨论。 我们会用两种方法来描述如何在 CentOS/RHEL 以及 Ubuntu 系统上做到重启或者系统启动时执行命令和脚本。 两种方法都通过了测试。 + +### 方法 1 – 使用 rc.local + +这种方法会利用 `/etc/` 中的 `rc.local` 文件来在启动时执行脚本与命令。我们在文件中加上一行 l 爱执行脚本,这样每次启动系统时,都会执行该脚本。 + +不过我们首先需要为 `/etc/rc.local` 添加执行权限, + +```shell +$ sudo chmod +x /etc/rc.local +``` + +然后将要执行的脚本加入其中, + +```shell +$ sudo vi /etc/rc.local +``` + +在文件最后加上 + +```shell +sh /root/script.sh & +``` + +然后保存文件并退出。 +使用 `rc.local` 文件来执行命令也是一样的,但是一定要记得填写命令的完整路径。 像知道命令的完整路径可以运行 + +```shell +$ which command +``` + +比如, + +```shell +$ which shutter +/usr/bin/shutter +``` + +如果是 CentOS,我们修改的是文件 `/etc/rc.d/rc.local` 而不是 `/etc/rc.local`。 不过我们也需要先为该文件添加可执行权限。 + +注意:- 启动时执行的脚本,请一定保证是以 `exit 0` 结尾的。 + +### 方法 2 – 使用 Crontab + +该方法最简单了。我们创建一个 cron 任务,这个任务在系统启动后等待 90 秒,然后执行命令和脚本。 + +要创建 cron 任务,打开终端并执行 + +```shell +$ crontab -e +``` + +然后输入下行内容, + +```shell +@reboot ( sleep 90 ; sh \location\script.sh ) +``` + +这里 `\location\script.sh` 就是待执行脚本的地址。 + +我们的文章至此就完了。如有疑问,欢迎留言。 + +-------------------------------------------------------------------------------- + +via: http://linuxtechlab.com/executing-commands-scripts-at-reboot/ + +作者:[Shusain][a] +译者:[lujun9972](https://github.com/lujun9972) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]:http://linuxtechlab.com/author/shsuain/