Cài đặt các requirements cần thiết cho script:
pip install requests python-telegram-bot pypiwin32
Code sample:
Tự động kiểm tra website có đang chạy hay không. Nếu không thì tự động thông báo lên group telegram và restart.
import win32com.shell.shell as shell
import requests
import time
import telegram
def send_message(mess):
try:
telegram_notify = telegram.Bot("bot token")
telegram_notify.send_message(chat_id="chat id", text=mess,
parse_mode='Markdown')
except Exception as ex:
print(ex)
def restart_iis():
send_message('restarting sample website...')
print('stop site sample')
cmdCommand =r'c:\windows\system32\inetsrv\appcmd.exe stop site /site.name:"hms2"'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+cmdCommand)
print('start site sample')
cmdCommand =r'c:\windows\system32\inetsrv\appcmd.exe start site /site.name:"hms2"'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+cmdCommand)
while True:
try:
if requests.get('https://example.com',timeout=30).status_code >400:
restart_iis()
time.sleep(240)
continue
except:
restart_iis()
time.sleep(240)
continue
time.sleep(70)
