Home > Article > System Tutorial > Detailed explanation of how to automatically execute commands or scripts when Linux starts
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.
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:
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
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
script2.sh:
#!/bin/bash SITE="Tecmint.com" DIR=/home/gacanepa echo "$SITE rocks... add us to your bookmarks." > $DIR/file2.txt
*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
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!