程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Ubuntu boot Python script

編輯:Python

matters needing attention : If the script is not executed , First of all, we should consider the issue of execution permission , Check whether the script and corresponding files have execution permission , If there is no need to grant execution permission .

Method 1:

1. Write a shell Script files , for example python_auto.sh, The contents of the document :

2. To write start.py file , It performs the operations you need the server to start

3. stay /etc/profile  In this file , add to shell Script file path , for example :

It will be automatically executed when the server is powered on /etc/profile The code in

notes : linux All the service programs started randomly are in the /etc/init.d In this folder , All the files inside are script files , In addition to /etc And in this folder, there are other things like rc1.d, rc2.d Until rc6.d Folder , These are all linux Different runlevel, We usually enter X windows The running level of multi-user is the second 5 level , That is to say rc5.d, The script file in this folder is to run the second 5 The service program to be started at random at level 1 . It should be noted that , At every rc (1-6).d The files in the folder are actually /etc/init.d A soft connection to the files under the folder , in other words , stay /etc/init.d Under the folder are all the service programs , And each rc(1-6).d Only link itself to start the corresponding service program needed ! also One thing to note is that , stay rc5.d in , The name of each link is S perhaps K At the beginning ,S At the beginning, it means that the system should be started randomly ,K The first one doesn't start randomly . this sample , You can know , If I want any service to start randomly , Just put the first letter of its name K Change to S That's all right. , Of course , hold S Change to K after , This service can't be started randomly .Ubuntu System operation level : 0   System shutdown status 1   Single user or system maintenance status 2~5 Multi user status 6   Restart  

Run the program automatically when you log in : When the user logs in ,bash First, automatically perform the global login established by the system administrator script :/ect/profile. then bash Search one of the three special files in order in the user's home directory :/.bash_profile、/.bash_login、 /.profile, But only the first one found . therefore , Just add a command to the above file according to the actual needs to realize the automatic running of some programs when the user logs in . Run the program automatically when you log out : When logging out ,bash Automatically execute individual logout scripts /.bash_logout.

example :

/opt/abc.sh 

#!/bin/sh
nohup /usr/bin/python2 /opt/agent.py >/dev/null 2>&1 &

/etc/profile Finally, add

bash /opt/abc.sh

Method 2:

stay /etc/rc.local Add a line to the file :

/usr/bin/python2.7 /usr/local/qlya/timing_insert.py & 

& The symbol is to let the script run in the background , Do not display results on the command line

Method 3:

Write a service, And let service Boot up (CentOS 7)

/usr/lib/systemd/system/  There are many files stored in the directory service Service file with suffix ,systemctl It is to manage services according to these service files .

Write your own service documentation test.service, Then put it in the directory ,EXP:

[Unit]
Description=test
Documentation=empty
After=network.target remote-fs.target nss-lookup.target mysqld.service
[Service]
Type=oneshot
PIDFile=/run/test.pid
ExecStartPre=
ExecStart=/apps/bin/python /apps/test.py
ExecReload=
ExecStop=
PrivateTmp=true
[Install]
WantedBy=multi-user.target

The specific parameters can be set at Baidu ,service After the document is written, it can be used systemctl To manage .

systemctl enable test.service   # This command causes the service to start automatically

After the power on ,systemctl The tool will automatically run... In the service file ExecStart Command for parameter filling

Method 4: in the light of Ubuntu18.04

Recently in use Ubutnu18.04, I suddenly realized that there was no rc.local The file , It turns out that the new version is used uniformly systemctl To manage the service , The self starting service is no exception .

vim /lib/systemd/system/rc-local.service
# add to install Parameters
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
touch /etc/rc.local # establish rc.local file , Not created by default
Write the command that requires startup and self startup in rc.local Medium will do
systemctl status rc.local and systemctl status rc-local
You will find that you use the same service file , namely rc-local.service

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved