詳解程序意外中斷自動重啟shell腳本(以Python為例)
我們經(jīng)常需要在后臺運行一些python腳本,來監(jiān)控系統(tǒng)或者做一些其他事情;但是 由于各種各樣的原因,排除python腳本代碼的問題,腳本運行過程中會掛掉。為了不天天耗在上面等重啟,可以制作shell腳本對程序予以監(jiān)控,對于意外中斷的程序自動重啟。
以控制 python自動重啟的shell腳本為例:
cd Desktop vim run.sh #新建名為run的shell腳本
寫入(此處以名為run的Python腳本為例)
#!/bin/bash while [ 1 ];do python run.py done
chmod 777 run.sh #設(shè)置shell腳本權(quán)限 ./run.sh #運行shell腳本


可見Python腳本意外中斷(被kill)后,由于shell腳本的循環(huán)語句,實現(xiàn)了自動重啟。

在測試完確保能夠正常運行后,切換為后臺運行:關(guān)于后臺運行命令原理,點此查看。
nohup ./run5.py &

此外,做爬蟲項目時,我們需要考慮一個爬蟲在爬取時會遇到各種情況(網(wǎng)站驗證,ip封禁),導(dǎo)致爬蟲程序中斷,這時我們已經(jīng)爬取過一些數(shù)據(jù),再次爬取時這些數(shù)據(jù)就可以忽略,所以我們需要在爬蟲項目中設(shè)置一個中斷重連的功能,使其在重新運行時從之前斷掉的位置重新爬取數(shù)據(jù)。此代碼參見自 匡虐博客
import os
class UrlManager(object):
def __init__(self): #建立兩個數(shù)組的文件
with open('new_urls.txt','r+') as new_urls:
self.new_urls = new_urls.read()
with open('old_urls.txt','r+') as old_urls:
self.old_urls = old_urls.read()
def add_new_url(self, url): #添加url到new_ulrs文件中
if url is None:
return
if url not in self.new_urls and url not in self.old_urls:
with open('new_urls.txt', 'a') as new_urls:
new_urls.write(url)
else:
print('url had done')
def add_new_urls(self, urls): #添加多個url到new_ulrs文件中
# if urls is None or (len(url) == 0 for url in urls):
if urls is None:
print('url is none')
return
for url in urls:
if urls is None:
print('url is none')
return
else:
self.add_new_url(url)
def has_new_url(self):
return len(self.new_urls) != 0
def get_new_url(self):
new_url = get_last_line('new_urls.txt') #讀取new_urls文件中最后一個url
del_last_url('new_urls.txt',new_url) #刪除new_urls文件中最后一個url
add_old_urls('old_urls.txt',new_url) #將讀取出來的url添加入old_urls數(shù)組中
return new_url
def get_last_line(inputfile):
filesize = os.path.getsize(inputfile)
blocksize = 1024
dat_file = open(inputfile, 'rb')
last_line = b""
lines = []
if filesize > blocksize:
maxseekpoint = (filesize // blocksize) # 這里的除法取的是floor
maxseekpoint -= 1
dat_file.seek(maxseekpoint * blocksize)
lines = dat_file.readlines()
while ((len(lines) < 2) | ((len(lines) >= 2) & (lines[1] == b'\r\n'))): # 因為在Windows下,所以是b'\r\n'
# 如果列表長度小于2,或者雖然長度大于等于2,但第二個元素卻還是空行
# 如果跳出循環(huán),那么lines長度大于等于2,且第二個元素肯定是完整的行
maxseekpoint -= 1
dat_file.seek(maxseekpoint * blocksize)
lines = dat_file.readlines()
elif filesize: # 文件大小不為空
dat_file.seek(0, 0)
lines = dat_file.readlines()
if lines: # 列表不為空
for i in range(len(lines) - 1, -1, -1):
last_line = lines[i].strip()
if (last_line != b''):
break # 已經(jīng)找到最后一個不是空行的
dat_file.close()
return last_line
def del_last_url(fname,part):
with open(fname,'rb+') as f:
a = f.read()
a = a.replace(part,b'')
with open(fname,'wb+') as f:
f.write(a)
def add_old_urls(fname,new_url):
line = new_url + b'\r'
with open(fname,'ab') as f:
f.write(line)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情
這篇文章主要介紹了Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06
用map函數(shù)來完成Python并行任務(wù)的簡單示例
這篇文章主要介紹了用map函數(shù)來完成Python并行任務(wù)的簡單示例,多線程和多進程編程的問題一直都是Python中的熱點和難點,需要的朋友可以參考下2015-04-04
Python使用Streamlit快速構(gòu)建一個數(shù)據(jù)應(yīng)用程序
Streamlit是一個開源的Python庫,它允許數(shù)據(jù)科學(xué)家和開發(fā)人員快速創(chuàng)建和分享數(shù)據(jù)應(yīng)用程序,而無需具備復(fù)雜的Web開發(fā)經(jīng)驗,本文將介紹Streamlit的基本用法,并通過一個實際案例展示如何快速構(gòu)建一個簡單的數(shù)據(jù)應(yīng)用程序,需要的朋友可以參考下2025-03-03
使用Python+Flask開發(fā)博客項目并實現(xiàn)內(nèi)網(wǎng)穿透
Flask是一個使用python編寫的輕量級Web框架,這篇文章我們將使用這個框架編寫一個屬于自己的博客網(wǎng)站!并教你如何通過使用內(nèi)網(wǎng)穿透工具處理項目,讓本地的項目可以在公網(wǎng)訪問,感興趣的可以了解一下2021-11-11
python的Crypto模塊實現(xiàn)AES加密實例代碼
這篇文章主要介紹了python的Crypto模塊實現(xiàn)AES加密實例代碼,簡單介紹了實現(xiàn)步驟,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
Python中數(shù)組,列表:冒號的靈活用法介紹(np數(shù)組,列表倒序)
下面小編就為大家分享一篇Python中數(shù)組,列表:冒號的靈活用法介紹(np數(shù)組,列表倒序),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

