Home > System Tutorial > LINUX > Detailed explanation of how to automatically execute commands or scripts when Linux starts

Detailed explanation of how to automatically execute commands or scripts when Linux starts

王林
Release: 2024-02-11 15:30:04
forward
1005 people have browsed it

I have always been curious about what happens during the process of booting a Linux system and logging in. By pressing the power button or launching a virtual machine, you initiate a chain of events that will lead you to a fully functional system, sometimes in less than a minute. The same is true when you log out or shut down your computer.

详解在 Linux 启动时,如何自动执行命令或脚本

I have always been curious about what happens during the process of starting a Linux system and logging in. By pressing the power button or launching a virtual machine, you initiate a chain of events that will lead you to a fully functional system, sometimes in less than a minute. The same is true when you log out or shut down your computer.

What’s more interesting is that you can also have the system perform specific operations when the system starts and when the user logs in or out.

In this article, we will explore the traditional methods of achieving these goals in the Linux operating system.

Note: We assume that Bash is used as the main shell for login and logout. If you are using another shell, some methods may not work. If you have other questions, please refer to the corresponding Shell documentation.

Execute Linux script at startup

There are two traditional methods to execute commands or scripts at startup:

Method #1 – Using cron tasks

In addition to the common formats (minute/hour/day/month/week), the cron scheduler also supports the @reboot directive. The parameter after this command is the absolute path of the script (the script to be executed at startup). However, there are two points to note with this approach:

  • a) The cron daemon must be running (it will usually be running), and
  • b) The script or crontab file must contain the required environment variables (if any, see StackOverflow for more details).

Method #2 – Using /etc/rc.d/rc.local

This method is also valid for systemd-based distribution Linux. However, to use this method, you need to grant execution permissions to the /etc/rc.d/rc.local file:

# chmod +x /etc/rc.d/rc.local
Copy after login

Then add the script at the bottom of this file.

The following figure illustrates how to run two sample scripts (/etc/rc.d/rc.local/home/gacanepa/script1.sh and /home/gacanepa/script2.sh) using cron tasks and rc.local respectively.
script1.sh:

#!/bin/bash
DATE=$(date +'%F %H:%M:%S')
DIR=/home/gacanepa
echo "Current date and time: $DATE" > $DIR/file1.txt
Copy after login

script2.sh:

#!/bin/bash
SITE="Tecmint.com"
DIR=/home/gacanepa
echo "$SITE rocks... add us to your bookmarks." > $DIR/file2.txt
Copy after login
详解在 Linux 启动时,如何自动执行命令或脚本

*Execute Linux script at startup *

Remember, be sure to grant execution permissions to the two sample scripts in advance:

$ chmod +x /home/gacanepa/script1.sh
$ chmod +x /home/gacanepa/script2.sh
Copy after login

Execute Linux script on login or logout

To execute the script when logging in or logging out, you need to use the ~.bash_profile and ~.bash_profile files respectively. In most cases, the latter needs to be created manually. At the bottom of each file, add the calling script code, as shown in the previous example, to achieve this functionality.

Summarize

This article mainly introduces how to execute scripts when starting, logging in and logging out of the system.

The above is the detailed content of Detailed explanation of how to automatically execute commands or scripts when Linux starts. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template