"All Programs"-->Just put a shortcut in "Startup", 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?"/> "All Programs"-->Just put a shortcut in "Startup", 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?">

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

小云云
小云云Original
2018-03-19 15:28:003671browse

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:
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