Create one of the simplest Linux random start services

Source: Internet
Author: User

Create one of the simplest Linux random start services? XIAOXIA[PG]

Create one of the simplest Linux random start services
There are many ways to get Linux to automatically run scripts or programs that you write, but most of them are written in/etc/rc.d/rc.local or/etc/rc.local, which can be run randomly, but not flexible enough. You cannot start, shut down, or restart a process using service commands or invoking scripts under init.d like Mysql,apache. For example

Service MySQL Restartservice apache2 stop

Or

/etc/init.d/mysql restart/etc/init.d/apache2 stop

Because of the different Linux distributions, the background service is not handled much the same way, so the following Ubuntu system as an example to see how to write a simple random start service.

Prepare a program that needs to be started randomly, such as/root/proxy.py, and set the executable property to ensure that it can be executed directly by entering an absolute path.

[Email protected]:~# chmod +x proxy.py
[Email protected]:~#/root/proxy.py
Loaded 0 hosts.
Proxy over h2.edu.bj.ie.sogou.com.
Please set your browser ' s proxy to (':: ', 1998).

Write a startup control script, take proxy as an example, create a/etc/init.d/proxy text file, and enter the following content:

#!/bin/sh

Case "$" in
Start
Start-stop-daemon--start--background--exec/root/proxy.py
;;
Stop
Start-stop-daemon--stop--name proxy.py
Esac
This is a simple shell script, case: In is used to perform different operations depending on the invocation parameters, Start-stop-daemon is a program that can manage the daemon process, and to view its detailed description, you can run the man Start-stop-daemon. Start, use--exec to specify the file to execute, stop, use--name according to the process name to use Killall to end the matching process.

Next, set the script file properties to set the executable tag.

[Email protected]:~# chmod 755/etc/init.d/proxy

This way, you can use the service command to start and close the process, such as the boot process as follows:

[Email protected]:~# Service Proxy start
[Email protected]:~# PS Aux|grep Proxy
Root 353 1.4 1.9 8644 5212? S 09:50 0:00/usr/bin/python/root/proxy.py
Root 355 0.0 0.2 1900 596 pts/0 s+ 09:50 0:00 grep--color=auto Proxy
Close the process,

[Email protected]:~# Service Proxy Stop
[Email protected]:~# PS aux |grep Proxy
Root 365 0.0 0.2 1900 592 pts/0 s+ 09:51 0:00 grep--color=auto Proxy
Here, the Process Control script for a Linux service has been written, but a step is required to implement a random startup.
When Linux is powered on, it does not run all the scripts under/ETC/INIT.D directly, but instead executes the scripts under/ETC/RC$RUNLEVEL.D according to different runlevel. Here the runlevel is used to differentiate the system's operation (such as single-user runlevel, multimedia desktop runlevel, server runlevel are different).

In Ubuntu, you can use UPDATE-RC.D to install/etc/init.d/proxy into various runlevel. For more instructions on UPDATE-RC.D, see Man Update-rc.d.

[Email protected]:~# update-rc.d Proxy defaults 99
Update-rc.d:warning:/etc/init.d/proxy Missing LSB information
Update-rc.d:see Adding system Startup For/etc/init.d/proxy ...
/etc/rc0.d/k99proxy. /init.d/proxy
/etc/rc1.d/k99proxy. /init.d/proxy
/etc/rc6.d/k99proxy. /init.d/proxy
/etc/rc2.d/s99proxy. /init.d/proxy
/etc/rc3.d/s99proxy. /init.d/proxy
/etc/rc4.d/s99proxy. /init.d/proxy
/etc/rc5.d/s99proxy. /init.d/proxy
UPDATE-RC.D is followed by three parameters, the script name under/ETC/INIT.D, the default installation mode, and the priority of the operation. The higher the number of priorities, the more late the operation, and here we put the service we wrote in the last run.

If you want to uninstall a randomly started service, perform

Update-rc.d-f Proxy Remove

The warning message was prompted when the UPDATE-RC.D was installed because the/etc/init.d/proxy we wrote was too primitive to provide even the LSB information.

Update-rc.d:warning:/etc/init.d/proxy Missing LSB information
Update-rc.d:see

Just make a few minor changes and you can avoid that warning. As follows:

#!/bin/sh
# # # BEGIN INIT INFO
# Provides:proxy
# Required-start: $remote _fs
# Required-stop: $remote _fs
# Default-start:2 3 4 5
# default-stop:0 1 6
# Short-description:start or stop the HTTP Proxy.
# # # END INIT INFO

Case "$" in
Start
Start-stop-daemon--start--background--exec/root/proxy.py
;;
Stop
Start-stop-daemon--stop--name proxy.py
Esac
In this case, one of the simplest random start-up services is written, and it looks like the article is quite long, but in fact there are just a few commands.
At the next boot-up, proxy.py will be automatically run as the root user.

In Jiangsu, Zhejiang and Shanghai to find technical work, can provide work trouble contact me

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.