From faea83134e8406b2e5ab228efb9fd2066cd091c7 Mon Sep 17 00:00:00 2001 From: geekpi Date: Fri, 29 May 2020 08:48:49 +0800 Subject: [PATCH] translated --- ... Command or Script at Reboot or Startup.md | 159 ------------------ ... Command or Script at Reboot or Startup.md | 159 ++++++++++++++++++ 2 files changed, 159 insertions(+), 159 deletions(-) delete mode 100644 sources/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md create mode 100644 translated/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md diff --git a/sources/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md b/sources/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md deleted file mode 100644 index 91e97dbbaf..0000000000 --- a/sources/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md +++ /dev/null @@ -1,159 +0,0 @@ -[#]: collector: (lujun9972) -[#]: translator: (geekpi) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) -[#]: subject: (How to Execute a Command or Script at Reboot or Startup) -[#]: via: (https://www.2daygeek.com/execute-run-linux-scripts-command-at-reboot-startup/) -[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/) - -How to Execute a Command or Script at Reboot or Startup -====== - -Well known services on Linux can be added on boot without any problems. - -For example, if you want to add Apache Httpd **[service on boot][1]**, you can do this with the help of the chkconfig and systemctl command. - -Sometimes you need to add a custom script or command or service on boot, and how to do it? - -You can do this using the below three methods. - -In this article, we will show you how to use these methods with examples. - -### Method-1: How to Run Script or Command at Reboot or Startup Using /etc/rc.d/rc.local File - -The **“/etc/rc.local”** file is traditionally executed after all normal computer services have been started at the end of the process of switching to a multiuser runlevel. - -This method also works on the systemd system. - -You need to add the location of your script to the “/etc/rc.d/rc.local” file to run on boot. - -Make sure the file has permission to run. - -``` -# chmod +x /etc/rc.d/rc.local -``` - -To demonstrate this, we are going to create a simple sample script. You can create any script as needed. - -``` -# vi /opt/scripts/run-script-on-boot.sh - -#!/bin/bash -date > /root/on-boot-output.txt -hostname > /root/on-boot-output.txt -``` - -Once the script is ready, set the executable permission. - -``` -# chmod +x /opt/scripts/run-script-on-boot.sh -``` - -Finally add the script to the bottom of the file. - -``` -# vi /etc/rc.d/rc.local - -/opt/scripts/run-script-on-boot.sh -``` - -**[Restart your system][2]** to check this. - -``` -# reboot -``` - -### Method-2: How to Execute a Command or Script at Reboot or Startup Using the crontab - -cron executes scheduled jobs automatically in the backend at a specific time. - -This can be easily accomplished using a special string called **“@reboot”** with **[cron job][3]**. - -@reboot is a special string and allows the user to run any command or script at startup (boot time). - -This example runs the “/opt/scripts/run-script-on-boot.sh” file on the system restart. - -We are going to use the same script as above. - -To do so, just add the following entry in the crontab file. - -``` -# crontab -e - -@reboot /opt/scripts/run-script-on-boot.sh -``` - -Restart your system to check this. - -``` -# reboot -``` - -### Method-3: How to Run a Command or Script at Reboot or Startup Using the systemd service unit - -This method only works on the systemd system. This method is very straightforward. - -We are going to use the same script above to demonstrate this. - -To do so, you need to create a systemd startup script and place it in the **“/etc/systemd/system/”** directory. - -This is our sample systemd startup unit script. - -``` -# vi sample-on-boot-script.service - -[Unit] -Description=Run a Custom Script at Startup -After=default.target - -[Service] -ExecStart=/opt/scripts/run-script-on-boot.sh - -[Install] -WantedBy=default.target -``` - -Once you place the unit script in the systemd location, run the following command to update the systemd configuration files and enable the service. - -``` -# systemctl daemon-reload -# systemctl enable sample-on-boot-script.service -``` - -Restart your system to check this. - -``` -# reboot -``` - -### Bonus Tips: - -If you want to run a script in the background, you need to add the trailing ampersand “&” symbol. - -``` -/Path/To/My_Script & -``` - -If you want to run the command as a different user, use the following format. - -``` -su - $USER -c /Path/To/My_Script -``` - --------------------------------------------------------------------------------- - -via: https://www.2daygeek.com/execute-run-linux-scripts-command-at-reboot-startup/ - -作者:[Magesh Maruthamuthu][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://www.2daygeek.com/author/magesh/ -[b]: https://github.com/lujun9972 -[1]: https://www.2daygeek.com/enable-disable-services-on-boot-linux-chkconfig-systemctl-command/ -[2]: https://www.2daygeek.com/6-commands-to-shutdown-halt-poweroff-reboot-the-linux-system/ -[3]: https://www.2daygeek.com/linux-crontab-cron-job-to-schedule-jobs-task/ diff --git a/translated/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md b/translated/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md new file mode 100644 index 0000000000..19feaf1fc2 --- /dev/null +++ b/translated/tech/20200525 How to Execute a Command or Script at Reboot or Startup.md @@ -0,0 +1,159 @@ +[#]: collector: (lujun9972) +[#]: translator: (geekpi) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (How to Execute a Command or Script at Reboot or Startup) +[#]: via: (https://www.2daygeek.com/execute-run-linux-scripts-command-at-reboot-startup/) +[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/) + +如何在重启或启动时执行命令或脚本 +====== + +总所周知 Linux 可以在启动时添加服务。 + +例如,如果要在**[启动时添加][1]** Apache Httpd 服务,你可以在 chkconfig 和 systemctl 命令的帮助下完成此操作。 + +有时你需要在启动时添加自定义脚本、命令或服务,该怎么做? + +你可以使用以下三种方法来做到。 + +在本文中,我们将通过示例向你展示如何使用这些方法。 + +### 方法 1:如何使用 /etc/rc.d/rc.local 文件在重启或启动时运行脚本或命令 + +传统上,**“/etc/rc.local”** 文件是在切换到多用户运行级别的过程结束时启动所有正常的计算机服务之后执行的。 + +此方法也适用于 systemd 系统。 + +你需要将脚本位置添加到 “/etc/rc.d/rc.local” 文件中以在启动时运行。 + +确保文件有运行权限。 + +``` +# chmod +x /etc/rc.d/rc.local +``` + +为了演示,我们将创建一个简单的示例脚本。你可以根据需要创建任何脚本。 + +``` +# vi /opt/scripts/run-script-on-boot.sh + +#!/bin/bash +date > /root/on-boot-output.txt +hostname > /root/on-boot-output.txt +``` + +脚本完成后,设置可执行权限。 + +``` +# chmod +x /opt/scripts/run-script-on-boot.sh +``` + +最后,将该脚本添加到文件底部。 + +``` +# vi /etc/rc.d/rc.local + +/opt/scripts/run-script-on-boot.sh +``` + +**[重启系统][2]**进行检查。 + +``` +# reboot +``` + +### 方法 2:如何使用 crontab 在重启或启动时执行命令或脚本 + +cron 在特定时间在后台自动执行计划的作业。 + +可以在 **[cron 任务][3]**中使用特殊的字符串 **“@reboot”** 来完成。 + +@reboot 是一个特殊的字符串,它允许用户在启动时运行任何命令或脚本。 + +此示例在系统重启时运行 “/opt/scripts/run-script-on-boot.sh” 文件。 + +我们将使用与上面相同的脚本。 + +为此,只需在 crontab 文件中添加以下条目。 + +``` +# crontab -e + +@reboot /opt/scripts/run-script-on-boot.sh +``` + +重启系统进行检查。 + +``` +# reboot +``` + +### 方法 3:如何使用 systemd 服务单元在重启或启动时运行命令或脚本 + +此方法仅适用于 systemd 系统。该方法非常简单。 + +我们将使用上面相同的脚本进行演示。 + +为此,你需要创建一个 systemd 启动脚本并将其放在 **”/etc/systemd/system/“** 目录中。 + +这是我们的示例 systemd 启动单元脚本。 + +``` +# vi sample-on-boot-script.service + +[Unit] +Description=Run a Custom Script at Startup +After=default.target + +[Service] +ExecStart=/opt/scripts/run-script-on-boot.sh + +[Install] +WantedBy=default.target +``` + +将单元脚本放置在 systemd 所在位置后,运行以下命令更新 systemd 配置文件并启用服务。 + +``` +# systemctl daemon-reload +# systemctl enable sample-on-boot-script.service +``` + +重启系统进行检查。 + +``` +# reboot +``` + +### 额外提示: + +如果你想在后台运行脚本,你需要在最后加上 “&” 符号 + +``` +/Path/To/My_Script & +``` + +如果你想以不同用户运行命令,使用以下格式。 + +``` +su - $USER -c /Path/To/My_Script +``` + +-------------------------------------------------------------------------------- + +via: https://www.2daygeek.com/execute-run-linux-scripts-command-at-reboot-startup/ + +作者:[Magesh Maruthamuthu][a] +选题:[lujun9972][b] +译者:[geekpi](https://github.com/geekpi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.2daygeek.com/author/magesh/ +[b]: https://github.com/lujun9972 +[1]: https://www.2daygeek.com/enable-disable-services-on-boot-linux-chkconfig-systemctl-command/ +[2]: https://www.2daygeek.com/6-commands-to-shutdown-halt-poweroff-reboot-the-linux-system/ +[3]: https://www.2daygeek.com/linux-crontab-cron-job-to-schedule-jobs-task/