LoginSignup
0
0

More than 1 year has passed since last update.

Apacheが実行されていることを監視(CSH)

Last updated at Posted at 2023-01-31

概要

今回はCSHでApacheが起動されているかどうか監視するスクリプトを作成したいと思います。

事前作業

以下のパッケージをRASPBERRY(LINUX)にインストールが必要です。

■パッケージ
・cshインストールコマンド
sudo apt-get install csh -y

・APACHEインストールコマンド
sudo apt install apache2 -y

■Apacheの起動コマンド
以下のコマンドで起動および停止ができます。
sudo apachectl stop
sudo apachectl start
sudo apachectl restart
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2

■Apacheのプロセスを確認コマンド
ps aux|grep apache|grep root
※grep apacheまたはgrep httpdで確認します。

CSHソース

下記のソースをrun.shに保存します。

#!/bin/csh -f
# APACHEの起動されている内容をPSコマンドから確認します。
set PSRUN=`ps -ef | grep apache|grep root > ./PS`

# PS結果からAPACHEの文字列を取得するための処理です。
set PSX = `cat ./PS | awk 'BEGIN{FS="sbin/"}{print $2}'|awk 'BEGIN{FS=" -k"}{print $1}'`

# PSの内容を出力します。起動されていない場合は、非表示になります。
cat ./PS

# 作業ファイルを削除します。
rm -rf ./PS

# apache2の文字列の比較です。OSによってhttpdの場合もあります。
if ( "$PSX" == "apache2" ) then
echo "APACHEが起動されている。"
endif

if ( "$PSX" == "" ) then
echo "APACHEが起動されていない。"
sudo systemctl start apache2
endif

実行結果

上記のCSHを以下のコマンドから実行します。

# `Apacheが軌道中の場合、表示されるメッセージ
pi@raspberrypi:~/work/php $ csh run.sh
root      2700     1  0 23:30 ?        00:00:00 /usr/sbin/apache2 -k start
APACHEが起動されている。

# Apacheの停止コマンドを実行
pi@raspberrypi:~/work/php $ sudo systemctl stop apache2

# Apacheが起動されていない
pi@raspberrypi:~/work/php $ ps aux |grep apache
pi        3002  0.0  0.1   5972   568 pts/1    S+   23:43   0:00 grep --color=auto apache

# Apacheが起動されていない場合、表示されるメッセージ
pi@raspberrypi:~/work/php $ csh run.sh
APACHEが起動されていない。
※この後で「sudo systemctl start apache2」コマンドで起動される

# Apacheの起動されていることを確認コマンド
pi@raspberrypi:~/work/php $ ps aux|grep apache
root      3025  0.5  3.4 192112 15128 ?        Ss   23:46   0:00 /usr/sbin/apache2 -k start
www-data  3029  0.0  1.0 192144  4680 ?        S    23:46   0:00 /usr/sbin/apache2 -k start
www-data  3030  0.0  1.0 192144  4680 ?        S    23:46   0:00 /usr/sbin/apache2 -k start
www-data  3031  0.0  1.0 192144  4680 ?        S    23:46   0:00 /usr/sbin/apache2 -k start
www-data  3032  0.0  1.0 192144  4680 ?        S    23:46   0:00 /usr/sbin/apache2 -k start
www-data  3033  0.0  1.0 192144  4680 ?        S    23:46   0:00 /usr/sbin/apache2 -k start
pi        3035  0.0  0.1   5972   592 pts/1    S+   23:46   0:00 grep --color=auto apache

終わりに

今回Apacheが起動されているか監視するスクリプトを作成した理由としてはApacheが停止されている場合、自動で起動するためのものです。
もしApacheの設定ファイルの問題がある場合は、正常に起動されない場合もあります。
下記のスクリプトですが、変数に設定してそのまま利用すればよいですが、
コンソールで上手く動作していなかったので、ファイル出力にしました。
set PSRUN=ps -ef | grep apache|grep root > ./PS
変数に設定したときのエラーメッセージは次のメッセージが表示され処理が中断されました。
^^;;

pi@raspberrypi:~/work/php $ csh run.sh
echo: No match.

以上です。 最後まで読んで頂いてありがとうございます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0