云网牛站
所在位置:首页 > Linux编程 > Bash脚本:实现在Linux上关闭时自动启动服务

Bash脚本:实现在Linux上关闭时自动启动服务

2019-08-05 09:36:10作者:戴进稿源:云网牛站

本文介绍六款Bash脚本,可以实现在Linux上关闭时自动启动服务,这些脚本都很简单,关键是非常实用,我在每个脚本中应用了不同的命令和方法,供你选择使用,同时记得更换自己的邮箱,而不是示例中的邮箱。

Bash脚本:实现在Linux上关闭时自动启动服务

 

1)如何在SysVinit Linux系统上关闭时自动启动服务

如果要在SysVinit Linux系统中启动单个服务,请使用以下bash脚本。

我在此脚本中添加了httpd服务,但你可以添加要监视的服务:

# vi /opt/scripts/service-monitor.sh

#!/bin/bash

serv=httpd

sstat=stopped

service $serv status | grep -i 'running\|stopped' | awk '{print $3}' | while read output;

do

echo $output

if [ "$output" == "$sstat" ]; then

service $serv start

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

else

echo "$serv service is running"

fi

done

为service-monitor.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor.sh

参考:在Linux系统下使用Crontab UI安全管理Cron定时任务

或者,可以使用以下脚本:

# vi /opt/scripts/service-monitor-1.sh

#!/bin/bash

serv=httpd

sstat=$(pidof $serv | wc -l )

if [ $sstat -gt 0 ]

then

echo "$serv is running fine!!!"

else

echo "$serv is down/dead"

service $serv start

echo "$serv service is UP now!!!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

fi

为service-monitor-1.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-1.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-1.sh

 

2)如何在systemd Linux系统上关闭时自动启动服务

如果要在systemd Linux系统中启动单个服务(参考:Systemd入门教程),请使用以下bash脚本。

我在此脚本中添加了httpd服务,但你可以添加要监视的服务:

# vi /opt/scripts/service-monitor-2.sh

#!/bin/bash

serv=httpd

sstat=dead

systemctl status $serv | grep -i 'running\|dead' | awk '{print $3}' | sed 's/[()]//g' | while read output;

do

echo $output

if [ "$output" == "$sstat" ]; then

systemctl start $serv

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

else

echo "$serv service is running"

fi

done

为service-monitor-2.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-2.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-2.sh

或者,可以使用以下脚本:

# vi /opt/scripts/service-monitor-3.sh

#!/bin/bash

serv=httpd

sstat=$(pidof $serv | wc -l )

if [ $sstat -gt 0 ]

then

echo "$serv is running fine!!!"

else

echo "$serv is down/dead"

systemctl start $serv

echo "$serv service is UP now!!!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

fi

为service-monitor-3.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-3.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-3.sh

 

3)如何在SysVinit Linux系统上关闭时自动启动多项服务

如果要在SysVinit Linux系统中启动多个服务,请使用以下bash脚本。

我在此脚本中添加了httpd和rsyslog服务,但可以添加要监视的服务:

# vi /opt/scripts/service-monitor-4.sh

#!/bin/bash

sstat=stopped

for serv in httpd rsyslog

do

service $serv status | grep -i 'running\|stopped' | awk '{print $3}' | while read output;

do

echo $output

if [ "$output" == "$sstat" ]; then

service $serv start

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

else

echo "$serv service is running"

fi

done

done

为service-monitor-4.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-4.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-4.sh

或者,可以使用以下脚本:

# vi /opt/scripts/service-monitor-5.sh

#!/bin/bash

for serv in rsyslog httpd

do

sstat=$(pgrep $serv | wc -l )

if [ $sstat -gt 0 ]

then

echo "$serv is running!!!"

else

echo "$serv is down/dead"

service $serv start

echo "$serv serv is UP now!!!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

fi

done

为service-monitor-5.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-5.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-5.sh

 

4)如何在systemd Linux系统上关闭时自动启动多个服务

如果要在systemd Linux系统中启动多个服务,请使用以下bash脚本。

我在此脚本中添加了httpd和rsyslog服务,但可以添加要监视的服务:

# vi /opt/scripts/service-monitor-6.sh

#!/bin/bash

sstat=dead

for serv in httpd rsyslog

do

systemctl status $serv | grep -i 'running\|dead' | awk '{print $3}' | sed 's/[()]//g' | while read output;

do

echo $output

if [ "$output" == "$sstat" ]; then

systemctl start $serv

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

else

echo "$serv service is running"

fi

done

done

为service-monitor-6.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-6.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-6.sh

或者,可以使用以下脚本:

# vi /opt/scripts/service-monitor-7.sh

#!/bin/bash

for serv in rsyslog httpd

do

sstat=$(pgrep $serv | wc -l )

if [ $sstat -gt 0 ]

then

echo "$serv is running!!!"

else

echo "$serv is down/dead"

systemctl start $serv

echo "$serv serv is UP now!!!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

fi

done

为service-monitor-7.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-7.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-7.sh

 

5)如何在systemd和SysVinit Linux系统上关闭时自动启动服务

如果要在systemd和SysVinit Linux系统中启动服务,请使用以下bash脚本。

我在此脚本中添加了httpd服务,但可以添加要监视的服务:

# vi /opt/scripts/service-monitor-8.sh

#!/bin/bash

smanager=$(ps -p1 | grep "init\|systemd" | awk '{print $4}')

serv=httpd

if (( $(pidof $serv | wc -l) > 0 ))

then

echo "$serv is running!!!"

elif [ "$smanager" == "init" ]

then

service $serv start

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

else

systemctl start $serv

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

fi

为service-monitor-8.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-8.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-8.sh

 

6)如何在systemd和SysVinit Linux系统上关闭时自动启动多个服务

如果要在systemd和SysVinit Linux系统中启动多个服务,请使用以下bash脚本。

我在此脚本中添加了httpd和rsyslog服务,但可以添加要监视的服务:

# vi /opt/scripts/service-monitor-9.sh

#!/bin/bash

smanager=$(ps -p1 | grep "init\|systemd" | awk '{print $4}')

for serv in rsyslog httpd

do

if (( $(pgrep $serv | wc -l) > 0 ))

then

echo "$serv is running!!!"

elif [ "$smanager" == "init" ]

then

service $serv start

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

else

systemctl start $serv

echo "$serv service is UP now.!" | mail -s "$serv service is DOWN and restarted now On $(hostname)" 2daygeek@gmail.com

fi

done

为service-monitor-9.sh文件设置可执行权限:

$ chmod +x /opt/scripts/service-monitor-9.sh

最后添加一个cronjob来自动执行此操作,它将每5分钟运行一次:

# crontab -e

*/5 * * * * /bin/bash /opt/scripts/service-monitor-9.sh

 

相关主题

在Linux上安装Gnome Tweak以在Gnome Shell上自动启动程序

精选文章
热门文章