溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

程序意外中斷自動重啟shell腳本的示例分析

發(fā)布時間:2021-07-16 13:49:10 來源:億速云 閱讀:138 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下程序意外中斷自動重啟shell腳本的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

我們經(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 #設置shell腳本權(quán)限
./run.sh   #運行shell腳本

程序意外中斷自動重啟shell腳本的示例分析

程序意外中斷自動重啟shell腳本的示例分析

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

程序意外中斷自動重啟shell腳本的示例分析

在測試完確保能夠正常運行后,切換為后臺運行:關于后臺運行命令原理,點此查看。

nohup ./run5.py &

程序意外中斷自動重啟shell腳本的示例分析

此外,做爬蟲項目時,我們需要考慮一個爬蟲在爬取時會遇到各種情況(網(wǎng)站驗證,ip封禁),導致爬蟲程序中斷,這時我們已經(jīng)爬取過一些數(shù)據(jù),再次爬取時這些數(shù)據(jù)就可以忽略,所以我們需要在爬蟲項目中設置一個中斷重連的功能,使其在重新運行時從之前斷掉的位置重新爬取數(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)

以上是“程序意外中斷自動重啟shell腳本的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI