Home Backend Development PHP Tutorial How to make programs start automatically when booting in Linux system

How to make programs start automatically when booting in Linux system

Mar 19, 2018 pm 03:28 PM
linux program

How to make the program automatically start when the computer is turned on in the Linux system Core tip: System services can generally start automatically when the computer is turned on. So what should you do if you want the program to start automatically when the computer is turned on under the Linux system? We know that in the Windows system "Start"-->"All Programs"-->"Start" just put a shortcut in it, but what about Linux systems? ...System services can generally start automatically when the computer is turned on. So what should you do if you want the program to start automatically when the computer is turned on under the Linux system? We know that in the Windows system, "Start" --> "All Programs" -- > Just put a shortcut in "Startup", but what about Linux system?

This is also a relatively simple problem. There are many ways to solve it. Here are three methods. Because it is a brief introduction, the specific details are not very detailed. You can read the relevant manuals through man.

1./etc/rc.local
This is the simplest method. Edit "/etc/rc.local" and enter the shell command to start the program (you need to enter the command full path), similar to "Startup" under Windows.
Use the command vi /etc/rc.local
Then add the full path of the program to be executed in the last line of the file.
For example, if a haha.sh is to be executed every time the computer is turned on, and this script is placed under /opt, then you can add a line "/opt/./haha.sh" to "/etc/rc.local", Or two lines of "cd /opt" and "./haha.sh".

2. Crontab (similar to Windows task scheduling service)
You can set the execution schedule of the program through crontab, for example, let the program be executed at 8 o'clock every day, or at 10 o'clock every Monday once.
crontab -l lists the schedule;
crontab -e edits the schedule;
crontab -d deletes the schedule;

"-l" has nothing to say, it is just a view. ;
"-e" is for editing, which is no different from vi (in fact, it is to use vi to edit a specific file);
"-d" is basically not used because it deletes all the timetables of the user. Generally, Use "-e" to edit and delete the unnecessary timetable line by line;

So how to edit it?

The format of the crontab file is: M H D m d CMD.
A 6-field field, the last CMD is the program to be executed, such as haha.sh.
M: Minutes (0-59)
H: Hours (0-23)
D: Date (1-31)
m: Month (1-12)
d: A Day of the week (0-6, 0 represents Sunday)

These five time fields are separated by spaces, and their value can be a number or multiple numbers separated by commas (or Others), if no setting is required, the default is "*".

For example, executing haha.sh at 8:05 every day is "5 8 * * * /opt/./haha.sh".

It seems that I have strayed away from the "automatic startup of the boot program", now I am back to the topic. In fact, the crontab function introduced above already has the ability to start automatically at boot. You can write a monitoring script and execute it every 5 minutes (*/5 * * * * ./haha.sh). If the program is no longer available, restart it. . (*/5) means every 5 minutes

3. Register system services
The services that come with the operating system, such as ssh, ftp, etc., are automatically started when the computer is powered on. We can also use this method This is a way to increase the "value" of the programs you develop.

For example, if I want to add an installed service as a system service, I can execute the following command:
chkconfig --add service name (First, add it as a system service. Note that there are two words in front of add. Horizontal bar)

chkconfig -leve startup level service name on
(Explanation, level 3 means starting in the command line mode, level 5 means starting in the graphical interface, on means opening)

chkconfig -leve startup level service name off            
(Explanation, off means to turn off automatic startup)
 
For example: chkconfig -level 3 mysql on                                                                                                                    
You can also use chkconfig --add service name to delete system services
******************************** *************************************************** ********If you want to check which services have been added as system services, you can use the command:
ntsysv or chkconfig --list

If you want to check which programs have been added as self-starting, you can use Command:
cat /etc/rc.local (check which program paths have been added to this file)
************************ *************************************************** *******************

The following is an example of how to add a shell script as a system service and follow the system startup:
You can see " There are many files under /etc/rc.d/init.d", and the contents of each file can be seen. They are actually some shell scripts.
The system service is started through the script file in "/etc/rc.d/init.d". We can also write our own script and place it here.
The content of the script file is also very simple, similar to this (for example, name it "hahad"):
. /etc/init.d/functions
start() {         echo "Starting my process "
          cd /opt
      ./haha.sh}
stop() {           killall haha.sh
echo "Stoped"}
After writing the script file, things are not over yet. Continue to complete the following steps: chmod +x hahad
chkconfig hahad on               #Set the hahad switch (on/off)
chkconfig --list hahad                 #You can see the services that have been registered with hahad  

At this time, all the work is completed.

Related recommendations:

How to install the Redis database under Linux and share the automatic startup script

nginx automatically starts the service after booting

Linux automatically starts MySQL

The above is the detailed content of How to make programs start automatically when booting in Linux system. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

See all articles